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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
public class ClassMateFind {
static JPanel panel;
private JTextField nameInput, addressInput, homeAddressInput, sexyInput, cityInput, companyInput, dutyInput,
salaryInput, searchTextField;
private JLabel titleLabel;
public ClassMateFind() {
panel = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
panel.setLayout(gridbag);
// Title label
titleLabel = new JLabel("同学查询结果信息");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20)); // Font settings
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
// Labels for student information
JLabel[] labels = {
new JLabel("姓名"), new JLabel("性别"), new JLabel("居住地址"),
new JLabel("家庭地址"), new JLabel("所在城市"),
new JLabel("所在公司"), new JLabel("职务"), new JLabel("薪水")
};
// Creating JTextFields for input
nameInput = new JTextField(15);
addressInput = new JTextField(15);
homeAddressInput = new JTextField(15);
sexyInput = new JTextField(15);
cityInput = new JTextField(15);
companyInput = new JTextField(15);
dutyInput = new JTextField(15);
salaryInput = new JTextField(15);
JTextField[] textFields = { sexyInput, addressInput, homeAddressInput, cityInput, companyInput, dutyInput,
salaryInput };
// Set all JTextFields with the same size
Dimension commonSize = new Dimension(150, nameInput.getPreferredSize().height);
for (JTextField textField : textFields) {
textField.setPreferredSize(commonSize);
}
// Adding components to the panel using GridBagLayout
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5); // Padding for components
// Title label
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4; // Title spans across 4 columns
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(20, 5, 15, 5); // Top padding
addComponent(titleLabel, gbc);
// Labels and input fields
for (int i = 0; i < labels.length; i++) {
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5); // Default padding
// Add labels (aligned to the right)
gbc.gridx = (i < 4) ? 0 : 2; // Left or right column
gbc.gridy = (i < 4) ? i + 1 : i - 3;
gbc.anchor = GridBagConstraints.EAST;
addComponent(labels[i], gbc);
// Add input fields (aligned to the left)
gbc.gridx = (i < 4) ? 1 : 3; // Corresponding input fields
gbc.anchor = GridBagConstraints.WEST;
if (i == 0) { // First field is the name input
addComponent(nameInput, gbc);
} else {
addComponent(textFields[i - 1], gbc);
}
}
JLabel titleLabel1 = new JLabel("查询系统");
titleLabel1.setFont(new Font("微软雅黑", Font.BOLD, 20)); // Font settings
titleLabel1.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 9; // Place it below the existing input fields
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.CENTER;
addComponent(titleLabel1, gbc);
// Adding the new "按姓名查询" label and text field for searching
JLabel searchLabel = new JLabel("按姓名查询:");
searchTextField = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 10; // Place it below the existing input fields
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
addComponent(searchLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 10;
gbc.gridwidth = 2; // Spanning 2 columns for the search field
gbc.anchor = GridBagConstraints.WEST;
addComponent(searchTextField, gbc);
// Adding the search button next to the search text field
JButton searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
queryClassmateInfo(searchTextField.getText());
}
});
gbc.gridx = 3;
gbc.gridy = 10;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
addComponent(searchButton, gbc);
}
// Method to add components to the panel
private void addComponent(Component c, GridBagConstraints constraints) {
panel.add(c, constraints);
}
// Simulate querying the classmate info and updating text fields
private void queryClassmateInfo(String name) {
// Creating an instance of ClassMateStore to interact with the database
ClassMateStore store = new ClassMateStore();
Connection conn = store.getConnection();
if (conn == null) {
JOptionPane.showMessageDialog(panel, "无法连接到数据库", "数据库连接错误", JOptionPane.ERROR_MESSAGE);
return;
}
// SQL query to fetch the classmate data based on the name input
String query = "SELECT * FROM classmate WHERE name = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, name); // Set the input name as parameter
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
// Populate the JTextFields with the data from the database
nameInput.setText(rs.getString("name"));
addressInput.setText(rs.getString("address"));
homeAddressInput.setText(rs.getString("homeaddress"));
sexyInput.setText(rs.getString("sex"));
cityInput.setText(rs.getString("city"));
companyInput.setText(rs.getString("company"));
dutyInput.setText(rs.getString("duty"));
salaryInput.setText(rs.getString("salary"));
} else {
// If no result found
JOptionPane.showMessageDialog(panel, "未找到该同学信息", "查询结果", JOptionPane.INFORMATION_MESSAGE);
resetFields();
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(panel, "查询失败: " + e.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
}
}
private void resetFields() {
addressInput.setText("");
homeAddressInput.setText("");
sexyInput.setText("");
cityInput.setText("");
companyInput.setText("");
dutyInput.setText("");
salaryInput.setText("");
nameInput.setText(""); // Clear the name field
}
public static void main(String[] args) {
JFrame frame = new JFrame("同学查询系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500); // Increased size for extra components
frame.setLocationRelativeTo(null);
// Initialize and add ClassMateFind panel
ClassMateFind classMateFind = new ClassMateFind();
frame.add(classMateFind.panel);
frame.setVisible(true);
}
}
|