登录窗口一般很常见,现在让我们自己也来写一个吧!
PS:很多import是重复的,是因为我是分了几个类写的,必须单独导入
//模拟qq登录窗口import java.awt.*;import java.io.*;import java.awt.event.*;import javax.swing.*;public class QQGUI extends JFrame implements ActionListener{ private JLabel userLa; private JLabel pwdLa; private JLabel verCodeLa;//验证码 private JTextField userTxt; private JPasswordField pwdTxt; private JTextField verCodeTxt;//验证码 private JButton sureBt; private JButton quitBt; private Mypanel mp; //构造方法 public QQGUI() { Init(); } public void Init() { Frame frame = new Frame("QQ登录"); //创建出控件对象(因为上面只是声明出来,并没有给出实际的空间) //用户文本 userLa = new JLabel(); userLa.setText("用户名:"); userLa.setSize(60, 50); userLa.setLocation(100, 80); //密码文本 pwdLa = new JLabel(); pwdLa.setText("密码:"); pwdLa.setSize(50, 50); pwdLa.setLocation(100, 120); //用户输入框 userTxt = new JTextField(); userTxt.setSize(100, 20); //this.setSize(width, height) userTxt.setLocation(170, 95); //密码输入框 pwdTxt = new JPasswordField(); pwdTxt.setSize(100, 20); pwdTxt.setLocation(170, 135); //确认按钮 sureBt = new JButton("登录"); sureBt.setSize(60, 25); sureBt.setLocation(135, 260); //退出按钮 quitBt = new JButton("退出"); quitBt.setSize(60, 25); quitBt.setLocation(240, 260); //验证码文本 verCodeLa = new JLabel(); verCodeLa.setText("验证码:"); verCodeLa.setSize(60, 50); verCodeLa.setLocation(100,165); //验证码文本框 verCodeTxt = new JTextField(); verCodeTxt.setSize(100, 20); verCodeTxt.setLocation(170, 180); //验证码 mp = new Mypanel(); mp.setSize(100, 30); mp.setLocation(280, 175); //登录方式选择框 JComboBox xlk=new JComboBox(); xlk.setSize(60, 20); xlk.setLocation(250, 220); xlk.addItem("在线"); xlk.addItem("隐身"); xlk.addItem("离开"); this.setLayout(null); this.setSize(500, 400); this.add(userLa); this.add(pwdLa); this.add(userTxt); this.add(pwdTxt); this.add(sureBt); this.add(quitBt); this.add(verCodeLa); this.add(verCodeTxt); this.add(mp); this.add(xlk); sureBt.addActionListener(this); quitBt.addActionListener(this); this.setVisible(true); } //具体事件的处理 public void actionPerformed(ActionEvent e) { //获取产生事件的事件源强制转换 JButton bt = (JButton)e.getSource(); //获取按钮上显示的文本 String str = bt.getText(); if(str.equals("登录")) { if(!CheckIsNull()) { //获取用户所输入的用户名 String user = userTxt.getText().trim(); //获取用户所输入的密码 String pwd = pwdTxt.getText().trim(); if(checkUserAndPwd(user,pwd)) { //隐藏当前登录窗口 this.setVisible(false); //验证成功创建一个主窗口 MainFrame frame = new MainFrame(); } else { //如果错误则弹出一个显示框 JOptionPane pane = new JOptionPane("用户或密码错误"); JDialog dialog = pane.createDialog(this,"警告"); dialog.show(); } } } else { //调用系统类中的一个正常退出 System.exit(0); } } private boolean CheckIsNull() { boolean flag = false; if(userTxt.getText().trim().equals(" ")) { flag = true; } else { if(pwdTxt.getText().trim().equals(" ")) { flag = true; } } return flag; } private boolean checkUserAndPwd(String user,String pwd) { boolean result = false; try { FileReader file = new FileReader("D://Workspaces//MyEclipse 8.5//testGUI.txt"); BufferedReader bre = new BufferedReader(file); String str = bre.readLine(); while(str!=null) { String[] strs = str.split(","); if(strs[0].equals(user)) { if(strs[1].equals(pwd)) result = true; } str = bre.readLine(); } file.close(); }catch(Exception ex) { System.out.print(""); } return result; }}//MainFrame类import javax.swing.*;public class MainFrame extends JFrame { public MainFrame() { this.setSize(300, 300); this.setVisible(true); }}//验证码的生成import java.awt.*;import java.util.*;public class Mypanel extends Panel { public void paint(Graphics g) { int height = 50; int width = 90; //验证码框背景颜色 g.setColor(Color.LIGHT_GRAY); //填充验证码背景 g.fillRect(0, 0, width, height); g.setColor(Color.BLACK); g.drawRect(0, 0, width-1, height-1); Random r = new Random(); //设置干扰点 for(int i = 0;i<100;i++) { int x = r.nextInt(width)-1; int y = r.nextInt(height)-1; g.drawOval(x, y, 2, 2); } g.setFont(new Font("黑体",Font.BOLD,20));//设置验证码字体以及大小 g.setColor(Color.RED);//设置验证码字体颜色 //生成随机验证码 char[] tmp = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); StringBuilder sb = new StringBuilder(); for(int i = 0;i<4;i++) { int pos = r.nextInt(tmp.length); char c = tmp[pos]; sb.append(c + " "); } g.drawString(sb.toString(), 10, 15);//写入验证码 }}//下拉框的实现import java.awt.*;import java.awt.event.*;import javax.swing.*;public class xialakuang extends JFrame { private JComboBox comboBox;//定义一个组合框 public void xia () { //JPanel panel = new JPanel();//创建一个JPanel面板 comboBox = new JComboBox(); comboBox.addItem("在线"); comboBox.addItem("隐身"); comboBox.addItem("离开"); this.add(comboBox); //this.add(panel); this.setSize(200, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }}//测试public class testQQGUI { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub QQGUI frame = new QQGUI(); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。
新闻热点
疑难解答
图片精选