GlassPane没有出现在JFrame上

时间:2012-06-26 13:51:22

标签: java swing jframe glasspane

我一直在努力为我的游戏中的游戏内HUD实现GlassPane,但是现在我似乎无法让JFrameGlassPane设置为自己的游戏已经使用setGlassPane()并且我已经阅读了一些试图找到我的错误的例子,但没有。所以我写了一个SSCCE来证明我的问题。我有一个JFrame,我添加了一个标签为“TESTIING”的Jpanel然后我启动我的galss窗格并在我的frame实例上调用setGlassPane()。我的GlassPane有一个MouseListener,一个JPanel和两个JLabels,以及一个覆盖paint(),但MouseListener不会显示paint()不会显示和我的标签不存在(基本上我的GlassPane未被设置为我的框架GlassPane) -

/*Main.java*/

import java.awt.EventQueue;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestGlassPane testGlassPane=new TestGlassPane();
                testGlassPane.setVisible(true);
            }
        });
    }
}

/*TestGlassPane.java*/

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class TestGlassPane extends JFrame{
    private GlassGamePane m_glassPane;
    private JPanel drawingPanel;
    private JLabel testLabel;

    public TestGlassPane() {
        createUI();
    }
    private void createUI() {
        setTitle("Test GlassGamePane");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 700);
        setResizable(false);
        setLocationRelativeTo(null);

        createComponents();
        //add components to frames content pane
        addComponentsToContentPane(getContentPane());


        //setting glassPane 
        m_glassPane = new GlassGamePane();
        //set opaque to false, i.e. make transparent 
        m_glassPane.setOpaque(false);
        m_glassPane.setVisible(true);

        getRootPane().setGlassPane(m_glassPane);
    }

    private void addComponentsToContentPane(Container contentPane) {
        drawingPanel.add(testLabel);
        contentPane.add(drawingPanel, BorderLayout.CENTER);
    }

    private void createComponents() {
        drawingPanel=new JPanel(new BorderLayout());
        testLabel=new JLabel("TESTIING");
    }
}

/*GlassGamePane.java*/

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GlassGamePane extends JPanel implements MouseListener {

    private JPanel statusPanel;
    private JLabel healthLabel;

    public GlassGamePane() {
        createGlassPane();
    }

    private void createGlassPane() {
        setLayout(new BorderLayout());
        createComponents();
        statusPanel.add(healthLabel);
        add(statusPanel, BorderLayout.NORTH);
        addMouseListener(this);
    }

    private void createComponents() {
        statusPanel = new JPanel(new GridLayout(2, 6));
        healthLabel = new JLabel("Player Health:");
        healthLabel.setForeground(Color.RED);
        healthLabel.setBackground(Color.BLUE);

    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.red);
        //Draw an oval in the panel  
        g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        Toolkit.getDefaultToolkit().beep();
    }

    @Override
    public void mousePressed(MouseEvent me) {
        Toolkit.getDefaultToolkit().beep();
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }
}

谢谢你。

3 个答案:

答案 0 :(得分:3)

在使GlassPane可见之前,您似乎必须首先将自定义GlassPane添加为您的GlassPane框架!这里的代码似乎有效:

    //setting glassPane 
    m_glassPane = new GlassGamePane();
    setGlassPane(m_glassPane);
    //set opaque to false, i.e. make transparent 
    m_glassPane.setOpaque(false);
    m_glassPane.setVisible(true);

答案 1 :(得分:3)

  • 看起来像Glasspane是正确创建的,但Glasspane只覆盖轻量级JComponent,否则就是落后,隐藏为重量级Swing或AWT J / Component

  • Glasspane尚未实现任何LayoutManager,您必须设置正确的LayoutManager

  • 用于测试非透明JLabel的背景

  • 我喜欢Glasspane,但您可以使用基于JXLayer(Java6)或OverlayLayout的JLayer(Java7),这是最简单的方式

答案 2 :(得分:1)

来自JavaDoc:http://docs.oracle.com/javase/7/docs/api/javax/swing/JRootPane.html#setGlassPane(java.awt.Component

在添加JRootPane后,您需要将glasspane设置为可见。