javaswing 学生选课系统
时间: 2023-07-30 22:10:40 浏览: 29
Java Swing 是 Java 的一个 GUI 工具包,可以用来开发桌面应用程序。下面是一个简单的学生选课系统的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CourseSelectionSystem extends JFrame {
private JLabel courseLabel;
private JList<String> courseList;
private JButton selectButton;
private DefaultListModel<String> courseModel;
public CourseSelectionSystem() {
setTitle("学生选课系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建课程标签
courseLabel = new JLabel("请选择要选的课程:");
// 创建课程列表
courseModel = new DefaultListModel<>();
courseModel.addElement("Java程序设计");
courseModel.addElement("计算机网络");
courseModel.addElement("数据库原理");
courseList = new JList<>(courseModel);
courseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 创建选课按钮
selectButton = new JButton("选课");
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String course = courseList.getSelectedValue();
if (course != null) {
JOptionPane.showMessageDialog(CourseSelectionSystem.this, "您已成功选修了 " + course + " 课程!");
} else {
JOptionPane.showMessageDialog(CourseSelectionSystem.this, "请选择要选修的课程!");
}
}
});
// 将组件添加到窗口中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(courseLabel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(courseList), BorderLayout.CENTER);
contentPane.add(selectButton, BorderLayout.SOUTH);
// 设置窗口大小并居中显示
setSize(400, 300);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
CourseSelectionSystem courseSelectionSystem = new CourseSelectionSystem();
courseSelectionSystem.setVisible(true);
}
}
```
这个程序界面比较简单,包含一个课程标签、一个课程列表和一个选课按钮。当用户点击选课按钮时,程序会弹出一个对话框,显示用户选修的课程。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)