using内GridBagLayout
で10(3x3 + 1)を使用しようとしています。JButtons
JPanel
BoxLayout
しかし、私がグルーボックスや同様に何をするにしても、GridBagLayout
JPanel
はの余分なスペースをすべて占有しますBoxLayout
。私はおそらく何かが足りないのですか、それともこれは不可能ですか?
私が使用した解決策の1つは、gridbaglayout内の拡張要素を使用してボタンを押し上げることです。これにより、ボタンは適切な場所に配置されますが、境界ボックスは大きく見えます。
これが私のサンプルコードです:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GridBagLayoutTest extends JFrame {
public GridBagLayoutTest(){
super();
this.setTitle("JVectorView");
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(new JLabel("Hello!"));
content.add(new Controls());
content.add(Box.createGlue());
this.setVisible(true);
}
private class Controls extends JPanel{
private static final int WIDTH = 3, HEIGHT = 3;
public Controls(){
GridBagConstraints constraints = new GridBagConstraints();
//this.setBorder(BorderFactory.createLineBorder(Color.red));
this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
constraints.fill = GridBagConstraints.NONE;
this.setLayout(new GridBagLayout());
for(int row = 0; row < HEIGHT; row++){
for(int col = 0; col < WIDTH; col++){
constraints.gridx = col;
constraints.gridy = row;
this.add(new JButton("B"+(col+row*WIDTH)), constraints);
}
}
constraints.gridx = 1;
constraints.gridy = 3;
this.add(new JButton("B"+(10)), constraints);
}
}
public static void main(String[] args) {
new GridBagLayoutTest();
}
}
ボタンの周りの境界線をきつくしたいのですが。gridbaglayoutをそのコンテンツに折りたたむことは可能ですか、それとも常にパネルを強制的に埋めますか?