📄 register.java
字号:
package com.chatroom;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.io.*;
import sun.security.x509.RFC822Name;
public class Register extends JFrame {
private ButtonGroup buttonGroup = new ButtonGroup();
private JPasswordField passwordField_1;
private JPasswordField passwordField;
private JTextField textField;
private Login login;
static int personNumber =0;
JOptionPane optionPane;
String inStr;
String sex;
String writeMessage;
/**
* Launch the application
* @param args
*/
/* public static void main(String args[]) {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}*/
/**
* Create the frame
*/
public Register() {
super("注册");
getContentPane().setBackground(Color.PINK);
getContentPane().setLayout(null);
setBounds(100, 100, 360,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
final JLabel label = new JLabel();
label.setForeground(Color.BLUE);
label.setText("请输入用户名");
label.setBounds(54, 68, 120, 30);
getContentPane().add(label);
textField = new JTextField();
textField.setBounds(168, 69, 120, 30);
getContentPane().add(textField);
final JLabel label_1 = new JLabel();
label_1.setForeground(Color.BLUE);
label_1.setText("请输入密码");
label_1.setBounds(54, 122, 120, 30);
getContentPane().add(label_1);
passwordField = new JPasswordField();
passwordField.setForeground(Color.RED);
passwordField.setBounds(168, 123, 120, 30);
getContentPane().add(passwordField);
final JLabel label_2 = new JLabel();
label_2.setForeground(Color.BLUE);
label_2.setText("请确认密码");
label_2.setBounds(54, 173, 120, 30);
getContentPane().add(label_2);
passwordField_1 = new JPasswordField();
passwordField_1.setForeground(Color.RED);
passwordField_1.setBounds(168, 174, 120, 30);
getContentPane().add(passwordField_1);
final JRadioButton radioButton = new JRadioButton();
radioButton.setSelected(true);
radioButton.setBackground(Color.PINK);
buttonGroup.add(radioButton);
radioButton.setText("男");
radioButton.setBounds(80, 209, 120, 30);
getContentPane().add(radioButton);
final JRadioButton radioButton_1 = new JRadioButton();
radioButton_1.setBackground(Color.PINK);
buttonGroup.add(radioButton_1);
radioButton_1.setText("女");
radioButton_1.setBounds(224, 209, 120, 30);
getContentPane().add(radioButton_1);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
optionPane.showMessageDialog(null, "很遗憾注册失败,将退回上一步!", "退出消息",
optionPane.ERROR_MESSAGE);
toLogin();
}
});
button.setBackground(Color.CYAN);
button.setText("退出");
button.setBounds(0, 245, 120, 30);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
private Login login;
public void actionPerformed(ActionEvent arg0) {
int i=0;
if (textField.getText().length()==0) {
optionPane.showMessageDialog(null, "用户名不能为空");
} else {
i++;
}
if (passwordField.getPassword().length==0) {
optionPane.showMessageDialog(null, "登录密码不能为空");
} else {
i++;
}
if (passwordField_1.getPassword().length==0) {
optionPane.showMessageDialog(null, "确认密码不能为空");
} else {
i++;
}
if (!new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))) {
optionPane.showMessageDialog(null, "两次输入密码不相同,请再次输入!");
} else {
i++;
}
if(i==4){
if(radioButton_1.isSelected()==false){
sex=radioButton.getText();
}else{
sex=radioButton_1.getText();
}
optionPane.showMessageDialog(null, "恭喜您注册成功!" +"\n"+
"您的用户名为:"+textField.getText() + "\n"+"您的登录密码为:"
+ new String(passwordField.getPassword())+"\n"+"您的性别为:"
+sex);
String writeMessage="第"+personNumber+"位用户信息如下:"+" "+"用户名:"+textField.getText()+"登录密码:"
+ new String(passwordField.getPassword())+"性别:"+sex+"\r\n";
try {
File f =new File("e:\\order.txt");
if (!f.exists()) {
f.createNewFile();
}
FileWriter fw =new FileWriter(f,true);
BufferedWriter bw =new BufferedWriter(fw);
bw.write(writeMessage);
bw.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
toLogin();
}
personNumber++;
}
});
button_1.setBackground(Color.CYAN);
button_1.setText("提交");
button_1.setBounds(234, 245, 120, 30);
getContentPane().add(button_1);
final JLabel label_3 = new JLabel();
label_3.setForeground(Color.YELLOW);
label_3.setFont(new Font("华文行楷", Font.BOLD | Font.ITALIC, 26));
label_3.setText(" 友谊聊天室提醒你注册信息");
label_3.setBounds(0, 0, 354, 62);
getContentPane().add(label_3);
}
private void toLogin() {
// TODO Auto-generated method stub
if (this.isVisible()) {
this.setVisible(false);
}
login =new Login();
login.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -