【问题标题】:paintComponent not visible java油漆组件不可见java
【发布时间】:2017-04-06 01:11:51
【问题描述】:

我想将它添加到另一个 JPanel 中,但它在那里不可见。我的另一个Jpanel 叫做bottomPanel。 paintComponent 应该显示在底部面板中

 bottomPanel.setLayout(null);  
 TestPane tp = new TestPane();
 bottomPanel.add(tp);

我已经扩展了 Jpanel。

  public class TestPane extends JPanel {
  @Override
   public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        int width = getWidth() - 100;
        int height = getHeight() - 100;
        int x = (getWidth() - width) / 2;
        int y = (getHeight() - height) / 2;
        g2d.setColor(Color.RED);
        g2d.drawRect(x, y, width, height);
        g2d.dispose();
    }

}

【问题讨论】:

  • 如何将面板添加到父容器中?这是如何在屏幕上显示的?
  • bottomPanel 是另一个我希望在其中显示的面板。不是 bottomPanel.add(new TestPane());够了吗?抱歉,我是 java 新手
  • 我们不需要屏幕截图,我们需要重现您的问题的代码。
  • 我已经添加了完整的代码
  • bottomPanel.setLayout(null); ... 说得够多了

标签: java swing jpanel paintcomponent


【解决方案1】:

问题开始于:

bottomPanel.setLayout(null);

Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。

并且在未来,发布一个 MCVE,而不是超过 300 行的代码,其中包含不相关的附加内容,例如文件 I/O、表格、行排序器等。

【讨论】:

    【解决方案2】:

    为我工作...

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    JPanel outer = new JPanel(new BorderLayout());
                    outer.add(new TestPane());
                    frame.add(outer);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                int width = getWidth() - 100;
                int height = getHeight() - 100;
                int x = (getWidth() - width) / 2;
                int y = (getHeight() - height) / 2;
                g2d.setColor(Color.RED);
                g2d.drawRect(x, y, width, height);
                g2d.dispose();
            }
    
        }
    
    }
    

    考虑提供一个runnable example 来证明您的问题

    【讨论】:

    • @hello12345678 它对我来说很好,告诉我它不工作 - 它甚至嵌入到另一个面板中并且仍然有效
    • 嘿,我在问题描述中添加了图片
    • runnable example 没有可靠的答案 - 其他一切都是猜测
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多