2019/9/19 登陆界面的完成
周一下午上JAVA课,老师给留了一个大作业,自己学习并做出一个登陆界面如下:
周三早上开始做,到周四晚上完成。过程一路坎坷,但还好有好多大佬敲出来了,自己也是借鉴了大佬的代码 https://blog.csdn.net/wyf2017/article/details/78831744?tdsourcetag=s_pcqq_aiomsg
感谢这位大佬的代码。内容很详细,自己也是补充了很多东西。第一次接触这个东西,还不是很成熟,以后还是要多看,多练习的。
代码:
这是界面类:
package com.testHomework;
import java.awt.AWTException;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginJFrame extends JFrame implements ActionListener {
JLabel jlabel1,jlabel2,jlabel3,jlabel4;
JTextField jt_usename,jt_code; // 用户名区域和验证码区域
JPasswordField jpasswordfield; // 密码区域
JButton login,regist; // 登陆按钮和注册按钮
String use_name; // 注册的账号
String use_password; // 注册的密码
JLabel jlabel5,jlabel6;
JPasswordField setPassword;
JTextField setUseName;
JButton Regist;
private ValidCode vcode = new ValidCode();
public void setLoginJFrame() {
jlabel1 = new JLabel("登陆名:");
jlabel2 = new JLabel(" 密码:");
jlabel3 = new JLabel("验证码:");
jlabel4 = new JLabel("忘记用户名/密码");
jt_usename = new JTextField();
jpasswordfield = new JPasswordField();
jt_code = new JTextField();
jlabel1.setBounds(50, 50, 60, 40);
jlabel2.setBounds(50, 100, 60, 40);
jlabel3.setBounds(50, 150, 60, 40);
jlabel4.setBounds(300,100,120,40);
jt_usename.setBounds(100, 50, 200, 40);
jpasswordfield.setBounds(100, 100, 200, 40);
jt_code.setBounds(100, 150, 100,40);
vcode.setBounds(200, 150, 70, 40);
login = new JButton("登录");
login.setBounds(120, 200, 200, 40);
regist = new JButton("快速注册");
regist.setBounds(120, 250, 200, 40);
login.addActionListener(this); //设置登录监听
regist.addActionListener(this); //设置注册监听
this.setLayout(null);
this.add(jlabel1);
this.add(jt_usename);
this.add(jlabel2);
this.add(jpasswordfield);
this.add(jlabel4);
this.add(jlabel3);
this.add(jt_code);
this.add(vcode);
this.add(login);
this.add(regist);
this.setTitle("请登录");
login.setBackground(Color.yellow);
regist.setBackground(Color.yellow);
this.setSize(450, 350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="登录") {
if(jt_code.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(!isValidCodeRight()) {
JOptionPane.showMessageDialog(null,"验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(isValidCodeRight()) {
if(use_name.equals(jt_usename.getText()) && use_password.equals(jpasswordfield.getText())) {
JOptionPane.showMessageDialog(null, "登录成功!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jt_usename.getText().isEmpty()&&jpasswordfield.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入账号和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jt_usename.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入账号","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jpasswordfield.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null,"账号和密码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
}
}
}
if(e.getActionCommand()=="快速注册") {
Regist=new JButton("注册");
jlabel5=new JLabel("注册账号:");
jlabel6=new JLabel("注册密码:");
setPassword=new JPasswordField();
setUseName=new JTextField();
jlabel5.setBounds(50, 50, 90, 40);
jlabel6.setBounds(50, 100, 90, 40);
setUseName.setBounds(120, 50, 200, 40);
setPassword.setBounds(120, 100, 200, 40);
Regist.setBounds(120, 160, 150, 40);
LoginJFrame ljf=new LoginJFrame();
ljf.setTitle("注册界面");
ljf.setLayout(null);
ljf.add(jlabel5);
ljf.add(jlabel6);
ljf.add(setPassword);
ljf.add(setUseName);
ljf.add(Regist);
Regist.setBackground(Color.yellow);
ljf.setSize(450, 350);
ljf.setLocationRelativeTo(null);
ljf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ljf.setVisible(true);
ljf.setResizable(false);
Regist.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(setPassword.getText().isEmpty()&&setUseName.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入账号和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(setUseName.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入账号!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(setPassword.getText().isEmpty()) {
JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else {
use_name=setUseName.getText();
use_password=setPassword.getText();
JOptionPane.showMessageDialog(null,"注册成功!","提示消息",JOptionPane.WARNING_MESSAGE);
ljf.setVisible(false);
}
// TODO Auto-generated method stub
}
});
}
}
public boolean isValidCodeRight() {
if(jt_code==null) {
return false;
}else if(vcode==null) {
return true;
}else if(vcode.getCode().equals(jt_code.getText())) { // getText() 得到文本框里的值
return true;
}else {
return false;
}
}
public static void main(String[] args) {
LoginJFrame jf=new LoginJFrame();
jf.setLoginJFrame();
}
}
这是验证码类:
package com.testHomework;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JComponent;
public class ValidCode extends JComponent implements MouseListener {
private int height=40,width;
private int length=6;
private String code; //验证码
private Random random=new Random();
private char character[]= {'0','1','2','3','4','5','6','7','8','9'};
// java Color 类的范围是0-255,用红、绿、蓝三个颜色调成成一个颜色
public ValidCode() {
width = this.length * 16 + (this.length - 1) * 10;
setPreferredSize(new Dimension(width, height));
setSize(width, height);
this.addMouseListener(this);
setToolTipText("点击可以更换验证码");
}
public Color getRandColor(int min,int max) {
int red=random.nextInt(max-min)+min; // 产生0-255的随机数字
int green=random.nextInt(max-min)+min;
int blue=random.nextInt(max-min)+min;
return new Color(red,green,blue);
}
// 产生六个随机验证码,或数字,或英文
public String getRandCode() {
char temp[]= new char[6];
for(int i=0;i<6;i++) {
if(random.nextBoolean()) {
temp[i]=character[random.nextInt(10)];
}else {
if(random.nextBoolean()) {
temp[i]=(char)(random.nextInt(26)+65);
}else {
temp[i]=(char)(random.nextInt(26)+97);
}
}
}
this.code=new String(temp);
return this.code;
}
public String getCode() {
return this.code;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
width = length * 16 + (length - 1) * 11;
super.setSize(width, height);
super.setPreferredSize(new Dimension(width, height));
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
g.setFont(mFont);
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200,250));
g2d.fillRect(0,0, width, height);
g2d.setColor(getRandColor(180,200));
g2d.drawRect(0,0, width-1, height-1);
//画50条干扰线
for(int i=0;i<50;i++) {
int x1=random.nextInt(width-1);
int y1=random.nextInt(height-1);
int x2=random.nextInt(width-10)+10;
int y2=random.nextInt(height-4)+4;
while(x1==x2&&y1==y2) {
x1=random.nextInt(width-1);
y1=random.nextInt(height-1);
x2=random.nextInt(width-10)+10;
y2=random.nextInt(height-4)+4;
}
g2d.setStroke(new BasicStroke(1.5F));
g2d.setColor(getRandColor(180,200));
g2d.drawLine(x1,y1,x2,y2);
}
// 画验证码
FontMetrics fm = g2d.getFontMetrics();
int base = (height - fm.getHeight())/2 + fm.getAscent();
getRandCode();
for(int i=0;i<6;i++) {
int b = random.nextBoolean()?1:-1;
g2d.rotate(random.nextInt(10)*0.01*b);
g2d.setColor(getRandColor(20,130));
g2d.drawString(code.charAt(i)+"",16*i+10, base);
}
}
public void nextCode() {
getRandCode();
repaint();
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
nextCode();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
运行结果:
什么都不输入点登录,结果是:
点击快速注册
验证码更换:鼠标移到验证码上更换。