Results 1 to 3 of 3
  1. #1
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default JPanel Listener doesn't work in a JFrame...

    Hi, all:

    Sorry for my silly question. Not quite familiar with Java GUI.

    My code looks like:
    Java Code:
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class JPanelInJFrame  extends JFrame {
    	JPanel panel;
    	
    	  public JPanelInJFrame() {
    			panel = null;
    	  }
    	  
    	  public void start() {
    		  panel = new JPanel();
    		  panel.setSize(200,200);
    		  panel.setVisible(true);
    		  
    		  panel.addMouseListener(new MouseAdapter() {
    				@Override
    				public void mousePressed(MouseEvent e) {
    					System.out.println(e);
    				}
      	});
    		  
    		this.pack();
    		this.setVisible(true);
    	  }
    	  
    	public static void main(String[] args) {
    		JPanelInJFrame frame = new JPanelInJFrame();
    		frame.setSize(200, 200);
    	    frame.setVisible(true);
    		frame.start();
    	  }
    }

    I guess there must be a very silly fault in my implementation, but where is it? Why the JPanel's listener doesn't work for JPanel itself? How to revise it?

    Thank you so much in advance.

    Best Regards
    JIA
    Welcome to Vision Open
    http://www.visionopen.com

  2. #2
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default

    How to remove this topic?
    Problem solved...

    this.getContentPane().add(panel);

    is a must...
    Welcome to Vision Open
    http://www.visionopen.com

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

Similar Threads

  1. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 05:07 AM
  2. JFrame and JPanel -Error
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 08-25-2009, 01:31 PM
  3. JFrame and two JPanel Classes
    By YellowPainting in forum AWT / Swing
    Replies: 9
    Last Post: 08-25-2009, 12:49 PM
  4. Tabs Won't Work In Right Aligned JPanel
    By davidedwardgill in forum AWT / Swing
    Replies: 0
    Last Post: 08-08-2008, 07:45 PM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 03:10 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
  •