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
| JTabbedPane dbTabPane = new JTabbedPane();
…… // 下面需要用到的 JButton 等组件变量定义(或声明)
private void initLayout()
{
initDBTabPane();// 初始化 JTabbedPane BTabPane 组件
this.getContentPane().add(BorderLayout.CENTER, dbTabPane);
// 将 JTabbedPane 组件:dbTabPane 布局于 JDialog 对话框的中间
initButtonPanel();// 初始化 JPanel:ButtonPanel 组件
this.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
// 将 JPanel 组件:buttonPanel 布局于 JDialog 对话框的底部(南面)
}
private void initDBTabPane()
{
JPanel loginPanel = new JPanel(new GridLayout(10, 1));
// 为保证两个 JCheckBox 组件位于顶端,设置为共 10 行,每行一个组件的布局,但只
// 放置界面要求的两个组件,这样就保持了界面的美观,否则如定义为
//Gridlayout(2,1) 则会使两个组件居中,而且中间会隔开较长的距离。
pwdBox.setMnemonic('P');
loginPanel.add(pwdBox);
dspBox.setMnemonic('D');
loginPanel.add(dspBox);
dbTabPane.add("Login", loginPanel); // 设置"Login"JPanel(图 3.4-1)的布局
needRadio.setMnemonic('N');
allRadio.setMnemonic('A');
cacheRadio.setMnemonic('U');
radioPanel.setBorder(new TitledBorder("Load Option"));// 加上边界标题
radioPanel.add(needRadio);
radioPanel.add(allRadio);
radioPanel.add(cacheRadio);
// 以上为加入需要的 JRadioButton 组件到指定的 JPanel: radioPanel
queryPanel.add(radioPanel);// 加入含 JRadioButton 组的 JPanel 到 queryPanel
reqBox.setMnemonic('R');
boxPanel.add(reqBox);
saveBox.setMnemonic('S');
boxPanel.add(saveBox);
autoBox.setMnemonic('t');
boxPanel.add(autoBox);
// 以上为加入需要的 JCheckBox 组到指定的 JPanel:boxPanel
queryPanel.add(boxPanel); // 加入含 JCheckBox 组的 JPanel 到 queryPanel
dbTabPane.add("Query", queryPanel);// 设置"Query"JPanel(图 3.4-2)的布局
initDrvPanel();
}
/** 设置"Drivers"JPanel(图 3.4-3)的布局 */
private void initDrvPanel()
{
gridBag.fill = GridBagConstraints.HORIZONTAL;
gridBag.weightx = 100;
gridBag.weighty = 0;
tipLabel.setForeground(Color.black);
this.add(drvPanel, tipLabel, gridBag, 0, 0, 4, 1);
urlLabel.setForeground(Color.black);
this.add(drvPanel, urlLabel, gridBag, 0, 5, 4, 1);
urlField.setEditable(false);
this.add(drvPanel, urlField, gridBag, 0, 6, 4, 1);
gridBag.weightx = 0;
gridBag.weighty = 0;
addButton.setMnemonic('A');
this.add(drvPanel, addButton, gridBag, 3, 1, 1, 1);
editButton.setMnemonic('E');
this.add(drvPanel, editButton, gridBag, 3, 2, 1, 1);
removeButton.setMnemonic('R');
this.add(drvPanel, removeButton, gridBag, 3, 3, 1, 1);
gridBag.fill = GridBagConstraints.BOTH;
gridBag.weightx = 100;
gridBag.weighty = 100;
// 设置 JTable 组件:drvTable 的从 0 到 7 行第 0 列的值
for (int i = 0; i < 8; i++)
drvTable.setValueAt(drvStrs,i,0);
// 设置 JTable 的列头
drvTable.getColumn(drvTable.getColumnName(0)).setHeaderValue("All Drivers");
drvTable.setShowGrid(false);// 设置不显示网格线
this.add(drvPanel, drvScroll, gridBag, 0, 1, 3, 4);
dbTabPane.add("Drivers", drvPanel);
}
/** 初始化底部 JButton 组的布局 */
private void initButtonPanel()
{
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
// 从右边开始进行 FlowLayout 布局
okButton.setMnemonic('O');
buttonPanel.add(okButton);
cancelButton.setMnemonic('C');
buttonPanel.add(cancelButton);
helpButton.setMnemonic('H');
buttonPanel.add(helpButton);
}
/** 给指定的容器 cn 在指定的(x,y)位置放置指定大小(宽度 =w, 高度 =h)的组件 c*/
private void add(Container cn, Component c, GridBagConstraints gbc, int x,
int y, int w, int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
cn.add(c, gbc);
}
|