📄 newaccount.java
字号:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class NewAccount extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
LoginFrame loginFrame;
private JButton submitButton;
private JButton resetButton;
private JButton cancelButton;
private JTextField usernameTF;
private JPasswordField passwordTF;
private JPasswordField pwverifyTF;
private JTextField useridTF;
public NewAccount(LoginFrame loginFrame){
this.loginFrame=loginFrame;
usernameTF=new JTextField(15);
passwordTF=new JPasswordField(15);
pwverifyTF=new JPasswordField(15);
useridTF=new JTextField(15);
JPanel userInfo=new JPanel();
userInfo.setLayout(new GridLayout(4,1));
JPanel userInfo1=new JPanel();
userInfo1.add(new JLabel("username"));
userInfo1.add(usernameTF);
JPanel userInfo2=new JPanel();
userInfo2.add(new JLabel("password"),passwordTF);
userInfo2.add(passwordTF);
JPanel userInfo3=new JPanel();
userInfo3.add(new JLabel("pwverify"),pwverifyTF);
userInfo3.add(pwverifyTF);
JPanel userInfo4=new JPanel();
userInfo4.add(new JLabel("userid "),useridTF);
userInfo4.add(useridTF);
userInfo.add(userInfo1);
userInfo.add(userInfo2);
userInfo.add(userInfo3);
userInfo.add(userInfo4);
submitButton=new JButton("submit");
resetButton=new JButton("reset");
cancelButton=new JButton("cancel");
JPanel buttons=new JPanel();
buttons.add(submitButton);
buttons.add(resetButton);
buttons.add(cancelButton);
submitButton.addActionListener(this);
resetButton.addActionListener(this);
cancelButton.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(userInfo,BorderLayout.CENTER);
this.add(buttons,BorderLayout.SOUTH);
this.setSize(400,200);
}
@Override
public void actionPerformed(ActionEvent event) {
String command=event.getActionCommand();
if(command.equals(submitButton.getText())){
if(submit()){
this.loginFrame.setVisible(true);
this.setVisible(false);
}
else reset();
}
else if(command.equals(cancelButton.getText())){
this.setVisible(false);
loginFrame.setVisible(true);
}
else if(command.equals(resetButton.getText())){
reset();
}
}
private void reset() {
usernameTF.setText("");
passwordTF.setText("");
pwverifyTF.setText("");
useridTF.setText("");
}
private boolean submit() {
String username=usernameTF.getText();
char password[]=passwordTF.getPassword();
char pwverify[]=pwverifyTF.getPassword();
String userid=useridTF.getText();
if(username==null||username.equals("")){
JOptionPane.showMessageDialog(this, "username is empty!");
return false;
}
if(!Arrays.equals(password, pwverify)){
password=null;
pwverify=null;
passwordTF.setText("");
pwverifyTF.setText("");
JOptionPane.showMessageDialog(this, "password is not match!");
return false;
}
if(!isLegalId(userid)){
JOptionPane.showMessageDialog(this, "userid is lawless!");
return false;
}
Long id=Const.getAccount();
User user=new User();
user.setAccount(id+1+"");
user.setUsername(username);
user.setPassword(password);
user.setUserid(userid);
if(!Const.newAccount(user))return false;
return true;
}
private boolean isLegalId(String userid) {
if(userid==null||userid.length()>18||userid.length()==0)
return false;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -