简体   繁体   English

自定义 JPanel 未添加到 JFrame

[英]Custom JPanel not being added to JFrame

I am creating a GUI that utilizes a custom JPanel that shows up three times in a row on a JPanel that is then added to the JFrame.我正在创建一个使用自定义 JPanel 的 GUI,该 JPanel 在 JPanel 上连续显示三次,然后将其添加到 JFrame。 For some reason, the custom JPanel does not show up when added to the JFrame .由于某种原因,自定义 JPanel 在添加到JFrame时不会显示。

The only way I managed to get the JPanel to show is when I added the below line to the bottom of the custom JPanel class:我设法让 JPanel 显示的唯一方法是将以下行添加到自定义 JPanel class 的底部:

this.add(myPanel); this.add(myPanel); // myPanel is the JPanel instance // myPanel 是 JPanel 实例

I am sure that is not the correct way我确定这不是正确的方法

class Driver with main method class 驱动带主法

public class Driver {
    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Test");

        JPanel mainPanel = new JPanel(new GridLayout(1, 3));

        MyPanel firstPanel = new MyPanel();
        MyPanel secondPanel = new MyPanel();
        MyPanel thirdPanel = new MyPanel();

        mainPanel.add(firstPanel);
        mainPanel.add(secondPanel);
        mainPanel.add(thirdPanel);

        frame.getContentPane().add(mainPanel);

        // show the window.
        frame.setSize(MyPanel.WIDTH, MyPanel.HEIGHT);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    } // createAndShowGUI

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            } // run
        }); // Runnable
    } // main
} // Driver

Custom JPanel class定制JPanel class

@SuppressWarnings("serial")
public class MyPanel extends JPanel {
     public MyPanel() {
        JPanel myPanel = new JPanel();

        // inner panel
        JPanel innerPanel = new JPanel();

        innerPanel.setBorder(BorderFactory.createEtchedBorder());
        innerPanel.setBackground(Color.red);
        innerPanel.setPreferredSize(new Dimension(WIDTH / 3, HEIGHT - 100));
        myPanel.add(innerPanel);

        JPanel buttonPanel = new JPanel();

        buttonPanel.setBackground(Color.MAGENTA);

        myPanel.add(buttonPanel);
    } // constructor
} // class

Some lines have been edited out有些行已被编辑掉

This class is in error:这个 class 是错误的:

public MyPanel() {
        JPanel myPanel = new JPanel();
        myPanel.setBorder(BorderFactory.createEtchedBorder());
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

        // inner panel
        JPanel innerPanel = new JPanel();

        innerPanel.setBorder(BorderFactory.createEtchedBorder());
        innerPanel.setBackground(Color.red);
        innerPanel.setPreferredSize(new Dimension(WIDTH / 3, HEIGHT - 100));

        myPanel.add(innerPanel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(Color.MAGENTA);

        myPanel.add(buttonPanel);
    } // constructor
} // class

I'm guessing that it extends JPanel, meaning that there are two JPanels being created when this class is created, a myPanel variable which gets components added to it, but which is never added to the GUI, and the MyPanel instance which has nothing added to it, and which does get added to the GUI.我猜它扩展了 JPanel,这意味着在创建此 class 时创建了两个 JPanel,一个 myPanel 变量将组件添加到其中,但从未添加到 GUI 中,而 MyPanel 实例没有添加任何内容到它,并且它确实被添加到 GUI 中。

Suggestion: don't have this class extend JPanel and instead make the myPanel local variable an instance field.建议:不要让这个 class 扩展 JPanel,而是让 myPanel 局部变量成为实例字段。 Give the class a getMyPanel() method that returns this field and add this to the GUI.为 class 提供一个getMyPanel()方法,该方法返回此字段并将其添加到 GUI。

Option 2: get rid of the myPanel local variable and instead add everything to this .选项 2:摆脱myPanel局部变量,而是将所有内容添加到this中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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