10 Replies - 2005 Views - Last Post: 01 March 2010 - 05:21 PM Rate Topic: -----

#1 Zjm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-January 10

main method errors. Action Listeners

Posted 01 March 2010 - 08:39 AM

Hey everyone, here is a program that uses beginner GUI techniques. I am trying to implement a listener for my first button. My test program compiles, but my main program does had two errors. The errors are marked with comment lines.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



	public class HelloFrame extends JFrame
	{
		public HelloFrame()
		{
			message = "Hello, World!";
			label = new JLabel(message);
			label.setFont(new Font("serif", Font.PLAIN, DEFAULT_SIZE));
			add(label, BorderLayout.CENTER);
			setSize(FRAME_WIDTH, FRAME_HEIGHT);
		}
		private String message;
		private JLabel label;
		private static int FRAME_WIDTH = 300;
		private static int FRAME_HEIGHT = 200;
		private static int DEFAULT_SIZE = 20;
		private JButton smallerButton; 
        private JButton largerButton;
	
	
	public class createSouthPanel 
     { 
                                   
        public createSouthPanel() 
         { 
             smallerButton = new JButton("Smaller"); 
             largerButton = new JButton("Larger"); 
                                         
               JPanel southPanel = new JPanel();        
               southPanel.add(smallerButton); 
               southPanel.add(largerButton); 
               add(southPanel, BorderLayout.SOUTH); 
               	
               	
               	
               	class LargerFontListener implements ActionListener
               	{
               		public void actionPerformed(ActionEvent event)
               		{
               			
               			fontSizeFactor = 1.25 * fontSizeFactor;
               			label.setFont(new Font("serif", Font.PLAIN, (int) (DEFAULT_SIZE * fontSizeFactor)));
               			label.repaint();
               			
               		}
               		private double fontSizeFactor = 1;
               	}
         } 
     }
	}
     




and the driver


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

	public class HelloTester extends JFrame
	{
		public static void main(String[]args)
		{
			JFrame frame = new HelloFrame();
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setTitle("HelloTester");
			frame.setVisible(true);
			
			JPanel southPanel = new createSouthPanel(); 
            frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
            
            ActionListener largerListener = new LargerFontListener(); //cannot find symbol class LargerFontListener
            largerButton.addActionListener(largerListener); // cannot find symbol variable largerButton
		}
		
	}




any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: main method errors. Action Listeners

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 08:56 AM

Let's start with your JFrame class. Nowhere in it do you set any of its key properties (save size) like visibility and default close operation. In addition, I don't see where you add your JPanel to your JFrame. So here's a basic example with comments to display the number of times a button has been clicked:
public class MyFrame extends JFrame implements ActionListener{
    JPanel panel;
    JLabel label;
    JButton button;
    int count;
     /*
       Your constructor is a place where you 
       should set up your Frame. This includes
       its properties as well as its components.
       Make sure to initialize all variables,
       add them to the Panel, add the Panel to the frame,
       set up listeners, etc.
     */
     public MyFrame(){
         //set up properties
         this.setSize(200,200);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          //init components
         panel = new JPanel();
         label = new JLabel();
         button = new JButton();

         //because MyFrame implements ActionListener
         //you want this class (or this object) to handle
         //the ActionEvents
         button.addActionListener(this);

         count = 0;

         //add components to panel
         panel.add(label);
         panel.add(button);
         this.add(panel);
         this.setVisible(true);
     } 

     //on each click, increment count and display count 
     //on JLabel
     public void actionPerformed(ActionEvent e){
        count++;
        label.setText(count + ""); 
     }
}



Then for your main method, the only thing you really need to do is instantiate an instance of your JFrame class. Like so:
public static void main(String[] args){
   //the frame will now pop up
   new MyFrame();
}


Was This Post Helpful? 0
  • +
  • -

#3 Zjm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-January 10

Re: main method errors. Action Listeners

Posted 01 March 2010 - 09:26 AM

Thanks for the reply. Im not sure I understand. In the main method, is not this statement placing my panel into a frame?

JPanel southPanel = new createSouthPanel(); 
frame.getContentPane().add(southPanel, BorderLayout.SOUTH);



If I take out the code fragment, both programs compile and my frame with my panel containing the two buttons do show. When I add actions to the buttons, that is when I get the error.
 ActionListener largerListener = new LargerFontListener(); //cannot find symbol class LargerFontListener
 largerButton.addActionListener(largerListener); // cannot find symbol variable largerButton



I know I should not be putting these lines of code in the main method, but its what my instructor wanted. He gave us a print out that show what he wants us to do.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 09:39 AM

My point is that you have 3 places where you work with JFrame: HelloFrame extends JFrame, HelloTester extends JFrame, and a third one in the main() method, when you should only have one- HelloFrame extends JFrame. Also, the JPanel should be added to the JFrame in the HelloFrame class.

Now the reason you are getting an error for your ActionListener is b/c LargeFontListener is encapsulated within your HelloFrame, and you don't note so in your main() method. To access an inner class outside of the outer class, do such:
outerClassName.innerClassName


Was This Post Helpful? 0
  • +
  • -

#5 Zjm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-January 10

Re: main method errors. Action Listeners

Posted 01 March 2010 - 10:24 AM

Thats what I originally thought the problem was, but I was thinking scope. Is there a way where I can rearrange my program so my action class is not encapsulated within another class?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 10:44 AM

Scope is the exact term I would use to describe the problem, so you are right on. To re-arrange the class, I would have your HelloFrame extend JFrame and implement ActionListener, as I showed in my example above.

Also, you should talk to your instructor to better understand what he or she wants, as what you have now is two approaches that are very redundant and counter-intuitive (no offense). It wouldn't be a bad idea (unless you're not supposed to be getting outside help) to show your instructor this thread and the suggestions we've talked about, and see if this is more of what he or she wants.
Was This Post Helpful? 0
  • +
  • -

#7 Zjm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-January 10

Re: main method errors. Action Listeners

Posted 01 March 2010 - 11:25 AM

Ok I really appreciate it. I am going to try and finish the program both ways, that way I can show him. For the original way I was doing it, Can you show me the code for accessing the inner class outside of the outer class?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 11:31 AM

Access your inner class using the dot-notation, like so: HelloFrame.LargeFontListener.
Was This Post Helpful? 0
  • +
  • -

#9 Zjm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-January 10

Re: main method errors. Action Listeners

Posted 01 March 2010 - 12:15 PM

So the statement should look as follows
ActionListener largerListener = new HelloFrame.LargerFontListener(); 
            largerButton.addActionListener(largerListener);



Is this correct?

This post has been edited by Zjm8: 01 March 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 05:07 PM

void actionPerformed() method is an instantiated method called for an object.

As objects declared/defined in the static main() method do not belongs to any object they cannot have an ActionListener()

Actually your main() method is too long it shoul look thike this

public static void main(String[] args) {
.. new HelloFrame();
}

all the rest, including the button definition and its ActionListener should be in the constructor of HelloFrame
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: main method errors. Action Listeners

Posted 01 March 2010 - 05:21 PM

View Postpbl, on 01 March 2010 - 08:07 PM, said:

Actually your main() method is too long it shoul look thike this

public static void main(String[] args) {
.. new HelloFrame();
}

all the rest, including the button definition and its ActionListener should be in the constructor of HelloFrame


I agree with you there, but I think the OP said that his instructor wants him to do it this way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1