?? usereditdialog.java
字號:
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class UserEditDialog implements ActionListener
{
private JTextField userName,password,passwordConfirm,account,shopName;
private JDialog dialog;
private BufferedReader in;
private PrintWriter out;
private int userID;
public UserEditDialog(JFrame f,User u,BufferedReader fin,PrintWriter fout)
{
in = fin;
out = fout;
userID = u.getId();
dialog = new JDialog(f,"更新個人信息",true);
dialog.setResizable(false);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridBagLayout());
textPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray,2),
"請填寫新的個人信息",TitledBorder.CENTER,TitledBorder.TOP));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2,2,2,2);
JLabel l1 = new JLabel("用戶名(*):");
JLabel l2 = new JLabel("密碼(*):");
JLabel l3 = new JLabel("密碼確認(*):");
JLabel l4 = new JLabel("帳戶號碼(*):");
JLabel l5 = new JLabel("地址:");
JLabel l6 = new JLabel("電子郵箱:");
userName = new JTextField(12);
userName.setText(u.getUserName());
password = new JTextField(12);
password.setText(u.getPassword());
passwordConfirm = new JTextField(12);
passwordConfirm.setText(u.getPassword());
account = new JTextField(12);
account.setText(u.getAccount()+"");
gbc.gridy=1;
gbc.gridx=0;
textPanel.add(l1,gbc);
gbc.gridx=1;
textPanel.add(userName,gbc);
gbc.gridy=2;
gbc.gridx=0;
textPanel.add(l2,gbc);
gbc.gridx=1;
textPanel.add(password,gbc);
gbc.gridy=3;
gbc.gridx=0;
textPanel.add(l3,gbc);
gbc.gridx=1;
textPanel.add(passwordConfirm,gbc);
gbc.gridy=4;
gbc.gridx=0;
textPanel.add(l4,gbc);
gbc.gridx=1;
textPanel.add(account,gbc);
gbc.gridy=5;
gbc.gridx=0;
textPanel.add(l5,gbc);
gbc.gridx=1;
textPanel.add(new JTextField(12),gbc);
gbc.gridy=6;
gbc.gridx=0;
textPanel.add(l6,gbc);
gbc.gridx=1;
textPanel.add(new JTextField(12),gbc);
JPanel buttonPanel=new JPanel();
JButton b=new JButton("確定");
b.addActionListener(this);
buttonPanel.add(b);
b=new JButton("取消");
b.addActionListener(this);
buttonPanel.add(b);
dialog.add(textPanel,BorderLayout.CENTER);
dialog.add(buttonPanel,BorderLayout.SOUTH);
dialog.setBounds(382,200,260,300);
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if (cmd.equals("確定"))
{
boolean legal=true;
if(userName.getSelectionEnd()==0 ||password.getSelectionEnd()==0 || passwordConfirm.getSelectionEnd()==0 || account.getSelectionEnd()==0)
{
legal = false;
JOptionPane.showMessageDialog(null," 請填寫完整!","填寫信息不正確",2);
}
else if(!password.getText().equals(passwordConfirm.getText()) )
{
legal = false;
JOptionPane.showMessageDialog(null," 兩次輸入的密碼不相同!","填寫信息不正確",2);
}
else if(password.getText().length()<6 ||password.getText().length()>20 )
{
legal = false;
JOptionPane.showMessageDialog(null," 密碼長度不符合規則!","填寫信息不正確",2);
}
else if(userName.getText().length()>12)
{
legal = false;
JOptionPane.showMessageDialog(null," 用戶名過長!","填寫信息不正確",2);
}
else
{
char[] temp = userName.getText().toCharArray();
for(int i=0;i<userName.getText().length();i++)
{
if(!((temp[i]>='0'&&temp[i]<='9')||(temp[i]>='a'&&temp[i]<='z')||(temp[i]>='A'&&temp[i]<='Z')||(temp[i]=='_')))
{
legal = false;
JOptionPane.showMessageDialog(null," 用戶名填寫不符合規則!","填寫信息不正確",2);
return;
}
}
temp = password.getText().toCharArray();
for(int i=0;i<password.getText().length();i++)
{
if(!((temp[i]>='0'&&temp[i]<='9')||(temp[i]>='a'&&temp[i]<='z')||(temp[i]>='A'&&temp[i]<='Z')))
{
legal = false;
JOptionPane.showMessageDialog(null," 密碼填寫不符合規則!","填寫信息不正確",2);
return;
}
}
temp = account.getText().toCharArray();
for(int i=0;i<account.getText().length();i++)
{
if(!((temp[i]>='0'&&temp[i]<='9')))
{
legal = false;
JOptionPane.showMessageDialog(null," 帳戶號填寫不符合規則!","填寫信息不正確",2);
return;
}
}
}
if(legal)
{
try
{
out.println("UPDATEUSER");
out.println(userID);
out.println(userName.getText());
out.println(password.getText());
out.println(account.getText());
String result = in.readLine();
if(result.equals("USED"))
{
out.println(JOptionPane.showInputDialog(null,"該賬戶已存在!","請輸入擁有該賬戶的用戶名"));
result = in.readLine();
}
if(result.equals("SUCCESS"))
{
JOptionPane.showMessageDialog(null,userName.getText()+": 已成功修改個人信息!","更新成功",1);
dialog.dispose();
}
if(result.equals("FAILURE"))
{
JOptionPane.showMessageDialog(null," 該用戶名已存在,請重新選擇用戶名!","更新失敗",0);
}
if(result.equals("ERROR"))
{
JOptionPane.showMessageDialog(null," 系統更新信息失敗!","更新失敗",0);
}
if(result.equals("AFAILURE"))
{
JOptionPane.showMessageDialog(null,"賬戶信息輸入不正確,請重新選擇賬戶號碼!","更新失敗",0);
}
}catch(Exception ex){}
}
}
else if (cmd.equals("取消"))
{
dialog.dispose();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -