サンプル
レイアウト
0 1 2 3 +------------------+ 0 | | +------------------+ +---++---++---++---+ 1 | 1 || 2 || 3 || + | +---++---++---++---+ +---++---++---++---+ 2 | 4 || 5 || 6 || - | +---++---++---++---+ +---++---++---++---+ 3 | 7 || 8 || 9 || * | +---++---++---++---+ +---++---++---++---+ 4 | 0 || C || / | +---++---++---++---+
GridBagLayoutDemo.java
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class GridBagLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("This is a title"); Container container = frame.getContentPane(); GridBagLayout gridBagLayout = new GridBagLayout(); container.setLayout(gridBagLayout); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; // [Result] gridBagConstraints.gridwidth = 4; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; JTextField resultTextField = new JTextField(""); gridBagLayout.setConstraints(resultTextField, gridBagConstraints); container.add(resultTextField); // [1] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; JButton button1 = new JButton("1"); gridBagLayout.setConstraints(button1, gridBagConstraints); container.add(button1); // [2] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; JButton button2 = new JButton("2"); gridBagLayout.setConstraints(button2, gridBagConstraints); container.add(button2); // [3] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; JButton button3 = new JButton("3"); gridBagLayout.setConstraints(button3, gridBagConstraints); container.add(button3); // [+] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 1; JButton buttonPlus = new JButton("+"); gridBagLayout.setConstraints(buttonPlus, gridBagConstraints); container.add(buttonPlus); // [4] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; JButton button4 = new JButton("4"); gridBagLayout.setConstraints(button4, gridBagConstraints); container.add(button4); // [5] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; JButton button5 = new JButton("5"); gridBagLayout.setConstraints(button5, gridBagConstraints); container.add(button5); // [6] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 2; JButton button6 = new JButton("6"); gridBagLayout.setConstraints(button6, gridBagConstraints); container.add(button6); // [-] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 2; JButton buttonMinus = new JButton("-"); gridBagLayout.setConstraints(buttonMinus, gridBagConstraints); container.add(buttonMinus); // [7] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; JButton button7 = new JButton("7"); gridBagLayout.setConstraints(button7, gridBagConstraints); container.add(button7); // [8] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; JButton button8 = new JButton("8"); gridBagLayout.setConstraints(button8, gridBagConstraints); container.add(button8); // [9] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 3; JButton button9 = new JButton("9"); gridBagLayout.setConstraints(button9, gridBagConstraints); container.add(button9); // [*] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 3; JButton buttonMultiplication = new JButton("*"); gridBagLayout.setConstraints(buttonMultiplication, gridBagConstraints); container.add(buttonMultiplication); // [0] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; JButton button0 = new JButton("0"); gridBagLayout.setConstraints(button0, gridBagConstraints); container.add(button0); // [C] gridBagConstraints.gridwidth = 2; gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; JButton buttonClear = new JButton("C"); gridBagLayout.setConstraints(buttonClear, gridBagConstraints); container.add(buttonClear); // [/] gridBagConstraints.gridwidth = 1; gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 4; JButton buttonDivide = new JButton("/"); gridBagLayout.setConstraints(buttonDivide, gridBagConstraints); container.add(buttonDivide); frame.pack(); frame.setVisible(true); } }
GridBagConstraints
gridx : リーディングエッジを含むセルを指定 gridy : 上部にセルを指定 gridwidth : 1 行のセル数を指定 gridheight : 1 列のセル数を指定 weightx : 余分の水平スペースを分配する方法を指定 weighty : 垂直の余分なスペースを分配する方法を指定 anchor : このフィールドはコンポーネントがその表示領域より小さい場合に使用 fill : 表示領域がコンポーネントの必須サイズよりも大きいときに使用 insets : 表示領域の端までの領域の最小値を指定 ipadx : 最小幅に追加する領域の値を指定 ipady : 最小の高さに追加する領域の値を指定
参考文献
http://www.javadrive.jp/tutorial/gridbaglayout/index3.htmlhttp://a4dosanddos.hatenablog.com/entry/2015/10/29/015240
http://www.techscore.com/tech/Java/JavaSE/UI/5-2/
http://typea.info/tips/wiki.cgi?page=Swing+%A5%EC%A5%A4%A5%A2%A5%A6%A5%C8+2#p2
http://www.javadrive.jp/tutorial/gridbaglayout/
http://www.javadrive.jp/tutorial/gridbaglayout/index2.html