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
|
/* Listing3106.java */
import java.awt.*;
import java.awt.event.*;
public class Listing3106
extends Frame
{
public static void main(String[] args)
{
Listing3106 wnd = new Listing3106();
wnd.setVisible(true);
}
public Listing3106()
{
super("Test GridBagLayout");
setBackground(Color.lightGray);
addWindowListener(new WindowClosingAdapter(true));
//Layout setzen und Komponenten hinzuf�gen
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc;
setLayout(gbl);
//List hinzuf�gen
List list = new List();
for (int i = 0; i < 20; ++i) {
list.add("This is item " + i);
}
gbc = makegbc(0, 0, 1, 3);
gbc.weightx = 100;
gbc.weighty = 100;
gbc.fill = GridBagConstraints.BOTH;
gbl.setConstraints(list, gbc);
add(list);
//Zwei Labels und zwei Textfelder
for (int i = 0; i < 2; ++i) {
//Label
gbc = makegbc(1, i, 1, 1);
gbc.fill = GridBagConstraints.NONE;
Label label = new Label("Label " + (i + 1));
gbl.setConstraints(label, gbc);
add(label);
//Textfeld
gbc = makegbc(2, i, 1, 1);
gbc.weightx = 100;
gbc.fill = GridBagConstraints.HORIZONTAL;
TextField field = new TextField("TextField " + (i +1));
gbl.setConstraints(field, gbc);
add(field);
}
//Ende-Button
Button button = new Button("Ende");
gbc = makegbc(2, 2, 0, 0);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbl.setConstraints(button, gbc);
add(button);
//Dialogelemente layouten
pack();
}
private GridBagConstraints makegbc(
int x, int y, int width, int height)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.insets = new Insets(1, 1, 1, 1);
return gbc;
}
}
|