-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShowGridBagLayout.java
86 lines (70 loc) · 3.04 KB
/
ShowGridBagLayout.java
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
package programs;
import java.awt.*;
import javax.swing.*;
public class ShowGridBagLayout extends JApplet {
private JLabel jlbl = new JLabel(
"Resize the Window and Study GridBagLayout", JLabel.CENTER);
private JTextArea jta1 = new JTextArea("Text Area 1", 5, 15 );
private JTextArea jta2 = new JTextArea("Text Area 2", 5, 15 );
private JTextField jtf = new JTextField("JTextField");
private JPanel jp = new JPanel();
private JButton jbt1 = new JButton("Button 1");
private JButton jbt2 = new JButton("Button 2");
public ShowGridBagLayout() {
// Set GridBagLayout in the container
setLayout(new GridBagLayout());
// Create an GridBagConstraints object
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.fill = GridBagConstraints.BOTH;
gbConstraints.anchor = GridBagConstraints.CENTER;
Container container = getContentPane();
// Place JLabel to occupy row 0 (the first row)
addComp(jlbl, container, gbConstraints, 0, 0, 1, 4, 0, 0);
// Place text area 1 in row 1 and 2, and column 0
addComp(jta1, container, gbConstraints, 1, 0, 2, 1, 5, 1);
// Place text area 2 in row 1 and column 3
addComp(jta2, container, gbConstraints, 1, 3, 1, 1, 5, 1);
// Place text field in row 2 and column 3
addComp(jtf, container, gbConstraints, 2, 3, 1, 1, 5, 0);
// Place JButton 1 in row 3 and column 1
addComp(jbt1, container, gbConstraints, 3, 1, 1, 1, 5, 0);
// Place JButton 2 in row 3 and column 2
addComp(jbt2, container, gbConstraints, 3, 2, 1, 1, 5, 0);
// Place Panel in row 1 and 2, and column 1 and 2
jp.setBackground(Color.red);
jp.setBorder(new javax.swing.border.LineBorder(Color.black));
gbConstraints.insets = new Insets(10, 10, 10, 10);
addComp(jp, container, gbConstraints, 1, 1, 2, 2, 10, 1);
}
/** Add a component to the container of GridBagLayout */
private void addComp(Component c, Container container,
GridBagConstraints gbConstraints,
int row, int column,
int numberOfRows, int numberOfColumns,
double weightx, double weighty) {
// Set parameters
gbConstraints.gridx = column;
gbConstraints.gridy = row;
gbConstraints.gridwidth = numberOfColumns;
gbConstraints.gridheight = numberOfRows;
gbConstraints.weightx = weightx;
gbConstraints.weighty = weighty;
// Add component to the container with the specified layout
container.add(c, gbConstraints);
}
public static void main(String[] args) {
ShowGridBagLayout applet = new ShowGridBagLayout();
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("ShowGridBagLayout");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}