📄 logondialog.java
字号:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
//登录界面
class LogonDialog implements ActionListener
{
private final JFrame f;
private JTextField t1;
private JPasswordField t2;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
//登录方式选择对话框类
class ChooseDialog
{
JDialog dialog;
ChooseDialog(final JFrame f,final int ID,final String name,final String psw,final String sname,final String stype,final String sinfo,final int aid)
{
dialog = new JDialog(f,"登录选择!",true);
JPanel p = new JPanel();
p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,3),
"请选择已何种身份登录",TitledBorder.CENTER,TitledBorder.TOP));
JButton b = new JButton("顾客");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialog.dispose();
f.dispose();
JOptionPane.showMessageDialog(null,name+": 欢迎您使用网络购物系统!\n\n ---Copyright By TERRY\n ---version1.0","登录成功",1);
Customer c = new Customer(ID,name,psw,aid,true);
Mall mall = new Mall(c,socket);
}
});
p.add(b);
b = new JButton("店主");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialog.dispose();
f.dispose();
JOptionPane.showMessageDialog(null,name+": 欢迎您使用网络购物系统!\n\n ---Copyright By TERRY\n ---version1.0","登录成功",1);
Merchant m = new Merchant(ID,name,psw,sname,stype,sinfo,aid);
ShopManager manager = new ShopManager(m,socket);
}
});
p.add(b);
dialog.add(p,BorderLayout.CENTER);
dialog.setBounds(415,310,200,120);
dialog.setVisible(true);
}
}
//构造函数
public LogonDialog(Socket socket)
{
this.socket = socket;
f = new JFrame("Logon");
try
{
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
}catch(Exception e)
{
}
Container contentPane=f.getContentPane();
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST; //设定Layout的位置
gbc.insets = new Insets(2,2,2,2); //设定与边界的距离(上,左,下,右)
textPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,2),
"请登录,新用户请注册",TitledBorder.CENTER,TitledBorder.TOP));
JLabel l1 = new JLabel("用户名:");
JLabel l2 = new JLabel("密码:");
t1 = new JTextField(12);
t2 = new JPasswordField(12);
t2.setEchoChar('*');
gbc.gridy=1;
gbc.gridx=0;
textPanel.add(l1,gbc);
gbc.gridx=1;
textPanel.add(t1,gbc);
gbc.gridy=2;
gbc.gridx=0;
textPanel.add(l2,gbc);
gbc.gridx=1;
textPanel.add(t2,gbc);
JPanel buttonPanel=new JPanel();
JButton b=new JButton("注册");
b.addActionListener(this);
buttonPanel.add(b);
b=new JButton("登录");
b.addActionListener(this);
buttonPanel.add(b);
JLabel pic = new JLabel(new ImageIcon(".\\logon.jpg"));
contentPane.add(pic,BorderLayout.NORTH);
contentPane.add(textPanel,BorderLayout.CENTER);
contentPane.add(buttonPanel,BorderLayout.SOUTH);
f.pack();
f.setBounds(377,100,270,450);
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try
{
out.println("END");
in.close();
out.close();
// socket.close();
}catch(IOException ex){}
System.exit(0);
}
});
}
//事件处理
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if (cmd.equals("注册"))
{
new LoginSystem(f,socket);
}
else if (cmd.equals("登录"))
{
try
{
out.println("LOGON");
out.println(t1.getText());
out.println(new String(t2.getPassword()));
String result = in.readLine();
if(result.equals("SUCCESS"))
{
int ID = Integer.parseInt(in.readLine());
int AID = Integer.parseInt(in.readLine());
f.dispose();
JOptionPane.showMessageDialog(null,t1.getText()+": 欢迎您使用网络购物系统!\n\n ---Copyright By TERRY\n ---version1.0","登录成功",1);
Customer c = new Customer(ID,t1.getText(),new String(t2.getPassword()),AID,false);
Mall mall = new Mall(c,socket);
}
else if(result.equals("ALREADY"))
{
JOptionPane.showMessageDialog(null," 该用户已经登录!","登录错误",2);
}
else if(result.equals("FAILURE"))
{
JOptionPane.showMessageDialog(null," 密码错误!","登录错误",2);
}
else if(result.equals("NOTEXIST"))
{
JOptionPane.showMessageDialog(null," 该用户不存在!","登录错误",2);
}
else if(result.equals("CHOOSE"))
{
final int ID = Integer.parseInt(in.readLine());
final int AID = Integer.parseInt(in.readLine());
final String userName = t1.getText();
final String password = new String(t2.getPassword());
final String shopName = in.readLine();
final String shopType = in.readLine();
final String shopInfo = in.readLine();
new ChooseDialog(f,ID,userName,password,shopName,shopType,shopInfo,AID);
}
}
catch(IOException ex){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -