java语言图形用户登录界面代码

所属分类: 软件编程 / java 阅读数: 47
收藏 0 赞 0 分享

本文实例为大家分享了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程序设计有所帮助。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多