?? 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+": 歡迎您使用網(wǎng)絡(luò)購物系統(tǒng)!\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+": 歡迎您使用網(wǎng)絡(luò)購物系統(tǒng)!\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);
}
}
//構(gòu)造函數(shù)
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; //設(shè)定Layout的位置
gbc.insets = new Insets(2,2,2,2); //設(shè)定與邊界的距離(上,左,下,右)
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()+": 歡迎您使用網(wǎng)絡(luò)購物系統(tǒng)!\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," 該用戶已經(jīng)登錄!","登錄錯誤",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){}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -