实验目标 掌握Java Swing图形用户界面编程 实验步骤 1. 实现页面菜单,菜单项包括按名字读取、按学号读取、按班级读取; 2. 实现搜索框,用于输入名字或学号,根据名字或学号查询学生信息; 3. 实现班级下拉列表,用于选择班级并按班级查询学生信息。 4. 实现学生信息展示功能,展示从数据库中查询到的学生信息
时间: 2023-12-10 20:38:16 浏览: 100
;
以下是一个简单的Java Swing图形用户界面编程示例,实现了以上的四个功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class StudentInfoGUI extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:mysql://localhost:3306/student_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static final String[] CLASS_OPTIONS = {"Class 1", "Class 2", "Class 3"};
private JLabel nameLabel, idLabel, classLabel;
private JTextField nameField, idField;
private JComboBox<String> classComboBox;
private JButton searchButton;
private JTextArea resultArea;
public StudentInfoGUI() {
super("Student Info");
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem nameItem = new JMenuItem("Search by Name");
JMenuItem idItem = new JMenuItem("Search by ID");
JMenuItem classItem = new JMenuItem("Search by Class");
nameItem.addActionListener(this);
idItem.addActionListener(this);
classItem.addActionListener(this);
fileMenu.add(nameItem);
fileMenu.add(idItem);
fileMenu.add(classItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 创建搜索面板
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
nameLabel = new JLabel("Name:");
idLabel = new JLabel("ID:");
nameField = new JTextField(10);
idField = new JTextField(10);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
searchPanel.add(nameLabel);
searchPanel.add(nameField);
searchPanel.add(idLabel);
searchPanel.add(idField);
searchPanel.add(searchButton);
// 创建班级下拉列表
classLabel = new JLabel("Class:");
classComboBox = new JComboBox<>(CLASS_OPTIONS);
classComboBox.addActionListener(this);
// 创建结果文本域
resultArea = new JTextArea(20, 50);
resultArea.setEditable(false);
// 将组件添加到容器中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(searchPanel, BorderLayout.NORTH);
JPanel classPanel = new JPanel();
classPanel.add(classLabel);
classPanel.add(classComboBox);
contentPane.add(classPanel, BorderLayout.WEST);
contentPane.add(new JScrollPane(resultArea), BorderLayout.CENTER);
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String name = nameField.getText().trim();
String id = idField.getText().trim();
String className = (String) classComboBox.getSelectedItem();
search(name, id, className);
} else if (e.getActionCommand().equals("Search by Name")) {
nameField.requestFocus();
} else if (e.getActionCommand().equals("Search by ID")) {
idField.requestFocus();
} else if (e.getActionCommand().equals("Search by Class")) {
classComboBox.requestFocus();
}
}
private void search(String name, String id, String className) {
resultArea.setText("");
String sql = "SELECT * FROM students WHERE name LIKE ? AND id LIKE ? AND class LIKE ?";
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "%" + name + "%");
stmt.setString(2, "%" + id + "%");
stmt.setString(3, "%" + className + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String result = String.format("Name: %s\nID: %s\nClass: %s\nScore: %d\n\n",
rs.getString("name"), rs.getString("id"), rs.getString("class"), rs.getInt("score"));
resultArea.append(result);
}
if (resultArea.getText().isEmpty()) {
resultArea.setText("No records found.");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
StudentInfoGUI gui = new StudentInfoGUI();
gui.setVisible(true);
}
}
```
在本示例中,我们使用了Java Swing的各种组件,如JLabel、JTextField、JComboBox、JButton、JTextArea等,同时还实现了ActionListener接口以响应用户事件。在搜索函数中,我们使用了Java JDBC连接MySQL数据库,并使用PreparedStatement执行带参数的SQL查询语句,以避免SQL注入攻击。
阅读全文
相关推荐
















