最近刚学习swing,书中说Glass Pane实际上是个JPanel对象,它负责抓鼠标消息,位于最上层。我索性想把这个透明的窗口显示出来,可是失败了,请各位帮助看看我为什么不能显示它。
public class FirstApplet
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Swing application");
JButton button = new JButton("Hello Swing Appliction");
frame.getContentPane().add(button, BorderLayout.EAST);
//在此我抓出 Glass Pane 对象,取消它的透明属性,并对其着色==============
((JPanel)frame.getGlassPane()).setOpaque(false);
((JPanel)frame.getGlassPane()).setBackground(Color.blue);
((JPanel)frame.getGlassPane()).setBorder(BorderFactory.createLineBorder(Color.black));
//=======================================================================
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public class FirstApplet
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Swing application");
JButton button = new JButton("Hello Swing Appliction");
frame.getContentPane().add(button, BorderLayout.EAST);
//在此我抓出 Glass Pane 对象,取消它的透明属性,并对其着色==============
((JPanel)frame.getGlassPane()).setOpaque(false);
((JPanel)frame.getGlassPane()).setBackground(Color.blue);
((JPanel)frame.getGlassPane()).setBorder(BorderFactory.createLineBorder(Color.black));
//=======================================================================
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
((JPanel)frame.getGlassPane()).setVisible(true);
把边框的颜色改为red,就能看到了。