先看效果
1.建立包com.QQUI0819
2.在包下建立类
LoginAction
package com.QQUI0819;
import javax.swing.*;
import java.awt.event.*;
//首先,编写按钮癿监听器实现类
public class LoginAction implements
ActionListener {
private int count=0;
//当前为null,创建后指向界面输入框
private JTextField ulName;
private JTextField ulpass;
//创建时,输入界面类中输入框
public LoginAction(JTextField ulName,JTextField ulpass ){
this.ulName=ulName;
this.ulpass=ulpass;
}
//实现接口中的方法
//当劢作发生时,这个方法执行
public void actionPerformed(ActionEvent e) {
//点击时,就取得界面输入框癿内容
//此时癿jtf,指向是界面上那个输入框
String o=ulName.getText();
String p=ulName.getText();
System.out.println("账号输入的是 "+o);
System.out.println("密码输入的是 "+p);
if(o.equals("saygood") &&(p.equals("123456"))){
//如果输入正确,弹出新界面
JFrame jf=new JFrame();
jf.setTitle("登陆成功");
jf.setSize(300,400);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
} else {
//如果输入正确,弹出新界面
JFrame jf=new JFrame();
jf.setTitle("登陆失败");
jf.setSize(300,100);
JButton b1 = new JButton("登陆失败,账号和密码不匹配,请重新登陆");
jf.add(b1);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}
}
QQ.java
package com.QQUI0819;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
public class QQ extends JFrame{
//用户名
private JTextField ulName;
//密码
private JPasswordField ulPasswd;
//小容器
private JLabel j1;
private JLabel j2;
private JLabel j3;
private JLabel j4;
JLabel j5;
JLabel j6;
//小按钮
private JButton b1;
//复选框
private JCheckBox c1;
private JCheckBox c2;
JPanel jp;
/**
* 初始化QQ登录页面
* */
public QQ(){
//设置登录窗口标题
this.setTitle("QQ登录");
//去掉窗口的装饰(边框)
//采用指定的窗口装饰风格
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
//窗体组件初始化
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置布局为绝对定位
this.setLayout(null);
this.setBounds(0, 0, 355, 265);
//设置窗体的图标
Image img0 = new ImageIcon("C:\\Users\\lenovo\\Pictures\\Java题\\qq1.jpg").getImage();
this.setIconImage(img0);
//窗体大小不能改变
this.setResizable(false);
//居中显示
this.setLocationRelativeTo(null);
//窗体显示
this.setVisible(true);
}
/**
* 窗体组件初始化
* */
public void init(){
//创建一个容器,其中的图片大小和setBounds
jp = new JPanel();
Container container = this.getContentPane();
j1 = new JLabel();
//设置背景色
Image img1 = new ImageIcon("C:\\\\Users\\\\lenovo\\\\Pictures\\\\Java题\\\\qq.jpg").getImage();
j1.setIcon(new ImageIcon(img1));
j1.setBounds(0, 0, 355, 90);
//qq头像设定
j2 = new JLabel();
Image img2 = new ImageIcon("C:\\Users\\lenovo\\Pictures\\Java题\\qq1.jpg").getImage();
j2.setIcon(new ImageIcon(img2));
j2.setBounds(150, 40, 60, 60);
//用户名输入框
ulName = new JTextField();
ulName.setBounds(100, 100, 150, 20);
//注册账号
j3 = new JLabel("账号");
j3.setBounds(260, 100, 70, 20);
//密码输入框
ulPasswd = new JPasswordField();
ulPasswd.setBounds(100, 130, 150, 20);
j4= new JLabel("密码");
j4.setBounds(260, 130, 70, 20);
j5= new JLabel("注册账号");
j5.setBounds(180, 155, 80, 15);
j6= new JLabel("忘记密码");
j6.setBounds(250, 155, 80, 15);
//记住密码
c1 = new JCheckBox("记住密码");
c1.setBounds(20, 155, 80, 15);
//自动登陆
c2 = new JCheckBox("自动登陆");
c2.setBounds(100, 155, 80, 15);
//登陆按钮
b1 = new JButton("登录");
//设置字体和颜色和手形指针
b1.setFont(new Font("宋体", Font.PLAIN, 12));
b1.setForeground(Color.black);
b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
b1.setBounds(20, 200, 300, 20);
JLabel jl3=new JLabel("状态:");
jp.add(jl3);
jl3.setBounds(10, 100, 30, 10);
JComboBox<String> jcb1=new JComboBox();
jp.add(jcb1);
jcb1.setBounds(10, 100,30, 10);
jcb1.addItem("在线");
jcb1.addItem("隐身") ;
jcb1.addItem("追剧中") ;
jcb1.addItem("吃瓜") ;
jcb1.addItem("读书中") ;
//给按钮添加
//所有组件用容器装载
this.add(j1);
this.add(j2);
this.add(j3);
this.add(j4);
this.add(j5);
this.add(j6);
this.add(c1);
this.add(c2);
this.add(b1);
add(jcb1);
add(jcb1);
add(jl3);
//创建监听器对象,幵加给按钮
LoginAction lo=new LoginAction(ulName,ulPasswd);
b1.addActionListener(lo);
container.add(j1);
container.add(ulName);
container.add(ulPasswd);
}
public static void main(String[] args) {
new QQ();
}
}