I have been stuck writing this program for 3 days now and every example I have come across seems to do it exactly the same way I am trying. I do not know what I am missing, its obviously something easy, someone please point it out to me
Basically I need to get text from a textfield on button press. My error is the actionPerformed can't see the text field. I have tried in an abstract method, a seperate class, and in the same class, all failed. I need to do more stuff to the text it after this happens, but I figure after I get over this hump, the rest will be easy.
Current Compiling Issues:
cannot find symbol method addActionListener(TheFrame)
I got this recently since i changed to 'implimenting ActionListener' in my TheFrame class, was not having trouble with this prior when I had a seperate class to handle it and made an object of it in TheFrame.
cannot find symbol variable textField
This is my main problem! The actionPerformed can't find textField??? What do I need to do to get it to see textField.
The Code:
public class TheFrame extends JFrame implements ActionListener
{
public TheFrame()
{
JPanel center = new JPanel();
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JLabel label1 = new JLabel("Text Field");
JLabel label2 = new JLabel("Text Area");
JLabel label3 = new JLabel("Combo Box");
JButton button = new JButton("Move");
JComboBox comboBox = new JComboBox();
JTextArea textArea = new JTextArea(10,20);
JTextField textField = new JTextField(20);
button.addActionListener(this);
comboBox.addActionListener(this);
textArea.addActionListener(this);
textField.addActionListener(this);
center.setLayout(new FlowLayout());
center.add(label1);
center.add(textField);
center.add(label2);
center.add(textArea);
center.add(label3);
center.add(comboBox);
top.setBackground(Color.WHITE);
top.add(new JLabel("By ..."));
bottom.setBackground(Color.WHITE);
bottom.add(button);
add(top, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
String text = textField.getText();
System.out.println(text);
}
public static void main(String[] arrrrggggggghhhhhs)
{
TheFrame frame = new TheFrame();
frame.setTitle("Action Event Demo");
frame.setSize(300, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Thanks in advance!!

Start a new topic
Add Reply




MultiQuote
| 


