Java Swing图形用户界面编程实验代码按照实验步骤 1.实现页面菜单,菜单项包括按名字读取、按学号读取、按班级读取; 2.实现搜索框,用于输入名字或学号,根据名字或学号查询学生信息; 3.实现班级下拉列表,用于选择班级并按班级查询学生信息。 4.实现学生信息展示功能,展示从数据库中查询到的学生信息; 并使用SwingUtilities.invokeLater(JCUI::createUI);
时间: 2024-02-23 08:58:47 浏览: 130
基于Java Swing+MySQL实现(界面)的宿舍信息管理系统【100011233】
以下是Java Swing图形用户界面编程实验的示例代码:
```
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
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.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JCUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/student";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "password";
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem nameMenuItem;
private JMenuItem idMenuItem;
private JMenuItem classMenuItem;
private JPanel searchPanel;
private JTextField searchField;
private JButton searchButton;
private JPanel classPanel;
private JLabel classLabel;
private JComboBox<String> classComboBox;
private JButton classButton;
private JTable table;
private DefaultTableModel tableModel;
private Connection conn;
private Statement stmt;
public JCUI() {
super("学生信息查询");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
nameMenuItem = new JMenuItem("按名字读取");
idMenuItem = new JMenuItem("按学号读取");
classMenuItem = new JMenuItem("按班级读取");
nameMenuItem.addActionListener(this);
idMenuItem.addActionListener(this);
classMenuItem.addActionListener(this);
fileMenu.add(nameMenuItem);
fileMenu.add(idMenuItem);
fileMenu.add(classMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 创建搜索框和按钮
searchPanel = new JPanel();
searchPanel.setLayout(new BorderLayout());
searchField = new JTextField();
searchButton = new JButton("搜索");
searchButton.addActionListener(this);
searchPanel.add(searchField, BorderLayout.CENTER);
searchPanel.add(searchButton, BorderLayout.EAST);
searchPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(searchPanel, BorderLayout.NORTH);
// 创建班级选择框和按钮
classPanel = new JPanel();
classPanel.setLayout(new BorderLayout());
classLabel = new JLabel("班级:");
classComboBox = new JComboBox<String>();
classButton = new JButton("查询");
classButton.addActionListener(this);
classPanel.add(classLabel, BorderLayout.WEST);
classPanel.add(classComboBox, BorderLayout.CENTER);
classPanel.add(classButton, BorderLayout.EAST);
classPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(classPanel, BorderLayout.SOUTH);
// 创建表格
tableModel = new DefaultTableModel();
tableModel.addColumn("姓名");
tableModel.addColumn("学号");
tableModel.addColumn("班级");
tableModel.addColumn("性别");
tableModel.addColumn("年龄");
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(600, 400));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
// 连接数据库
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
stmt = conn.createStatement();
String sql = "SELECT DISTINCT class FROM student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String className = rs.getString("class");
classComboBox.addItem(className);
}
rs.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nameMenuItem) {
String name = JOptionPane.showInputDialog(this, "请输入姓名:");
if (name != null && !name.trim().isEmpty()) {
searchByName(name.trim());
}
} else if (e.getSource() == idMenuItem) {
String id = JOptionPane.showInputDialog(this, "请输入学号:");
if (id != null && !id.trim().isEmpty()) {
searchById(id.trim());
}
} else if (e.getSource() == classMenuItem) {
String className = JOptionPane.showInputDialog(this, "请输入班级:");
if (className != null && !className.trim().isEmpty()) {
searchByClass(className.trim());
}
} else if (e.getSource() == searchButton) {
String keyword = searchField.getText().trim();
if (!keyword.isEmpty()) {
searchByNameOrId(keyword);
}
} else if (e.getSource() == classButton) {
String className = (String) classComboBox.getSelectedItem();
if (className != null && !className.trim().isEmpty()) {
searchByClass(className.trim());
}
}
}
private void searchByName(String name) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE name LIKE '%" + name + "%'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchById(String id) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE id='" + id + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchByClass(String className) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE class='" + className + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchByNameOrId(String keyword) {
clearTable();
try {
String sql = "SELECT * FROM student WHERE name LIKE '%" + keyword + "%' OR id='" + keyword + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] row = new String[5];
row[0] = rs.getString("name");
row[1] = rs.getString("id");
row[2] = rs.getString("class");
row[3] = rs.getString("gender");
row[4] = String.valueOf(rs.getInt("age"));
tableModel.addRow(row);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clearTable() {
int rowCount = tableModel.getRowCount();
for (int i = rowCount - 1; i >= 0; i--) {
tableModel.removeRow(i);
}
}
public static void createUI() {
JCUI ui = new JCUI();
ui.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JCUI::createUI);
}
}
```
注意:这只是一个示例代码,实际项目中需要根据具体需求进行修改和完善。
阅读全文