簡體   English   中英

如何從不同的類向容器添加JPanel?

[英]How do I add a JPanel to container from a different class?

基本上,我有一個充當容器的類。 這是一個JFrame ,具有一個采用JPanel的構造函數。 現在,我在容器類之外構造了各種JPanels。 我出於某些特定原因設計了這種方法,主要是與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布局。 但是,如果有人知道使用null布局的方法,請分享。 更清楚一點,如果我將其全部作為容器類中的方法使用,那么所有這些代碼都可以使用,但是我不希望這樣。 任何想法將不勝感激。

問題出在您的testFrame類上。 擴展了 JPanel並且在其中使用了另一個不必要的JPanel 如果堅持使用,則testFrame使用語句add(testPanel);將其添加到add(testPanel); testFrame的構造函數的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]);

同意Ravindra的觀點,直接使用JFrame.getContentPane()是最佳實踐。 您可能應該做的另一件事是給ContentPane一個明確的布局(類似於您對JPanel所做的事情)。 就像是:

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

您可以選擇所需的任何布局,但是我經常為我的主框架選擇BorderLayout,因為CENTER區域是“貪婪的”(無論放置在哪里,都會自動嘗試填充該空間),而這通常是理想的行為。

其他可能有幫助的事情包括使用GridBagConstraints中的fill屬性來通知它嘗試填充整個面板區域,並覆蓋Panel的getPreferredSize()來為布局提供尺寸提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM