关的程序,今天和别人讨论问题时,刚好说到有关GlassPane的程序,我想当然地给了对方一个答复,结果他写完程序后发现有问题,这才让我自己想到要
去写个关于GlassPane的程序,我是按想当然的思路写的测试程序(其实很多人都是这么写的):
JPanel glassPane = new JPanel();
glassPane.addMouseListener(new MouseListener(){
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
System.out.println("Click");
}
});
JFrame.setGlassPane(glassPanel);
glassPane.addMouseListener(new MouseListener(){
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
System.out.println("Click");
}
});
JFrame.setGlassPane(glassPanel);
很显然,我的本意是想用GlassPane去截获所有鼠标事件,并对单击事件会打印文字,但是运行后却完全没效果。
所以我猜测GlassPane并没有起作用,于是查了些资料,才发现原来错的多了,现给出正确的GlassPane的用法:
因为GlassPane在JRootPane中是一个Component
下面是人家API的源代码
因为GlassPane在JRootPane中是一个Component
下面是人家API的源代码
<span style="font-size:18px;">public class JRootPane extends JComponent implements Accessible { private static final String uiClassID = "RootPaneUI"; private static final boolean LOG_DISABLE_TRUE_DOUBLE_BUFFERING = ((Boolean) AccessController .doPrivileged(new GetBooleanAction( "swing.logDoubleBufferingDisable"))).booleanValue(); private static final boolean IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING = ((Boolean) AccessController .doPrivileged(new GetBooleanAction( "swing.ignoreDoubleBufferingDisable"))).booleanValue(); public static final int NONE = 0; public static final int FRAME = 1; public static final int PLAIN_DIALOG = 2; public static final int INFORMATION_DIALOG = 3; public static final int ERROR_DIALOG = 4; public static final int COLOR_CHOOSER_DIALOG = 5; public static final int FILE_CHOOSER_DIALOG = 6; public static final int QUESTION_DIALOG = 7; public static final int WARNING_DIALOG = 8; private int windowDecorationStyle; protected JMenuBar menuBar; protected Container contentPane; protected JLayeredPane layeredPane; protected Component glassPane; protected JButton defaultButton;</span>
public class GlassPane extends JComponent implements MouseListener{
public GlassPane(){
this.addMouseListener(this);
}
public void paint(Graphics g)//重载Paint以保证GlassPane不透明
{}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
System.out.println("Click");
}
}
public GlassPane(){
this.addMouseListener(this);
}
public void paint(Graphics g)//重载Paint以保证GlassPane不透明
{}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
System.out.println("Click");
}
}
在调用时,这样:
GlassPane gp = new GlassPane();
JFrame.setGlassPane(gp);
gp.setVisible(true)//这个是用于开启GlassPane 的
GlassPane的写法大致是这样,可以根据要求改写任何部分,虽然简单,但是因为一直都缺少相应的文档和资料以至于很多人都用不好,但是它强大的功能却是值得任何一个SWING开发人员注意的。