⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginui.java

📁 基于 C/S 模式的网上购物系统
💻 JAVA
字号:
package RegisterAndLogin;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import java.net.*;
import java.io.*;
import javax.swing.ImageIcon;
public class LoginUI extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JLabel jLabelName = null;

	private JLabel jLabelPassword = null;

	private JTextField jTextFieldName = null;

	private JPasswordField jPasswordField = null;

	private JButton jButtonShoper = null;

	private JButton jButtonRegister = null;

	//private ButtonGroup group=new ButtonGroup();  //  @jve:decl-index=0:

	//private JRadioButton[] button=new JRadioButton[2];
	private Socket clientSocket=null;
	private ObjectOutputStream os=null;
	private ObjectInputStream is=null;
	public ServerSocket control=null;
	public ControlLisening  conthread=null;   
	public Socket con=null;

	private JButton jButtonShopHost = null;

	private JLabel jLabel1Image = null;
	/**
	 * This method initializes jTextFieldName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldName() {
		if (jTextFieldName == null) {
			jTextFieldName = new JTextField();
			jTextFieldName.setBounds(new Rectangle(342, 97, 141, 28));
		}
		return jTextFieldName;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(345, 141, 146, 28));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jButtonShoper	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonShoper() {
		if (jButtonShoper == null) {
			jButtonShoper = new JButton();
			jButtonShoper.setBounds(new Rectangle(217, 187, 107, 27));
			jButtonShoper.setText("以购物者登录");
			jButtonShoper.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				//	System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					
					try{
					clientSocket=new Socket("192.168.0.4",8000);
					os=new ObjectOutputStream(clientSocket.getOutputStream());
					is=new ObjectInputStream(clientSocket.getInputStream());
					
				/*	control=new ServerSocket(8001);
				    con=control.accept();
				    System.out.println(con);
				    conthread=new ControlLisening(con);
				    conthread.start();
				   */ 
					String Info=ConstanValue.MSG_LOGIN;
					os.writeObject(Info);
					String username=jTextFieldName.getText().trim();
					//if(username.equals(""))
					//	JOptionPane.showMessageDialog(null, "用户名不能为空");
					//String word=Password.createPassword(jPasswordField.getText().trim());
					String word=jPasswordField.getText().trim();
					os.writeObject(username);
					os.writeObject(word);
					os.writeBoolean(false);
					os.flush();
					//InetAddress clientAddress=new InetAddress(clientSocket.getInetAddress());
					//ps=new PrintStream(clientSocket.getOutputStream());
					//br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
				    //ps.println(Info);
					//ps.flush();
					//String flag=group.getSelection().getActionCommand();
					//if(flag.equals("shopper"))
					//	os.writeBoolean(false);
					//else
					//	os.writeBoolean(true);
					//os.flush();
					String result=(String)is.readObject();
					if(result.equals(ConstanValue.MSG_ACC_LOGIN))
					{
						JOptionPane.showMessageDialog(null,"AccLogin");
						setVisible(false);
						try{												
						      	os.writeObject("hello");						
						     ServerSocket sevsoc=new ServerSocket(ConstanValue.SERVER_CONTROL_PORT);
						      Socket conn=sevsoc.accept();
						      System.out.println(conn);						      
						     ObjectInputStream conis=new ObjectInputStream(conn.getInputStream());
						     ObjectOutputStream conos=new ObjectOutputStream(conn.getOutputStream());
						     System.out.println("1");	
						     Hall frame=new Hall(clientSocket,os,is,username);
						     System.out.println("2");
						     ControlLisening control=new ControlLisening(
						    		 conn,frame,conis,conos);
						      control.start();
						}
						catch(Exception ex)
						{
							ex.printStackTrace();
						}
					}
					else if(result.equals(ConstanValue.MSG_REF_LOGIN))
					{
						String explation=(String)is.readObject();
						Info="登录失败\n"+"原因:"+explation;
						JOptionPane.showMessageDialog(null, Info);
						os.close();
						is.close();
						clientSocket.close();						
					}
					
					}
					catch(Exception ex)
					{
						System.out.println(ex);
					}
				}
			});
		}
		return jButtonShoper;
	}

	/**
	 * This method initializes jButtonRegister	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonRegister() {
		if (jButtonRegister == null) {
			jButtonRegister = new JButton();
			jButtonRegister.setBounds(new Rectangle(283, 239, 123, 31));
			jButtonRegister.setText("注册");
			jButtonRegister.addActionListener(new java.awt.event.ActionListener()
			{public void actionPerformed(java.awt.event.ActionEvent e) {
				RegisterUI frame=new RegisterUI();
			}	
			});
		}
		return jButtonRegister;
	}

	/**
	 * @param args
	 */
/*	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LoginUI thisClass = new LoginUI();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}
*/
	/**
	 * This is the default constructor
	 */
	public LoginUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(534, 361);
		this.setContentPane(getJContentPane());
		this.setTitle("诚信购物登录");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1Image = new JLabel();
			jLabel1Image.setBounds(new Rectangle(10, 10, 198, 305));
			ImageIcon image=new ImageIcon("image/image1.gif");
			jLabel1Image.setText("JLabel");
			jLabel1Image.setIcon(image);
			jLabelPassword = new JLabel();
			jLabelPassword.setBounds(new Rectangle(214, 144, 108, 26));
			jLabelPassword.setText("密码");
			jLabelName = new JLabel();
			jLabelName.setBounds(new Rectangle(213, 99, 106, 28));
			jLabelName.setText("用户名");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(260, 22, 159, 40));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setText("欢迎使用诚信购物系统");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabelName, null);
			jContentPane.add(jLabelPassword, null);
			jContentPane.add(getJTextFieldName(), null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(getJButtonShoper(), null);
			jContentPane.add(getJButtonRegister(), null);
			jContentPane.add(getJButtonShopHost(), null);		   
			jContentPane.add(jLabel1Image, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButtonShopHost	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonShopHost() {
		if (jButtonShopHost == null) {
			jButtonShopHost = new JButton();
			jButtonShopHost.setBounds(new Rectangle(348, 185, 124, 31));
			jButtonShopHost.setText("以店主登录");
			jButtonShopHost.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					
						//	System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
							
							try{
							clientSocket=new Socket("192.168.0.4",8000);
							os=new ObjectOutputStream(clientSocket.getOutputStream());
							is=new ObjectInputStream(clientSocket.getInputStream());
							
						
							String Info=ConstanValue.MSG_LOGIN;
							os.writeObject(Info);
							String username=jTextFieldName.getText().trim();
							
							String word=jPasswordField.getText().trim();
							os.writeObject(username);
							os.writeObject(word);
							os.writeBoolean(true);
							os.flush();
							
							String result=(String)is.readObject();
							if(result.equals(ConstanValue.MSG_ACC_LOGIN))
							{
								JOptionPane.showMessageDialog(null,"AccLogin");
								setVisible(false);
								try{												
								   ShopHostUI    frame=new ShopHostUI(clientSocket,os,is,username);							
								   System.out.println("wo");
								   Bargain bargain=new Bargain(username);
								   bargain.start();
								  
								}
								catch(Exception ex)
								{
									ex.printStackTrace();
								}
							}
							else if(result.equals(ConstanValue.MSG_REF_LOGIN))
							{
								String explation=(String)is.readObject();
								Info="登录失败\n"+"原因:"+explation;
								JOptionPane.showMessageDialog(null, Info);
								os.close();
								is.close();
								clientSocket.close();						
							}
							
							}
							catch(Exception ex)
							{
								System.out.println(ex);
							}						
				}
			});
			
		}
		return jButtonShopHost;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -