Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Undefined variable: this

843810Oct 7 2005 — edited Oct 10 2005
When running a swing application I get an undefined variable: this error when attempting to call the method addActionListener(), I can not see why I am getting this error as this is not a variable but rather a reserved word
import javax.swing.*;
import java.awt.*;  
import java.awt.event.*;      

public class GUIPractice implements ActionListener {
	
   
    private static void createAndShowGUI() {    
    	JFrame frame = new JFrame("Address Book");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(6,1));
        JLabel label = new JLabel("<html><font color=black size=6>MMMMMMMMM</font><font color=lime size=7>Address Book</font></html>");
        frame.getContentPane().add(label);
        JButton newBook = new JButton("<html><font color=navy size=6>Create New Book</font></html>");
        newBook.addActionListener(this);
        JButton addEntry = new JButton("<html><font color=navy size=6>Add Contact</font></html>");
        addEntry.addActionListener(this);
        JButton change = new JButton("<html><font color=navy size=6>Change Contact</font></html>");
        change.addActionListener(this);
        JButton display = new JButton("<html><font color=navy size=6>Display Book</font></html>");
        display.addActionListener(this);
        JButton quit = new JButton("<html><font color=navy size=6>Quit</font></html>");
        quit.addActionListener(this);
        frame.setBackground(new Color(0x000000));
        frame.getContentPane().add(newBook);
        frame.getContentPane().add(addEntry);
        frame.getContentPane().add(change);
        frame.getContentPane().add(display);
        frame.getContentPane().add(quit);
        
        frame.pack();
        frame.setVisible(true);
        
        frame.setSize(600,600);
    }
    
    public void actionPerformed(ActionEvent e)
    {
    JFrame frame = new JFrame("Address Book");
    String s=e.getActionCommand();
    if(s.equals("Create New Book"))
		JOptionPane.showMessageDialog(frame,"You are creating a new address book");
	else if(s.equals("Add Contact"))
		JOptionPane.showMessageDialog(frame,"You are adding another contact to your address book");
	else if(s.equals("Change Contact"))
		JOptionPane.showMessageDialog(frame,"You are changing a contact in your address book");
	else if(s.equals("Display Book"))
		JOptionPane.showMessageDialog(frame,"You are displaying a address book");
	else if(s.equals("Quit"))
		JOptionPane.showMessageDialog(frame,"Goodbye");
	}

    public static void main(String[] args) {
    	GUIPractice g = new GUIPractice();
    
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
any help would be greatly appreciated
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 7 2005
Added on Oct 7 2005
3 comments
242 views