用java写一个网吧管理系统,要有用户登陆和电脑管理功能,并且写出用户登陆和电脑管理功能的面板,写出源代码
时间: 2024-03-08 22:50:38 浏览: 19
好的,我可以为您提供一些参考代码。
首先,我们需要设计一下网吧管理系统的数据结构,如下所示:
```
// 用户类
class User {
public String username; // 用户名
public String password; // 密码
}
// 电脑类
class Computer {
public int id; // 电脑编号
public boolean isUsed; // 是否正在被使用
}
```
接下来,我们可以开始编写Java代码了。首先是用户登陆功能的面板:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginPanel extends JPanel {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JLabel messageLabel;
public LoginPanel() {
setLayout(new GridLayout(3, 2));
JLabel usernameLabel = new JLabel("用户名:");
add(usernameLabel);
usernameField = new JTextField();
add(usernameField);
JLabel passwordLabel = new JLabel("密码:");
add(passwordLabel);
passwordField = new JPasswordField();
add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
// TODO: 在这里进行用户验证,验证通过则跳转到电脑管理面板
}
});
add(loginButton);
messageLabel = new JLabel("");
add(messageLabel);
}
}
```
接下来是电脑管理功能的面板:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ComputerPanel extends JPanel {
private ArrayList<Computer> computers;
private JLabel[] computerLabels;
public ComputerPanel() {
// 初始化电脑列表
computers = new ArrayList<Computer>();
for (int i = 1; i <= 20; i++) {
Computer computer = new Computer();
computer.id = i;
computer.isUsed = false;
computers.add(computer);
}
// 初始化界面
setLayout(new GridLayout(5, 4));
computerLabels = new JLabel[20];
for (int i = 0; i < 20; i++) {
JLabel label = new JLabel(String.format("电脑%d", i + 1));
add(label);
computerLabels[i] = label;
}
// 更新电脑状态
updateComputerLabels();
}
// 更新电脑状态
private void updateComputerLabels() {
for (int i = 0; i < 20; i++) {
Computer computer = computers.get(i);
if (computer.isUsed) {
computerLabels[i].setText(String.format("电脑%d(正在使用)", computer.id));
} else {
computerLabels[i].setText(String.format("电脑%d(空闲)", computer.id));
}
}
}
}
```
最后,我们需要将这两个面板组合起来,并编写一个主函数:
```java
import java.awt.*;
import javax.swing.*;
public class InternetCafeSystem extends JFrame {
public InternetCafeSystem() {
setTitle("网吧管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// 创建用户登陆面板和电脑管理面板
LoginPanel loginPanel = new LoginPanel();
ComputerPanel computerPanel = new ComputerPanel();
// 创建选项卡面板
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("用户登陆", loginPanel);
tabbedPane.addTab("电脑管理", computerPanel);
// 将选项卡面板添加到窗口中
add(tabbedPane);
setVisible(true);
}
public static void main(String[] args) {
new InternetCafeSystem();
}
}
```
以上就是一个简单的网吧管理系统的Java实现,希望能对您有所帮助。