14

2 つの jPanels を JFrame に並べて追加したいと考えています。2 つのボックスは jpanels で、外側のボックスは jframe ですここに画像の説明を入力

これらのコード行があります。JPanel を拡張する seatinPanel という 1 つのクラスがあり、このクラス内には、コンストラクターと、JPanel オブジェクトを返す utilityButtons という 1 つのメソッドがあります。utilityButtons JPanel を右側に配置します。ここにあるコードは、実行時に utilityButtons JPanel のみを表示します。

public guiCreator()
    {
        setTitle("Passenger Seats");
        //setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();

        seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
        contentPane.add(seatingPanel1); //adding the seats
        contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons

        pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
        setVisible(true);  
    }
4

2 に答える 2

28

私がお勧めする最も柔軟な LayoutManager はBoxLayoutです。

次のことができます。

JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

//panel1.set[Preferred/Maximum/Minimum]Size()

container.add(panel1);
container.add(panel2);

次に、コンテナーからオブジェクトをフレーム コンポーネントに追加します。

于 2011-06-12T23:32:48.750 に答える