?? login.java
字號:
/*
* Login.java
*美化后的HappyChat登入界面
*/
package happychat;
import happychat.jdbc_login;
import java.awt.*;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
/**
* HAPPYCHAT聊天系統登入界面
* @author 范方青
*/
public class Login extends JFrame implements ActionListener{
JPanel pnlLogin;
JButton btnLogin,btnRegister,btnExit;
JLabel lblServer,lblUserName,lblPassword,lblLogo;
JTextField txtUserName,txtServer;
JPasswordField pwdPassword;
//用于將窗口定位
Dimension scrnsize;
Toolkit toolkit=Toolkit.getDefaultToolkit();
/** Creates a new instance of Longin */
public Login() {
super("登入[HappyChat]聊天室");//
pnlLogin = new JPanel();
this.getContentPane().add(pnlLogin);
//初始化組件
lblServer = new JLabel(" 服務器(S):");
lblUserName = new JLabel(" 用戶名(U):");
lblPassword = new JLabel(" 口 令(P):");
txtServer = new JTextField();
txtServer.setText("127.0.0.1");
txtUserName = new JTextField(20);
pwdPassword = new JPasswordField(20);
btnLogin = new JButton("登入(L)");
btnLogin.setToolTipText("登入到服務器");
btnLogin.setMnemonic('L');
btnRegister = new JButton("注冊(R)");
btnRegister.setToolTipText("注冊新用戶");
btnRegister.setMnemonic('R');
btnExit = new JButton("退出(X)");
btnExit.setToolTipText("退出系統");
btnExit.setMnemonic('X');
//設置面板背景顏色
pnlLogin.setLayout(null);
pnlLogin.setBackground(new Color(52,130,203));
lblServer.setBounds(50,100,100,30);
txtServer.setBounds(150,105,120,20);
lblUserName.setBounds(50,130,100,30);
txtUserName.setBounds(150,135,120,20);
lblPassword.setBounds(50,160,100,30);
pwdPassword.setBounds(150,165,120,20);
btnLogin.setBounds(50,200,80,25);
btnRegister.setBounds(130,200,80,25);
btnExit.setBounds(210,200,80,25);
//設置組件字體
Font fontstr = new Font("宋體",Font.PLAIN,12);
lblServer.setFont(fontstr);
txtServer.setFont(fontstr);
lblUserName.setFont(fontstr);
txtUserName.setFont(fontstr);
lblPassword.setFont(fontstr);
pwdPassword.setFont(fontstr);
btnLogin.setFont(fontstr);
btnRegister.setFont(fontstr);
btnExit.setFont(fontstr);
//設置組件背景顏色
lblUserName.setForeground(Color.BLACK);
lblPassword.setForeground(Color.BLACK);
btnLogin.setBackground(Color.ORANGE);
btnRegister.setBackground(Color.ORANGE);
btnExit.setBackground(Color.ORANGE);
//設置背景圖片
Icon logo1 = new ImageIcon("D:\\MyCode\\java\\HappyChat\\build\\classes\\happychat\\login.gif");
//JLabel.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\范方青\\桌面\\logo2.jpg"));
//lblLogo =jLabel.setText("JLabel");
lblLogo = new JLabel(logo1);
lblLogo.setBounds(0,0,340,100);
pnlLogin.add(lblLogo);
//添加組件到面板
pnlLogin.add(lblLogo);
pnlLogin.add(lblServer);
pnlLogin.add(txtServer);
pnlLogin.add(lblUserName);
pnlLogin.add(txtUserName);
pnlLogin.add(lblPassword);
pnlLogin.add(pwdPassword);
pnlLogin.add(btnLogin);
btnLogin.addActionListener(this);
pnlLogin.add(btnRegister);
btnRegister.addActionListener(this);
pnlLogin.add(btnExit);
btnExit.setActionCommand("quit");
btnExit.addActionListener(this);
//設置登入窗口
setResizable(false);
setSize(340,260);
setVisible(true);
scrnsize = toolkit.getScreenSize();
setLocation(scrnsize.width/2-this.getWidth()/2,scrnsize.height/2-this.getHeight()/2);
Image img = toolkit.getImage(this.getClass().getResource("1.jpg").toString().substring(6));
this.setIconImage(img);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnLogin){
//判斷用戶密碼是否為空**與服務器連接**數據庫查詢賬戶是否可用**************************
if(txtUserName.getText().equals("")||pwdPassword.getPassword().length==0)
{
JOptionPane.showMessageDialog(null,"用戶、密碼不能為空");
}
else{
//登入聊天界面
//strServerIP = txtServer.getText();
// Login();
try {
String n = this.txtUserName.getText();
String p = String.valueOf(this.pwdPassword.getPassword());
// DBUtility p11 = new DBUtility();
//System.out.println(p11.x(n,p));
DataBase database = new DataBase();
//jdbc_login p11 = new jdbc_login();
if(database.isLogin(n,p)){
//System.out.println("1");
JFrame p2 = new chatdemo();
p2.setVisible(true);
this.setVisible(false);
//database.closeConnection();
}
else{
//System.out.println("0");
JOptionPane.showMessageDialog(null,"用戶不存在、或密碼錯誤");
}
}catch (Exception exx){
exx.printStackTrace();
}
}
}
if(e.getSource() == btnRegister){
//strServer IP = txtServer.getText();
// this.dispose();
JFrame p1 = new Register_Customer();
// p1.getContentPane().setBackground(new Color(52,130,203));
p1.setVisible(true);
this.setVisible(false);
//進入注冊界面****************************
}
if(e.getSource() == btnExit){
System.exit(0);
}
}
public static void main(String []args){
new Login();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -