帮我写一个有可视化界面登录注册功能的,且登陆以后可以进行选择五句古诗词出上句接下句或者做100以内的加减法测试的java系统代码
时间: 2023-07-16 22:16:19 浏览: 45
好的,这里是一个简单的Java系统代码,包含了登录注册功能和选择五句古诗词出上句接下句或者做100以内的加减法测试的功能。
需要注意的是,这个代码只是一个简单的示例,可能还需要进行优化和修改才能适用于实际的项目。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginSystem extends JFrame implements ActionListener {
JTextField usernameField;
JPasswordField passwordField;
JButton loginButton, registerButton;
public LoginSystem() {
super("登录系统");
// 创建界面组件
JLabel usernameLabel = new JLabel("用户名:");
JLabel passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("登录");
registerButton = new JButton("注册");
// 设置布局
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(usernameLabel);
inputPanel.add(usernameField);
inputPanel.add(passwordLabel);
inputPanel.add(passwordField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(loginButton);
buttonPanel.add(registerButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// 添加事件监听器
loginButton.addActionListener(this);
registerButton.addActionListener(this);
// 设置窗口大小和关闭方式
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) { // 处理登录按钮事件
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (login(username, password)) {
// 登录成功,打开主界面
new MainSystem();
dispose(); // 关闭登录界面
} else {
// 处理登录失败情况
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
usernameField.setText("");
passwordField.setText("");
}
} else if (e.getSource() == registerButton) { // 处理注册按钮事件
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (register(username, password)) {
JOptionPane.showMessageDialog(this, "注册成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名已存在!");
usernameField.setText("");
passwordField.setText("");
}
}
}
// 登录方法,返回true表示登录成功,false表示登录失败
public boolean login(String username, String password) {
// 此处省略具体实现,可以是从数据库或文件中读取数据进行验证
return true;
}
// 注册方法,返回true表示注册成功,false表示用户名已存在
public boolean register(String username, String password) {
// 此处省略具体实现,可以是将用户名和密码保存到数据库或文件中
return true;
}
}
class MainSystem extends JFrame implements ActionListener {
JButton poemButton, mathButton;
public MainSystem() {
super("主界面");
// 创建界面组件
JLabel promptLabel = new JLabel("请选择功能:");
poemButton = new JButton("古诗词接龙");
mathButton = new JButton("100以内加减法测试");
// 设置布局
JPanel buttonPanel = new JPanel(new GridLayout(2, 1));
buttonPanel.add(poemButton);
buttonPanel.add(mathButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(promptLabel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
// 添加事件监听器
poemButton.addActionListener(this);
mathButton.addActionListener(this);
// 设置窗口大小和关闭方式
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == poemButton) { // 处理古诗词接龙按钮事件
// TODO: 实现古诗词接龙功能
} else if (e.getSource() == mathButton) { // 处理100以内加减法测试按钮事件
// TODO: 实现100以内加减法测试功能
}
}
}
```