您的位置:首页 > 编程语言 > Java开发

QQ登陆页面用java实现

2013-07-27 14:11 323 查看
package com.itcast.QQTalk;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class QqLogin2 {

/**

*启动窗口类

*/

public static void main(String[] args) {

Login log = new Login("QQ2013登陆");

log.addListener();

}

}

class Login extends JFrame {

/**

* 登陆窗口

*/

private static final long serialVersionUID = 1L;

JLabel iamLab,LabZH,LabPsw,LabTop1,LabTop2;

JTextField text1;

JPasswordField text2;

JButton btn1,btn2,btn3;

public Login(String str) {

//构造方法

super(str);

iamLab = new JLabel(new ImageIcon("res\\tu1.jpg"));

this.add(iamLab);

this.setResizable(false);

//设置字体颜色和样式

LabZH = new JLabel("账号");

LabZH.setFont(new Font("楷体_GB2312", 10, 18));

LabZH.setForeground(Color.BLACK);

LabPsw = new JLabel("密码");

LabPsw.setFont(new Font("楷体_GB2312", 10, 18));

LabPsw.setForeground(Color.BLACK);

//登录窗口头部加背景图片

ImageIcon background;

background = new ImageIcon("res//qe.png");//背景图片

LabTop1 = new JLabel(background);

LabTop1.setBounds(115, 0, background.getIconWidth(), background.getIconHeight());

LabTop2 = new JLabel(background);

LabTop2.setBounds(175, 0, background.getIconWidth(), background.getIconHeight());

//增加密码文本框面板

text1 = new JTextField();

text2 = new JPasswordField();

text2.setEchoChar('*');

//添加按钮图片

btn1 = new JButton(new ImageIcon("res//denglu.jpg"));

btn2 = new JButton(new ImageIcon("res//zhuce.jpg"));

btn3 = new JButton(new ImageIcon("res//quxiao.jpg"));

//依次添加组件到窗口

iamLab.add(LabTop1);

iamLab.add(LabTop2);

iamLab.add(LabZH);

iamLab.add(text1);

iamLab.add(LabPsw);

iamLab.add(text2);

iamLab.add(btn1);

iamLab.add(btn2);

iamLab.add(btn3);

iamLab.revalidate();

//设置组件尺寸大小

text1.setSize(150, 30);

text2.setSize(150, 30);

LabZH.setSize(40, 30);

LabPsw.setSize(40, 30);

btn1.setSize(60, 25);

btn2.setSize(60, 25);

btn3.setSize(60, 25);

//设置组件位置

LabZH.setLocation(60, 80);

text1.setLocation(110, 80);

LabPsw.setLocation(60, 130);

text2.setLocation(110, 130);

btn1.setLocation(70, 180);

btn2.setLocation(150, 180);

btn3.setLocation(230, 180);

//设置窗口大小和位置并组件可见

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(360, 280);

this.setLocation(300, 180);

this.setVisible(true);

}

//为按钮添加事件监听器

public void addListener() {

//登陆按钮监听器

btn1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if((text1.getText().matches("\\d{9}")) & String.copyValueOf(text2.getPassword()).matches("\\w{1,}")) {

JOptionPane.showMessageDialog(btn1, "登陆成功");

} else if(text1.getText().equals("")) {

JOptionPane.showMessageDialog(btn1, "账号为空,请重新输入账号");

} else if(String.copyValueOf(text2.getPassword()).equals("")) {

JOptionPane.showMessageDialog(btn1, "密码为空,请重新输入密码");

} else {

text2.setText("");

JOptionPane.showMessageDialog(btn1, "密码或用户名不正确,请重新登录!");

}

}

});

//注册按钮监听器

btn2.addMouseListener(new MouseListener() {

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

JOptionPane.showMessageDialog(btn1, "请去官网网站注册或发短信“10086”到10086012520");

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

}

});

//取消按钮监听器

btn3.addMouseListener(new MouseListener() {

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseClicked(MouseEvent e) {

System.exit(0);

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐