forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridBag4.java
More file actions
35 lines (31 loc) · 984 Bytes
/
Copy pathGridBag4.java
File metadata and controls
35 lines (31 loc) · 984 Bytes
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
//file: GridBag4.java
import java.awt.*;
import javax.swing.*;
public class GridBag4 extends JPanel {
GridBagConstraints constraints = new GridBagConstraints();
public GridBag4() {
setLayout(new GridBagLayout());
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 1.0;
int x, y; // for clarity
constraints.weightx = 0.1;
addGB(new JButton("one"), x = 0, y = 0);
constraints.weightx = 0.5;
addGB(new JButton("two"), ++x, y);
constraints.weightx = 1.0;
addGB(new JButton("three"), ++x, y);
}
void addGB(Component component, int x, int y) {
constraints.gridx = x;
constraints.gridy = y;
add(component, constraints);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridBag4");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300, 100);
frame.setLocation(200, 200);
frame.setContentPane(new GridBag4());
frame.setVisible(true);
}
}