Results 1 to 7 of 7

Thread: Selly Mistake

  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Selly Mistake

    Hi

    As a review, I wrote the following code to implement Java Applet. The problems is when I press the button, the label does not change. I think the problem with "static" that I declared before each declaration of components.

    Java Code:
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Jframe implements ActionListener{
    	
    	public static JFrame frame = new JFrame("FrameDemo");
    	public static JLabel lbl = new JLabel("Hello .. Press Enter");
    	public static JButton button = new JButton("Press");
    	
    	public static void main(String[] args){
    		
    		frame.setSize(400, 150);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		Container content = frame.getContentPane();
    	    content.setLayout(new FlowLayout()); 
    	   
    	    content.add(lbl);
    	    
    	    content.add(button);
    	       
    	    
    	}
    	
    	public Jframe(){
    		button.addActionListener(this);	 
    	}
    
    	public void actionPerformed(ActionEvent arg0) {
    		if (arg0.getSource()==button){
    		lbl.setText("Welcome");
    		}
    	}
    }
    Thank you.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default

    There's a difference between a JFrame and a Jframe ...

    edit: darn, those names are misleading ...

    kind regards,

    Jos
    Last edited by JosAH; 04-19-2011 at 03:17 PM.
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    It didn't work :(

    Thank you for your helping

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    It didn't work :(

    Thank you for your helping
    Stick a few System.out.printlns( ... ) in your actionPerformed( ... ) method and see if it is called.

    kind regards,

    Jos

    ps. and please change the class name, it's making me dizzy.
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TryJframe implements ActionListener{
    	
    	public static JFrame frame = new JFrame("FrameDemo");
    	public static JLabel lbl = new JLabel("Hello .. Press Enter");
    	public static JButton button = new JButton("Press");
    	
    	public static void main(String[] args){
    		
    		frame.setSize(400, 150);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		Container content = frame.getContentPane();
    	    content.setLayout(new FlowLayout()); 
    	   
    	    content.add(lbl);
    	    
    	    content.add(button);
    	       
    	    
    	}
    	
    	public TryJframe(){
    		button.addActionListener(this);	 
    	}
    
    	public void actionPerformed(ActionEvent arg0) {
    		if (arg0.getSource()==button){
    		lbl.setText("Welcome");
    		}
    	}
    }
    I did what you told me but nothing has changed. It seems the problem with the button.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Java Code:
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TryJframe implements ActionListener{
    	
    	public static JFrame frame = new JFrame("FrameDemo");
    	public static JLabel lbl = new JLabel("Hello .. Press Enter");
    	public static JButton button = new JButton("Press");
    	
    	public static void main(String[] args){
    		
    		frame.setSize(400, 150);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		Container content = frame.getContentPane();
    	    content.setLayout(new FlowLayout()); 
    	   
    	    content.add(lbl);
    	    
    	    content.add(button);
    	       
    	    
    	}
    	
    	public TryJframe(){
    		button.addActionListener(this);	 
    	}
    
    	public void actionPerformed(ActionEvent arg0) {
    		if (arg0.getSource()==button){
    		lbl.setText("Welcome");
    		}
    	}
    }
    I did what you told me but nothing has changed. It seems the problem with the button.
    You didn't add a System.out.println("whatever ...") to your actionPerformed( ... ) method. You should at least see it output the String when you press that button.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default

    Got it: you didn't create a new TryJFrame() in your main method so there is no ActionListener attached to your button ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Point my mistake
    By amzers in forum New To Java
    Replies: 11
    Last Post: 12-16-2010, 01:31 PM
  2. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 11:03 PM
  3. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 3
    Last Post: 04-29-2009, 05:26 AM
  4. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 2
    Last Post: 04-28-2009, 06:30 AM
  5. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 11:50 AM

Posting Permissions

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