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);
    }
}

nullレイアウトを使用しないように何度も言われたため、GridBagLayoutを使用しています。ただし、null レイアウトを使用して回避する方法を誰かが知っている場合は、共有してください。もう少し明確にするために、コンテナクラス内のメソッドとしてすべてを持っていれば、このコードはすべて機能しますが、そのようにしたくありません。どんなアイデアでも大歓迎です。

4

2 に答える 2

1

問題はあなたのtestFrameクラスにあります。それは伸び JPanel、その中にあなたは必要のない別のJPanelものを取りました。どうしても取る場合は、のコンストラクターの最後にあるtestFrameusing the ステートメントに追加する必要があります。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

JFrame.getContentPane() を直接使用することがベスト プラクティスであるという Ravindra の意見に同意します。おそらく行うべきもう 1 つのことは、その ContentPane に明示的なレイアウトを与えることです (JPanel で行ったことと同様です)。何かのようなもの:

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

任意のレイアウトを選択できますが、私はメイン フレームに BorderLayout を選択することがよくあります。これは、CENTER 領域が「貪欲」であるため (そこに配置したものは自動的にスペースを埋めようとするため)、多くの場合、それが望ましい動作です。

fillGridBagConstraints のプロパティを使用して、パネル領域全体を埋めるように通知したり、Panel をオーバーライドgetPreferredSize()してレイアウトにサイズのヒントを与えたりすることが役立つ可能性があります。

于 2013-03-16T05:34:36.863 に答える