java语言图形用户登录界面代码
作者:坏蛋好人 发布时间:2021-09-11 23:19:34
标签:java,登录
本文实例为大家分享了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


猜你喜欢
- 最近由于参加一个小小的创意比赛,用安卓做了一个小小的图片识别应用,主要是通过拍照识别图片中的菜品,还有对象位置查找的东西。之前没有做过安卓,
- 这个问题困扰了很久,有些类不是controller在使用autowired注入的类显示为空,找到网上的方法是在类初始化时主动注入被Autow
- 本文实例讲述了C#实现自定义windows系统日志的方法。分享给大家供大家参考。具体实现方法如下:using System;using Sy
- 前言quarkus号称超音速亚原子JAVA为Graalvm量身定制的java堆栈,是否名副其实呢?下面就来看看真实情况如何。动手前先简单介绍
- 为什么要限流在保证可用的情况下尽可能多增加进入的人数,其余的人在排队等待,或者返回友好提示,保证里面的进行系统的用户可以正常使用,防止系统雪
- 本文为大家分享了 Android Retrofit 2.0框架上传图片解决方案,具体内容如下1.单张图片的上传/** * 上传一
- 一. 加载预加载:1.反射注解框架Reflect信息,在Application内多线程预加载至缓存。2.资源预加载懒加载:1.Fragmen
- 最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能。在EditText提供了一个方法
- jpa配置多数据源教程很多,在Springboot2之后有一些变化,来看一下。application.yml如下spring:
- 一、网络爬虫的基本知识网络爬虫通过遍历互联网络,把网络中的相关网页全部抓取过来,这体现了爬的概念。爬虫如何遍历网络呢,互联网可以看做是一张大
- 1.基本介绍代码块又称为初始化块,属于类中的成员(类的一部分),类似于方法,讲逻辑语句封装在方法体中,用{}抱起来;但和方法不同,没有方法名
- 现象正常情况下修改完代码,运行项目就会立即生效的。但是突然有一天发现运行的还是老的代码,新代码根本没有生效。通过 mvn clean、 in
- 微信是现在比较流行的应用了,在各大安卓市场几乎都是名列前茅了。说实话不得不羡慕腾讯庞大的用户群体,只要腾讯敢做,就会有很多人去用。废话不多说
- ContentProvider是内容提供者,可以跨进程提供数据。大家都知道,ContentProvider的启动,是在Application
- 一本书的页码从自然数1开始顺序编码直到自然数n。书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0。例如第6页用6表示而不是06或0
- 最近学习了一个视频公开课,讲到了利用HorizontalScrollView仿ViewPager设计的一个简单相册,其实主要用了ViewPa
- 一、Redis分布式锁概念篇建议直接采用Redis的官方推荐的Redisson作为redis的分布式锁1.1、为什么要使用分布式锁 
- WPF中全屏窗口,会自动隐藏任务栏。那非全屏窗口如何隐藏任务栏?甚至有没有一种场景,隐藏任务后自定义一套系统任务栏来显示?以下会分阶段讲述一
- 本文实例讲述了Android编程实现自定义ProgressBar样式。分享给大家供大家参考,具体如下:效果图如下,本例中设置了第一级进度条和
- 1、前言随着技术的发展,微信的一系列服务渗透进了我们的生活,但是我们应该怎样进行微信方面的开发呢。相信很多的小伙伴们都很渴望知道吧。这篇文章