1

親JFrame以外のレイアウトのJPanelを使用できるかどうか知りたいです。例えば。ボーダーレイアウトのJFrameがあり、JPanelが埋め込まれていて、レイアウトが異なる場合。出来ますか ?

私はそれをやろうとしています。しかし、このように、そのJPanelのコンポーネントは表示されていません。

ここに問題の詳細があります:

私はJFrameを持っていて、そのレイアウトはBorderLayoutです。このフレームにJPanelを追加しています。JPanelのレイアウトを設定しない場合。JPanelのすべてのコンポーネントがウィンドウに表示されていますが、JPanelのグリッドレイアウトを設定すると、JPanelのコンポーネントが表示されません。コンポーネントを揃えるために、JPanelにレイアウトを追加しています。以下は私のコードです:

メインクラス、フレームクラス、Jpanelクラスがあります。

    public class AppMain {

    public static void main(String[] args) {

        AppPage1 page1 = new AppPage1("test");
        page1.setVisible(true);
    }
}



public class AppPage1 extends JFrame {

    public AppPage1(String title) throws HeadlessException {

        super(title);
        this.setLayout(new BorderLayout());

        addWindowListener(new WindowAdapter() {

            public void windowOpened(WindowEvent e) {
                setExtendedState(MAXIMIZED_BOTH);
            }
        });

        //Panel for logo
        JLabel testLogo = new JLabel("");
        testLogo.setIcon(new javax.swing.ImageIcon("test.JPG"));
        List<JComponent> componentList = new ArrayList<JComponent>();
        componentList.add(testLogo);


        PagePanel logoPanel = new PagePanel(componentList,null);
        this.add(logoPanel, BorderLayout.NORTH);


        //Panel for Button and checkboxes
        JLabel panelTitle = new JLabel("test Wizard");
        JRadioButton rdButton_ExistingConfigurationFile = new JRadioButton("Existing Configuration File");
        JRadioButton rdButton_ConfigureNewPropertyFile = new JRadioButton("Configure new Property File");

        componentList = new ArrayList<JComponent>();
        componentList.add(panelTitle);
        componentList.add(rdButton_ExistingConfigurationFile);
        componentList.add(rdButton_ConfigureNewPropertyFile);

        PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));
        this.add(buttonPanel, BorderLayout.CENTER);

        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }
}



    public class PagePanel extends JPanel {

    public PagePanel(List<JComponent> componentList, LayoutManager layOutManager) {

        this.setBackground(Color.decode("#4E6987"));

        if (layOutManager != null) {
            this.setLayout(null);
        }
        for (JComponent jComponent : componentList) {

            jComponent.setBackground(Color.decode("#4E6987"));
            this.add(jComponent);
        }
    }
}

事前に感謝しますRaviKumar

4

2 に答える 2

0

これが完全に可能であることを示す例を書くこともできますが、SOにはすでに優れた「ネストされたレイアウト」の例があるため、リンクで十分です。

于 2012-06-19T05:45:39.477 に答える
0

PagePanel問題は、クラスにレイアウトマネージャーを設定する代わりに、ヌルのレイアウトマネージャーを設定することです。

if (layOutManager != null) {
    this.setLayout(null);
}

レイアウトを設定する場合はnull、コンポーネントを「手動で」配置およびサイズ設定する必要があるため、可能な限りそれを避け、LayoutManagerを適切に使用するようにしてください。

この行も意味がありません。

PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));

GroupLayoutコンストラクターの引数は、ランダムコンポーネントではなく、レイアウトを設定するコンテナーである必要があります。

于 2012-06-19T06:28:58.680 に答える