When I compile this code, I get an error - 'undefined variable this ' at the submitBtn.addActionListener(this). Can anyone see what the problem is ? import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class initial extends JInternalFrame implements ActionListener { // Declarations & Initialisations private static JTabbedPane tabbedPane = new JTabbedPane(); private static JPanel topPanel = new JPanel(); private static JPanel panel1 = new JPanel(); private static JPanel panel2 = new JPanel(); private static JButton submitBtn = new JButton("Submit"); private static JLabel userNameL = new JLabel("Username :"); private static JLabel passwordL = new JLabel("Password :"); private static JTextField userNameTF = new JTextField(); private static JPasswordField passwordPF = new JPasswordField(); private static JDesktopPane desktop = new JDesktopPane(); public static String userName = new String(); public static String password = new String(); private static JScrollPane scrollPane1;
//System.out.println("in class initial"); public initial() { setClosable( true ); setMaximizable( true ); setIconifiable( true ); setResizable( true ); setTitle("Network Info Tool"); setSize(210, 250);
System.out.println("in method initial");
// Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel );
private void CreateTopPane( JPanel topPanel ) { // Create a text area JTextArea area = new JTextArea(); // Load a file into the text area, catching any exceptions try { FileReader fileStream = new FileReader( "TestFrame.java" ); area.read( fileStream, "TestFrame.java" ); } catch( FileNotFoundException e ) { System.out.println( "File not found" ); } catch( IOException e ) { System.out.println( "IOException occurred" ); } // Create the scrolling pane for the text area scrollPane1 = new JScrollPane(); scrollPane1.getViewport().add( area ); topPanel.add( scrollPane1, BorderLayout.CENTER ); }
That's because your method createPage1 is declared as static. Staic methods exist for all object instances, and have no "this". It should work if you remove the "static".