1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
/**
* Created by IntelliJ IDEA.
* User: bebe
*/
public class Toto2 extends JPanel {
private JPanel topPanel = new JPanel();
private JPanel eastPanel = new JPanel();
private JPanel westPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JPanel southPanel = new JPanel();
private JButton one = new JButton();
private JButton two = new JButton();
private JButton three = new JButton();
private JButton four = new JButton();
private JButton five = new JButton();
private JButton six = new JButton();
private JButton seven = new JButton();
private JButton eight = new JButton();
private JButton ten = new JButton();
public Toto2() {
initGUI();
}
private void initGUI() {
setLayout(new BorderLayout());
topPanel.setLayout(new GridBagLayout());
eastPanel.setLayout(new GridBagLayout());
westPanel.setLayout(new GridBagLayout());
centerPanel.setLayout(new GridBagLayout());
southPanel.setLayout(new GridBagLayout());
one.setText("1");
two.setText("2");
three.setText("3");
four.setText("4");
five.setText("5");
six.setText("6");
seven.setText("7");
eight.setText("8");
ten.setText("10"); // no nine :-)
// north
topPanel.add(one,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(5, 0, 5, 0), 0, 0));
topPanel.add(two,
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
add(topPanel, BorderLayout.NORTH); // or BorderLayout.PAGE_START
// west
westPanel.add(three,
new GridBagConstraints(0, 0, 1, 1, 1.0, 0.5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
westPanel.add(four,
new GridBagConstraints(0, 1, 1, 1, 0.0, 0.5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
add(westPanel, BorderLayout.WEST);
// east
eastPanel.add(eight,
new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
eastPanel.add(seven,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
add(eastPanel, BorderLayout.EAST);
// center
centerPanel.add(five, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
centerPanel.add(six, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
add(centerPanel, BorderLayout.CENTER);
//south
southPanel.add(ten,
new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(5, 0, 5, 0), 0, 0));
add(southPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame("My frame");
myFrame.add(new Toto2());
myFrame.setSize(400, 300); // pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
});
}
} |
Partager