Results 1 to 5 of 5
Thread: Swing/ActionListener help?
- 03-01-2011, 02:13 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Swing/ActionListener help?
Quite honestly I'm very new at Java, but any help is greatly appreciated. ^^;
This is a program I have to make for my AP Computer Science class- I have to make a program that calculates a regular polygon.
I have pretty much everything worked out- except the whole "getting input" part.
But every freakin' time- "lenfield cannot be resolved, numfield cannot be resolved".Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class MainClass extends JPanel { private JTextField lenfield = new JTextField(" "); private JTextField numfield = new JTextField(" "); public MainClass() { JLabel len = new JLabel("Side Length:"); JLabel numsides = new JLabel("Number of Sides:"); JButton calcbutton = new JButton("Calculate"); calcbutton.addActionListener(new ButtonListener()); add(len); add(lenfield); add(numsides); add(numfield); add(calcbutton); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); } } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e){ String input; double len; int num; input=lenfield.getText(); len= Double.parseDouble(input); input=numfield.getText(); num=Integer.parseInt(input); Polygon poly= new Polygon(num, len); System.out.println(len+" "+num); } }
(and, yes, I could have done javax.swing.*;, but I got kind of desperate ^^;)
-
You declare the variable in one class and try to use it in another class, so the errors shouldn't be too surprising. Perhaps you want to make the listener class a private inner class?
- 03-01-2011, 02:26 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Blah, this freaking book lied to me, I practically copied the code word for word. >.>;
Uh... sorry, I'm relatively new at this. How would I go about doing so, just make the actionlistener a private class?
-
Are you sure that you didn't just copy things wrong? You would remove the end curly brace "}" that is immediately above this line:
and add an end curly brace to the bottom. Then change the line above to:Java Code:class ButtonListener implements ActionListener {
Java Code:private class ButtonListener implements ActionListener {
- 03-01-2011, 02:58 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Best way to implement an ActionListener.
By VisionIncision in forum New To JavaReplies: 4Last Post: 12-07-2010, 12:52 AM -
ActionListener w/ Dialog
By BariMutation in forum New To JavaReplies: 1Last Post: 12-03-2010, 04:50 PM -
ActionListener+KeyListener
By mandelbrot in forum AWT / SwingReplies: 5Last Post: 09-10-2010, 12:25 AM -
JComboBox ActionListener and Swing Thread safety
By frenk_castle in forum AWT / SwingReplies: 6Last Post: 04-05-2010, 10:02 AM -
How to use KeyListener and ActionListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks