运用Java Swing图形用户界面编程,写一篇代码,完成以下要求 1. 实现页面菜单,菜单项包括按名字读取、按学号读取、按班级读取; 2. 实现搜索框,用于输入名字或学号,根据名字或学号查询学生信息; 3. 实现班级下拉列表,用于选择班级并按班级查询学生信息。 4. 实现学生信息展示功能,展示从数据库中查询到的学生信息;5.连接到MySQL数据库中的student中
时间: 2024-02-25 19:57:24 浏览: 87
以下是一个实现上述要求的Java Swing代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class StudentGUI extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:mysql://localhost:3306/student";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static final String[] HEADERS = {"ID", "Name", "Class", "Score"};
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem byNameItem;
private JMenuItem byNumberItem;
private JMenuItem byClassItem;
private JPanel searchPanel;
private JLabel searchLabel;
private JTextField searchField;
private JButton searchButton;
private JPanel classPanel;
private JLabel classLabel;
private JComboBox<String> classCombo;
private JButton classButton;
private JTable table;
private DefaultTableModel tableModel;
public StudentGUI() {
super("Student Information");
// Create menu bar and menu items
menuBar = new JMenuBar();
menu = new JMenu("Read");
byNameItem = new JMenuItem("By Name");
byNumberItem = new JMenuItem("By Number");
byClassItem = new JMenuItem("By Class");
// Add menu items to menu and menu to menu bar
menu.add(byNameItem);
menu.add(byNumberItem);
menu.add(byClassItem);
menuBar.add(menu);
// Create search panel
searchPanel = new JPanel();
searchLabel = new JLabel("Search:");
searchField = new JTextField(20);
searchButton = new JButton("Go");
searchPanel.add(searchLabel);
searchPanel.add(searchField);
searchPanel.add(searchButton);
// Create class panel
classPanel = new JPanel();
classLabel = new JLabel("Class:");
classCombo = new JComboBox<String>(new String[]{"Class 1", "Class 2", "Class 3"});
classButton = new JButton("Go");
classPanel.add(classLabel);
classPanel.add(classCombo);
classPanel.add(classButton);
// Create table and table model
tableModel = new DefaultTableModel(null, HEADERS);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
// Add components to frame
add(menuBar, BorderLayout.NORTH);
add(searchPanel, BorderLayout.WEST);
add(classPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
// Add action listeners
byNameItem.addActionListener(this);
byNumberItem.addActionListener(this);
byClassItem.addActionListener(this);
searchButton.addActionListener(this);
classButton.addActionListener(this);
// Set frame properties
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == byNameItem) {
// TODO: Implement by name read functionality
} else if (e.getSource() == byNumberItem) {
// TODO: Implement by number read functionality
} else if (e.getSource() == byClassItem) {
// TODO: Implement by class read functionality
} else if (e.getSource() == searchButton) {
// Get search term from search field
String term = searchField.getText();
// Clear table
tableModel.setRowCount(0);
// Query database for matching students
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "SELECT * FROM students WHERE name LIKE ? OR number LIKE ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "%" + term + "%");
statement.setString(2, "%" + term + "%");
ResultSet result = statement.executeQuery();
while (result.next()) {
Object[] row = {result.getInt("id"), result.getString("name"), result.getString("class"), result.getInt("score")};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == classButton) {
// Get selected class from class combo box
String selectedClass = (String) classCombo.getSelectedItem();
// Clear table
tableModel.setRowCount(0);
// Query database for matching students
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "SELECT * FROM students WHERE class = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, selectedClass);
ResultSet result = statement.executeQuery();
while (result.next()) {
Object[] row = {result.getInt("id"), result.getString("name"), result.getString("class"), result.getInt("score")};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new StudentGUI();
}
}
```
说明:
1. 这个程序包含一个名为`StudentGUI`的类,继承自`JFrame`类,用于创建用户界面。
2. 在构造函数中,我们创建了菜单栏(`JMenuBar`)、菜单(`JMenu`)和菜单项(`JMenuItem`),并将它们添加到界面上。
3. 我们还创建了搜索面板(`searchPanel`)和班级面板(`classPanel`),它们包含了搜索框(`searchField`)、搜索按钮(`searchButton`)、班级下拉列表(`classCombo`)和查询按钮(`classButton`)。
4. 我们还创建了一个数据表(`table`)和表模型(`tableModel`),用于展示学生信息。
5. 在`actionPerformed`方法中,我们根据触发事件的组件执行相应的操作。当按下菜单项时,我们将实现按名字、按学号或按班级读取学生信息的功能。当按下搜索按钮或班级查询按钮时,我们将从数据库中查询学生信息,并将它们展示在数据表中。
阅读全文