java语言图形用户登录界面代码
作者:坏蛋好人 时间:2021-09-11 23:19:34
本文实例为大家分享了java登录界面的具体实现代码,供大家参考,具体内容如下
1. Login.java
package wzb;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
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 Login extends JFrame implements ActionListener {
String userName;
String password;
String captcha;
public static String randomcaptcha;
public JLabel logoLabel, userNameLabel, passwordLabel, captchaLabel;
public JTextField userNameInput, captchaInput;
public JPasswordField passwordInput;
public JButton login, logout,change;
public Panel panel;
public Login() {
setTitle("µÇ¼½çÃæ");
setSize(400, 300);
setLocationRelativeTo(null);
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
public void init() {
setLayout(null);
// logoLabel= new JLabel();
// logoLabel.setIcon(new ImageIcon("E:\\eclipse\\student3\\welcome.gif"));
logoLabel = new JLabel(new ImageIcon("welcome.gif"));
logoLabel.setBounds(125, 10, 150, 70);
add(logoLabel);
userNameLabel = new JLabel("̞:");
userNameLabel.setBounds(90, 90, 60, 40);
add(userNameLabel);
userNameInput = new JTextField();
userNameInput.setBounds(150, 100, 150, 20);
add(userNameInput);
passwordLabel = new JLabel("ÃÜ¡¡Âë:");
passwordLabel.setBounds(90, 120, 60, 40);
add(passwordLabel);
passwordInput = new JPasswordField();
passwordInput.setBounds(150, 130, 150, 20);
add(passwordInput);
captchaLabel = new JLabel("ÑéÖ¤Âë:");
captchaLabel.setBounds(90, 150, 60, 40);
add(captchaLabel);
captchaInput = new JTextField();
captchaInput.setBounds(150, 160, 70, 20);
add(captchaInput);
panel = new PanelDemo();
panel.setBounds(220, 160, 80, 20);
add(panel);
change = new JButton("»»Ò»»»");
change.setBounds(300, 160, 80, 20);
change.setContentAreaFilled(false);
change.setBorderPainted(false);
add(change);
login = new JButton("µÇ¼£¨L£©", new ImageIcon("login.gif"));
login.setBounds(70, 200, 120, 30);
login.setMnemonic(KeyEvent.VK_L);
add(login);
logout = new JButton("Í˳ö£¨X£©", new ImageIcon("exit.gif"));
logout.setBounds(210, 200, 120, 30);
logout.setMnemonic(KeyEvent.VK_X);
add(logout);
userNameInput.addActionListener(this);
passwordInput.addActionListener(this);
captchaInput.addActionListener(this);
login.addActionListener(this);
logout.addActionListener(this);
change.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
userName = userNameInput.getText();
password = new String(passwordInput.getPassword());
captcha = captchaInput.getText();
if (e.getSource() == change) {
panel.repaint();
}
if (e.getSource() == login) {
if ((userName.equals("w")) && (password.equals("w"))) {
if (captcha.equals(randomcaptcha)) {
JOptionPane.showMessageDialog(this, "»¶Ó­µÇ½!");
} else {
JOptionPane.showMessageDialog(this, "ÑéÖ¤Âë´íÎó!");
panel.repaint();
}
} else {
JOptionPane.showMessageDialog(this, "Óû§Ãû»òÃÜÂë´íÎó!");
}
}
if (e.getSource() == logout) {
JOptionPane.showMessageDialog(this, "»¶Ó­Ï´ÎÔÙÀ´£¡");
//System.exit(0);
dispose();
}
}
public static void main(String[] args) {
new Login();
}
}
class PanelDemo extends Panel {
public void paint(Graphics g) {
int width = 80;
int height = 20;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width, height);
Random rd = new Random();
for (int i = 0; i < 100; i++) {
int x = rd.nextInt(width) - 2;
int y = rd.nextInt(height) - 2;
g.setColor(Color.RED);
g.drawOval(x, y, 2, 2);
}
g.setFont(new Font("ºÚÌå", Font.BOLD, 20));
g.setColor(Color.BLUE);
char[] c = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
int index = rd.nextInt(c.length);
sb.append(c[index] + " ");
}
g.drawString(sb.toString(), 0, 18);
String str = sb.toString().replaceAll(" ", "");
Login.randomcaptcha = str;
}
}
2. 捕获.PNG
标签:java,登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java并发中的内存模型
2022-09-07 17:54:47
![](https://img.aspxhome.com/file/2023/3/62093_0s.png)
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
![](https://img.aspxhome.com/file/2023/9/82419_0s.gif)
GSON实现Java对象与JSON格式对象相互转换的完全教程
2023-11-23 09:23:37
mybatis新手快速入门以及一些错误汇总
2023-01-25 13:01:14
![](https://img.aspxhome.com/file/2023/8/63798_0s.png)
使用@pathvariable与@requestparam碰到的一些问题及解决
2023-11-27 16:30:34
![](https://img.aspxhome.com/file/2023/9/60459_0s.png)
MyBatis-plus中的模糊查询解读
2022-06-16 08:27:03
![](https://img.aspxhome.com/file/2023/5/61665_0s.png)
Android应用开发SharedPreferences存储数据的使用方法
2023-06-23 16:02:47
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
java 使用foreach遍历集合元素的实例
2022-11-17 09:24:58
springboot如何统一设置时区
2022-01-26 18:16:39
![](https://img.aspxhome.com/file/2023/9/63659_0s.png)
Java内存模型与JVM运行时数据区的区别详解
2023-11-24 13:29:08
springboot接收别人上传的本地视频实例代码
2023-03-22 01:55:39
基于Java8实现提高Excel读写效率
2023-11-25 10:01:37
如何自动生成Mybatis的Mapper文件详解
2023-07-28 08:20:54
![](https://img.aspxhome.com/file/2023/8/57938_0s.jpg)
java Mail邮件接收工具类
2022-04-24 14:17:17
java中Date类和Strng类的灵活转化
2022-12-09 07:47:24
Spring Boot 读取静态资源文件的方法
2023-08-25 02:53:07
IDEA中配置文件模板的添加方法
2023-10-28 17:52:31
![](https://img.aspxhome.com/file/2023/4/58674_0s.jpg)
java利用数组随机抽取幸运观众
2023-11-11 11:14:33
![](https://img.aspxhome.com/file/2023/0/59400_0s.jpg)
idea springboot远程debug的操作方法
2022-11-01 00:04:31
![](https://img.aspxhome.com/file/2023/8/62498_0s.png)
浅析Mybatis 在CS程序中的应用
2023-06-24 08:44:15
![](https://img.aspxhome.com/file/2023/2/57422_0s.jpg)