java - 在 NullLayoutManager 中将 JComponent 放在图形前面?

java - Put JComponent in front of graphics in NullLayoutManager?

我在具有图形的 JPanel 中有一个 JButton,但该按钮不会显示,因为它位于图形下方的层中。 我已经读过这个:Put JLabel in front of Graphics 2D Rectangle in JPanel

但答案告诉我不要使用 NullLayoutManager。 有什么方法可以使用 NullLayoutManager 来完成它,因为我需要在我的 JPanel 中专门定位我的 JButton? 如果这不可能,是否有任何其他方法可以将 JComponent 定位在 x、y 位置?我也用谷歌搜索了一下,NullLayoutManager 是万维网给我的。

代码:

    JPanel p = new JPanel(){

        @Override
        public void paintComponent(Graphics gr){
            Graphics2D g = (Graphics2D) gr;
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 800, 800);
            g.setFont(titlefont);
            g.setColor(Color.WHITE);
            g.drawString("dont steal my game idea plz", 25, 100);
            g.drawImage(bi, 138, 70, null);
            repaint();
        }

    };
    p.setLayout(null);





    JButton b = new JButton("PLAY");
    b.setLocation(100, 200);
    b.setFont(ufont);




    f.add(p);
    p.add(b);

Is there any way to do it with the NullLayoutManager, because I need to specifically position my JButton in my JPanel?

答案是"yes",但是您了解布局管理器的实际作用吗?他们如何工作以及他们扮演的角色是为了取代他们并接管他们的功能要求?

null 布局只是个坏主意,它们有很多问题,光是想一想就让人头皮发麻。如果 none 的库存布局管理器做你想做的,也许考虑使用 MigLayout 或其他布局管理器,甚至可能编写你自己的,至少这样,你仍然可以在 API,它的设计旨在解决布局管理器问题。

还有几个其他问题,首先,您在绘制文本之后绘制图像,如果两者重叠,这可能会导致一些问题。其次,您通过不调用 super.paintComponent 来破坏绘制链,这可能会导致一些意外和不需要的结果。第三,不用填充组件,使用setBackgroundsuper.paintComponent处理。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DownWithNullLayouts {

    public static void main(String[] args) {
        new DownWithNullLayouts();
    }

    public DownWithNullLayouts() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Font titlefont;
        private BufferedImage bi;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(30, 0, 0, 0);
            add(new JButton("Play"), gbc);
            try {
                bi = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            titlefont = UIManager.getFont("Label.font");
            setBackground(Color.BLACK);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - bi.getWidth()) / 2;
            int y = (getHeight()- bi.getHeight()) / 2;
            g2d.drawImage(bi, x, y, this);
            g2d.setFont(titlefont);
            g2d.setColor(Color.WHITE);
            g2d.drawString("dont steal my game idea plz", 25, 100);
            g2d.dispose();
        }

    }

}

仔细查看 Painting in AWT and Swing and Performing Custom Painting 了解更多详情