私はGridBagLayout
を使用してインターフェイスを設計しています。インターフェイスは、JTabbedPane
を北に設定し、サイズを変更すると両方向に塗りつぶします。JTabbedPane
のすぐ下に2つのJButton
があります。追加のJPanelをコンテナとして使用しなくても、同じGridBagLayoutインターフェイスを構築できますか?
私は(余分JPanel
を導入することなく)のみGridBagLayout
機能を使用して東でこれらの2つのボタンが入れている達成したいが、私はそうしませんでした。
| - [tab] -------------------------- |
| --------------------------------- |
| --------------------------------- |
| --------------------------------- |
| --------------------------------- |
| ------------- [button] [button] |
両方のボタンのレイアウト制約をWEST(EASTではなく)に設定したとき。両方とも正しく西に行く!
c.anchor = GridBagConstraints.WEST;
しかし、私は東
c.anchor = GridBagConstraints.EAST;
一つは東に行くと、他の西に行きます!
この問題を解決するために、JPanel
を追加し、両方のボタンを保持し、このJPanel
をインターフェイスに追加し、そのレイアウト制約をc.anchor = GridBagConstraints.EAST;
に設定しました。それはうまくいく。
JPanel
をコンテナに使用せずに同じGridBagLayout
インターフェイスを構築することはできますか?
SSCCEフォーマットには、次の2つのクラスがあります。
import javax.swing.*;
import java.awt.*;
public class GridBagTest extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.gridwidth = 2;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_next, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest()::initGUI);
}
}
ソリューションクラス:
import javax.swing.*;
import java.awt.*;
public class GridBagTest_solved extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JPanel panel_buttons;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
panel_buttons = new JPanel(new GridBagLayout());
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
// Adding buttons to panel_buttons
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_next, c);
// Adding tabbedPane & panel_buttons to this (GridBagTest_solved)
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
this.add(panel_buttons, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest_solved");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest_solved()::initGUI);
}
}
最初のものは問題
GridBagTest
、および他のショーソリューション余分
JPanel
問題のクラスを利用するGridBagTest_solved
を表示します
コードは[SSCCE](http://sscce.org/)に準拠するように単純化されています。私が問題を解決したときに、 'GridBagLayout'機能のみを使用するソリューションがあるかどうかを知りたいとします。 –