【问题标题】:java - Put JComponent in front of graphics in NullLayoutManager?java - 将JComponent 放在NullLayoutManager 中的图形前面?
【发布时间】:2016-04-28 12:46:05
【问题描述】:

我在具有图形的 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);

【问题讨论】:

  • 每次我在paintComponent 内部看到repaint() 并使用null 布局我都会有点恶心,就像黑板上的钉子一样——你为什么要这样做?谁在教你和许多其他新手这些坏习惯?请阅读教程以了解如何正确执行此操作。并阅读本网站上 Swing 专家对类似问题的回答,以了解如何正确地做事。这一切都被问过很多次了。
  • “但按钮不会显示在图形下方的图层中” - 不,不是,paintComponentpaintChildren 之前被调用,它会绘制组件。您通过不调用super.paintComponent“可能”来打破油漆链的事实与它有关
  • But the answers tell me not to use the NullLayoutManager - 那你为什么不试着听听答案???为什么你认为我们会因为你再次问同样的问题而改变答案??? because I need to specifically position my JButton in my JPanel? - 所以使用布局管理器来定位按钮。顺便说一句,您也没有理由绘制文本。您只需使用 JLabel。因此,布局管理器将能够轻松处理标签和按钮。

标签: java swing graphics paintcomponent


【解决方案1】:

有什么办法可以用 NullLayoutManager 来做,因为我需要在我的 JPanel 中专门定位我的 JButton?

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

null 布局只是一个坏主意,有很多事情可能会导致它们出错,只是想一想就会麻木。如果没有任何库存布局管理器可以满足您的需求,则可以考虑使用 MigLayout 或其他一些布局管理器,甚至可能编写自己的布局管理器,至少这样,您仍然可以在 API 中工作,该 API 旨在工作围绕布局管理器。

还有其他几个问题,首先,您是在绘制文本之后绘制图像,如果两者重叠,这可能会导致一些问题。其次,您通过不调用super.paintComponent 来破坏油漆链,这可能会导致一些意外和不需要的结果。三、不需要填充组件,使用setBackground,让super.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 SwingPerforming Custom Painting 了解更多详情

【讨论】:

  • 所以不可能?
  • 要做到这一点,您需要非常了解布局管理器 API 的角色,因为您需要替换该功能。我们不鼓励使用空布局的原因通常是因为不了解它们在做什么。我个人的建议是编写一个自定义布局管理器,以实现您正在寻找的结果
  • 这将是一个开始
  • 由于某种原因,上面的代码似乎让我的JPanel的背景消失了。
猜你喜欢
  • 2018-02-06
  • 2015-05-04
  • 2018-07-27
  • 2019-11-14
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 2016-04-23
相关资源
最近更新 更多