Hi, I'm having problems with my GUI java application. I get an error when I try to add an Aciton Listener to a button. I'm not sure if I am doing this the right way. Any help would be much appreciated.
Here is my code:
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class JavaLabel extends JFrame implements ActionListener{ public static JLabel label; public static void main(String[] args){ JLabel label; JTextField edit; JTextField noedit; JLabel Entername; JTextField name = null; JButton click; final String StoreName = ""; edit = new JTextField(); name = new JTextField(); noedit = new JTextField("Enter Text: "); label = new JLabel("label"); JFrame frame = new JFrame("frame"); Entername = new JLabel("enter name: "); click = new JButton("Okay"); click.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getSource() == this){ StoreName = name.getText(); JOptionPane.showMessageDialog(null, StoreName); System.exit(0); } } }); Entername.setBounds(100, 200, 120, 30); name.setBounds(100, 250, 130, 30); click.setBounds(100, 300, 60, 30); Dimension size1 = edit.getPreferredSize(); Dimension size2 = noedit.getPreferredSize(); Dimension size3 = label.getPreferredSize(); edit.setBounds(100, 30, 100, size1.height); noedit.setBounds(30, 30, size2.width, size2.height); label.setBounds(200 - size3.width, 5, size3.width, size3.height); noedit.setEditable(false); frame.setLayout(null); frame.setSize(400,400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(label); frame.add(edit); frame.add(noedit); frame.add(click); frame.add(name); frame.add(Entername); } }
I get an error on line 4 with the JavaLabel.
I also get an error in line 26 inside the public void actionPerformed. I get an error on the name.getText();
Please any help would be great!
THanks