我一直在尝试为游戏的游戏内HUD实现GlassPane
,但是现在我似乎无法让JFrame
将我的GlassPane
设置为自己;我曾经使用setGlassPane()
阅读了一些示例来尝试发现我的错误,但是什么也没有。所以我写了一个SSCCE来证明我的问题。我有一个JFrame,向其中添加了带有标签“ TESTIING”的Jpanel,然后启动galss窗格并在我的框架实例上调用setGlassPane()。我的GlassPane有一个MouseListener
,一个JPanel
和2个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) {
}
}
谢谢。
最佳答案
看来您必须首先将自定义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);