如何从其他类添加JPanel到容器?

时间:2013-03-16 05:08:49

标签: java swing jframe jpanel

基本上我有一个充当容器的类。这是一个JFrame,它有一个带JPanel的构造函数。现在,我在容器类之外构建了各种JPanel。我出于特定的原因设计了这种方式,主要是与MVC设计模式有关。

我遇到的问题是每当我将JPanel添加到容器时,从容器类中它显示为空白。没有编译错误。我不明白为什么它不会添加我要求它。我会发布一些代码,这是容器:

public class MainFrame {

    private JFrame mainContainer = new JFrame("Frog Checkers");

    private JPanel frame = new testFrame();

    public void start() {
        mainContainer(frame);
    }

    private JFrame mainContainer(JPanel frame) {
        mainContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainContainer.setSize(925, 608);
        mainContainer.setVisible(true);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        mainContainer.setLocation(dim.width / 2 - mainContainer.getSize().width
            / 2, dim.height / 2 - mainContainer.getSize().height / 2);
        mainContainer.setResizable(false);

        mainContainer.add(frame);

        return mainContainer;
    }
}

这是我试图添加的一个JPanel的示例:

public class testFrame extends JPanel {
    private JPanel testPanel = new JPanel()
    private JButton testButton, anotherButton;

    public testFrame() {
        testPanel.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        testButton = new JButton("New Button");
        constraints.gridx = 0; // Location on grid
        constraints.gridy = 3; // Location on grid
        constraints.gridwidth = 2; // How many grids the width will consume
        constraints.gridheight = 1; // How many grids the length will consume
        constraints.weightx = 1.0; // for resizing
        constraints.weighty = 1.0; // for resizing
        constraints.anchor = GridBagConstraints.SOUTHWEST; // Where it will be anchored
        constraints.ipadx = 20; // Internal padding
        constraints.ipady = 20; // Inernal padding
        constraints.insets = new Insets(50,50,50,50);
        testPanel.add(testButton, constraints);

        anotherButton = new JButton("another Button");
        constraints.gridx = 0; // Location on grid
        constraints.gridy = 3; // Location on grid
        constraints.gridwidth = 2; // How many grids the width will consume
        constraints.gridheight = 1; // How many grids the length will consume
        constraints.weightx = 1.0; // for resizing
        constraints.weighty = 1.0; // for resizing
        constraints.anchor = GridBagConstraints.SOUTHWEST; // Where it will be anchored
        constraints.ipadx = 20; // Internal padding
        constraints.ipady = 20; // Inernal padding
        constraints.insets = new Insets(50, 50, 120, 80);
        testPanel.add(anotherButton, constraints);
    }
}

我正在使用GridBagLayout,因为我被告知多次不使用null布局。但如果有人知道如何使用空布局,请分享。更清楚一点,所有这些代码都可以工作,如果我把它作为容器类中的一个方法,但我不希望这样。任何想法都将不胜感激。

2 个答案:

答案 0 :(得分:1)

问题在于您的testFrame课程。它扩展 JPanel并且在其中你又拿了另一个JPanel,这是不必要的。如果您坚持要采取,则必须使用testFrame构造函数末尾的语句add(testPanel);将其添加到testFrame

班级名称应以大写字母开头。我建议您将班级重命名为TestFrame,而不是testFrame

public class testFrame extends JPanel {
    private JButton testButton, anotherButton;

    public testFrame() {
        setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        testButton = new JButton("New Button");
        constraints.gridx = 0; // Location on grid
        constraints.gridy = 3; // Location on grid
        constraints.gridwidth = 2; // How many grids the width will consume
        constraints.gridheight = 1; // How many grids the length will consume
        constraints.weightx = 1.0; // for resizing
        constraints.weighty = 1.0; // for resizing
        constraints.anchor = GridBagConstraints.SOUTHWEST; // Where it will be anchored
        constraints.ipadx = 20; // Internal padding
        constraints.ipady = 20; // Inernal padding
        constraints.insets = new Insets(50,50,50,50);
        add(testButton, constraints);

        anotherButton = new JButton("another Button");
        constraints.gridx = 0; // Location on grid
        constraints.gridy = 3; // Location on grid
        constraints.gridwidth = 2; // How many grids the width will consume
        constraints.gridheight = 1; // How many grids the length will consume
        constraints.weightx = 1.0; // for resizing
        constraints.weighty = 1.0; // for resizing
        constraints.anchor = GridBagConstraints.SOUTHWEST; // Where it will be anchored
        constraints.ipadx = 20; // Internal padding
        constraints.ipady = 20; // Inernal padding
        constraints.insets = new Insets(50, 50, 120, 80);
        add(anotherButton, constraints);
    }
}

MainFrame中,使用

getContentPane().add([testFrame object]);

答案 1 :(得分:0)

同意Ravindra直接使用JFrame.getContentPane()是最佳做法。您可能应该做的另一件事是为ContentPane提供一个显式布局(类似于您对JPanel所做的操作)。类似的东西:

mainContainer.getContentPane().setLayout(new BorderLayout());
mainContainer.getContentPane().add(yourPanel, BorderLayout.CENTER);

你可以选择你想要的任何布局,但我经常为我的主框架选择BorderLayout,因为CENTER区域是“贪婪的”(无论你放在那里会自动尝试填充空间),这通常是所希望的行为。

其他可能有帮助的事情包括使用GridBagConstraints中的fill属性来通知它尝试填充整个面板区域,并覆盖Panel的getPreferredSize()以给您的布局提供一个大小提示。