Results 1 to 3 of 3
- 11-18-2010, 08:40 AM #1
Member
- Join Date
- Aug 2009
- Location
- Richmond, Canada
- Posts
- 15
- Rep Power
- 0
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
JIAWelcome to Vision Open
http://www.visionopen.com
- 11-18-2010, 08:48 AM #2
Member
- Join Date
- Aug 2009
- Location
- Richmond, Canada
- Posts
- 15
- Rep Power
- 0
How to remove this topic?
Problem solved...
this.getContentPane().add(panel);
is a must...Welcome to Vision Open
http://www.visionopen.com
- 11-18-2010, 02:03 PM #3
Similar Threads
-
JPanel/JFrame
By Mayur in forum New To JavaReplies: 2Last Post: 12-20-2009, 05:07 AM -
JFrame and JPanel -Error
By britto_bicsjohn in forum AWT / SwingReplies: 2Last Post: 08-25-2009, 01:31 PM -
JFrame and two JPanel Classes
By YellowPainting in forum AWT / SwingReplies: 9Last Post: 08-25-2009, 12:49 PM -
Tabs Won't Work In Right Aligned JPanel
By davidedwardgill in forum AWT / SwingReplies: 0Last Post: 08-08-2008, 07:45 PM -
Listener for JFrame size change
By Thez in forum AWT / SwingReplies: 10Last Post: 02-14-2008, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks