Results 1 to 5 of 5
  1. #1
    XelaIsPwn is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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.

    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);
    		
    	}
      }
    But every freakin' time- "lenfield cannot be resolved, numfield cannot be resolved".

    (and, yes, I could have done javax.swing.*;, but I got kind of desperate ^^;)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    XelaIsPwn is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you sure that you didn't just copy things wrong? You would remove the end curly brace "}" that is immediately above this line:
    Java Code:
    class ButtonListener implements ActionListener {
    and add an end curly brace to the bottom. Then change the line above to:

    Java Code:
    private class ButtonListener implements ActionListener {

  5. #5
    XelaIsPwn is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Oh. Duh. Looks like I did copy something wrong, I'm a moron. xD

    Thanks anyway, dude, I'll be sure to remember in the future!

Similar Threads

  1. Best way to implement an ActionListener.
    By VisionIncision in forum New To Java
    Replies: 4
    Last Post: 12-07-2010, 12:52 AM
  2. ActionListener w/ Dialog
    By BariMutation in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 04:50 PM
  3. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 12:25 AM
  4. JComboBox ActionListener and Swing Thread safety
    By frenk_castle in forum AWT / Swing
    Replies: 6
    Last Post: 04-05-2010, 10:02 AM
  5. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •