2

私は Swing の初心者で、添付したタイプの画面を設計したいと考えています。大きな右側のパネルは、さまざまなボタンのクリックで表示されるカード レイアウトを使用します。6 つの列があり、コンポーネントを追加すると行の数が増えるなど、コンポーネントを配置する方法がわかりません。どのレイアウトを使用する必要があるか、またはどのように行う必要があるかの疑似コードを誰かが教えてくれれば幸いです。

トンありがとう!!!

ここに画像の説明を入力

更新: MigLayout を使用するために、それぞれのソリューションを移動しました。非常に簡単で、動的コンポーネント配置の場合に非常に使いやすいです。貴重な時間と回答をありがとうございました。

4

2 に答える 2

5

GridLayoutはこれに最適です: GridLayout(int rows,int cols). の値は、0コンポーネントを追加すると行/列が大きくなるように指定します。

Oracle からの短いサンプル:

ここに画像の説明を入力

GridLayout experimentLayout = new GridLayout(0,2);//create grid any amount of rows and 2 coloumns

...

compsToExperiment.setLayout(experimentLayout);//add gridlayout to Component/JPanel

compsToExperiment.add(new JButton("Button 1"));
compsToExperiment.add(new JButton("Button 2"));
compsToExperiment.add(new JButton("Button 3"));
compsToExperiment.add(new JButton("Long-Named Button 4"));
compsToExperiment.add(new JButton("5"));

アップデート:

ただし、より柔軟なグリッド レイアウトが必要な場合はGridBagLayout、Guillaume Polet の提案を参照してください。

ここに画像の説明を入力

ご覧のとおり、コンポーネントごとに複数の行/列を使用できます。

ここに画像の説明を入力

JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
}




button = new JButton("Button 1");
if (shouldWeightX) {
                   c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);




button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);




button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 2;   //2 columns wide
c.gridy = 2;       //third row
pane.add(button, c);
于 2012-09-24T17:09:39.637 に答える
2

GroupLayoutまたはGridBagLayoutのいずれかを見てください。前者はおそらく後者よりも扱いやすいでしょう。

于 2012-09-24T17:22:14.673 に答える