帮我写一个 java gui 点名教师端
时间: 2023-08-12 09:05:13 浏览: 61
好的,以下是一个简单的 Java GUI 点名教师端示例程序:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TeacherAttendance extends JFrame implements ActionListener {
private JLabel title;
private JLabel nameLabel;
private JTextField nameField;
private JButton presentButton;
private JButton absentButton;
private JTextArea attendanceList;
public TeacherAttendance() {
// 设置窗口标题
setTitle("Teacher Attendance");
// 创建标题标签
title = new JLabel("Teacher Attendance");
title.setFont(new Font("Arial", Font.BOLD, 24));
title.setHorizontalAlignment(SwingConstants.CENTER);
// 创建姓名标签和文本框
nameLabel = new JLabel("Name:");
nameField = new JTextField(20);
// 创建出席和缺席按钮
presentButton = new JButton("Present");
absentButton = new JButton("Absent");
presentButton.addActionListener(this);
absentButton.addActionListener(this);
// 创建出席列表文本区域
attendanceList = new JTextArea(10, 20);
attendanceList.setEditable(false);
// 创建面板并添加组件
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 1));
panel.add(title);
panel.add(nameLabel);
panel.add(nameField);
panel.add(presentButton);
panel.add(absentButton);
// 将面板添加到窗口中
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(attendanceList), BorderLayout.CENTER);
// 设置窗口大小和可见性
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == presentButton) {
attendanceList.append(nameField.getText() + " is present.\n");
} else if (e.getSource() == absentButton) {
attendanceList.append(nameField.getText() + " is absent.\n");
}
nameField.setText("");
}
public static void main(String[] args) {
new TeacherAttendance();
}
}
```
该程序包含一个窗口,其中包含一个标题标签、一个姓名标签和文本框、一个出席按钮、一个缺席按钮和一个出席列表文本区域。当用户输入姓名并单击“Present”或“Absent”按钮时,程序将在出席列表文本区域中显示相应的消息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)