1

基本上我有一个充当容器的类。它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 是因为我多次被告知不要使用空布局。但是,如果有人知道使用空布局的方法,请分享。为了更清楚一点,如果我将所有这些代码都作为容器类中的一个方法,那么所有这些代码都可以工作,但我不希望这样。任何想法将不胜感激。

4

2 回答 2

1

问题出在你的testFrame课上。它延伸 JPanel并在里面你拿了另一个JPanel没有必要的东西。如果非要取,就得把它加到构造函数末尾的testFrameusing语句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]);

于 2013-03-16T05:19:38.883 回答
0

同意 Ravindra,直接使用 JFrame.getContentPane() 是最佳实践。您可能应该做的另一件事是给 ContentPane 一个明确的布局(类似于您对 JPanel 所做的)。就像是:

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

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

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

于 2013-03-16T05:34:36.863 回答