简体   繁体   中英

GlassPane is not showing up on JFrame

I have been trying to implement a GlassPane for my games' in-game HUD, but right now I cant seem to get the JFrame to set my GlassPane as its own i;ve used setGlassPane() and Ive been reading up a few examples trying to find my mistake, but nothing. So I wrote a SSCCE that demonstrates my problem. I have a JFrame to which I add a Jpanel with a label "TESTIING" then I initiate my galss pane and call setGlassPane() on my frames instance. My GlassPane has a MouseListener , a JPanel and 2 JLabels , and an overriden paint() however the MouseListener wont work the paint() wont show and my labels are not there (so basically my GlassPane is not being set as my frames new 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) {
    }
}

Thanks you.

It seems that you have to first add the custom GlassPane as your frames GlassPane before making the GlassPane visible! This code here seemed to work:

    //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);

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

You need to set the glasspane to visible after adding it the JRootPane.

  • seems like as Glasspane is correcty created, but Glasspane to overlay only lightweight JComponent otherwise is behind, hidden be heavyweight Swing or AWT J/Component

  • Glasspane has not implemented any LayoutManager, you have to set proper LayoutManager

  • for testing put non opaque JLabel with some Background

  • I m love Glasspane, but you can to use JLayer (Java7) based on JXLayer (Java6) or OverlayLayout, as easiest of ways

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM