【问题标题】:Can't see child JPanel in JPanel in JFrame在 JFrame 中的 JPanel 中看不到子 JPanel
【发布时间】:2016-08-21 20:32:38
【问题描述】:

我创建了一个JFrame。 在这个JFrame 中,我创建了一个JPanel。 在这个JPanel 中,我创建了另一个JPanel(我们称之为“A”)。

我在“A”中绘制了一个矩形,并希望使用图形创建按钮。 我的 gui 中没有矩形。我可以看到“A”中的paintComponent() 方法没有被调用。

代码: JPanels:(子 JPanel 是内部类)

public class MemoryPanel extends JPanel {
    
    public MemoryPanel(){
        setPreferredSize(new Dimension(350,448));
    }
    
    @Override 
    public void paintComponent(Graphics g) {    
        //POSITIONING
        setLayout(new BorderLayout());
        
        //CREATE MEMORY BUTTONS
        MemButton a=new MemButton();
        
        //Drawing Rectangles for Memory
        add(a,BorderLayout.CENTER);

    } 
    

    
    private class MemoryButton extends JPanel{
        public MemoryButton(){
            setLayout(null);
            setPreferredSize(new Dimension(87,40));
        }
        
        @Override
        public void paintComponent(Graphics g){
            Graphics2D td= (Graphics2D)g;
            td.drawRect(0, 0, 20, 20);
        }
    }
}

【问题讨论】:

  • 1) paintComponent 方法应该用于绘制图形,而不是用于创建和添加组件。在构造函数中执行此操作。 2) 如果您将按钮添加到BorderLayout.CENTERsetPreferredSize 不会按照您的预期进行。该按钮将占用整个可用空间。 3) 不要忘记在paintComponent 方法的开头调用super.paintComponent()。另外,MemButtonMemoryButton 一样吗? - 请考虑发布重现问题的minimal reproducible example

标签: java swing jframe jpanel awt


【解决方案1】:

您应该首先对 JButton 进行编程,以使您的图形能够作为按钮工作。我相信这篇文章会帮助你:

Creating a custom button in Java

如果您想要一个矩形作为按钮的背景,您可以在主面板中绘制它并在其上添加按钮。尝试使用不同的布局来维持一些秩序。

【讨论】:

    【解决方案2】:

    我制作了一个简单的 GUI 来测试您的代码,矩形显示正确。 我没有对您发布的代码进行任何相关更改。

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class SimpleJFrameProgram extends JFrame {
         private static final long serialVersionUID = 1L;
    
         public SimpleJFrameProgram() {
             super("TEST");
    
             initComponents();
    
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             this.pack();
             this.setLocationRelativeTo(null);
             this.setVisible(true);
         }
    
    
        private void initComponents() {
            MemoryPanel memoryPanel = new MemoryPanel();
    
            this.add(memoryPanel);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        new SimpleJFrameProgram();
                    } catch (Exception e) {
                       e.printStackTrace();
                    }
                }
           });
        }
     }
    

    我已对您的 MemoryPanel 进行了细微更改:将 MemButton 替换为您的 MemoryButton 并将矩形填充为红色以提高其对测试的可见性。如果没有最后的更改,矩形也会出现。

      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
    
      import javax.swing.JPanel;
    
      public class MemoryPanel extends JPanel {
    
        public MemoryPanel(){
             setPreferredSize(new Dimension(350,448));
        }
    
        @Override
        public void paintComponent(Graphics g) {
            // POSITIONING
            setLayout(new BorderLayout());
    
            // CREATE MEMORY BUTTONS
            MemoryButton a = new MemoryButton();
    
            // Drawing Rectangles for Memory
            add(a,BorderLayout.CENTER);
    
        }
    
        private class MemoryButton extends JPanel{
            public MemoryButton(){
                setLayout(null);
                setPreferredSize(new Dimension(87,40));
            }
    
            @Override
            public void paintComponent(Graphics g) {
    
                Graphics2D td = (Graphics2D) g;
                td.setColor(Color.red);
                td.fillRect(0, 0, 20, 20);
            }
        }
     }
    

    这是得到的结果:

    也许您的问题在于初始化父 JFrame

    【讨论】:

    • 谢谢,我已经找到问题了。我在同一个项目中有另一个包,名称为“MemButton 和从那里初始化的对象”。
    【解决方案3】:

    更改 MemoryButton 的类名修复了它。

    我有另一个具有相同类名的包。

    【讨论】:

      猜你喜欢
      • 2011-09-22
      • 2011-05-24
      • 2015-04-15
      • 1970-01-01
      • 2021-01-29
      • 2011-11-30
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多