溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java圖形化界面實現(xiàn)登錄窗口

發(fā)布時間:2020-09-16 11:26:27 來源:腳本之家 閱讀:192 作者:鯨魚姑娘 欄目:編程語言

登錄窗口一般很常見,現(xiàn)在讓我們自己也來寫一個吧!

PS:很多import是重復(fù)的,是因為我是分了幾個類寫的,必須單獨導(dǎo)入

//模擬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;

 //構(gòu)造方法
 public QQGUI()
 {
  Init();
 }
 public void Init()
 {
  Frame frame = new Frame("QQ登錄");

  //創(chuàng)建出控件對象(因為上面只是聲明出來,并沒有給出實際的空間)

  //用戶文本
  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);

  //確認(rèn)按鈕
  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)
  {
   //獲取產(chǎn)生事件的事件源強制轉(zhuǎn)換
   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))
     {

      //隱藏當(dāng)前登錄窗口
      this.setVisible(false);
      //驗證成功創(chuàng)建一個主窗口
      MainFrame frame = new MainFrame();
     }
     else
     {
      //如果錯誤則彈出一個顯示框
      JOptionPane pane = new JOptionPane("用戶或密碼錯誤");
      JDialog dialog = pane.createDialog(this,"警告");
      dialog.show();
     }
    }
   }
   else
   {
    //調(diào)用系統(tǒng)類中的一個正常退出
    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();
  //設(shè)置干擾點
  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));//設(shè)置驗證碼字體以及大小
  g.setColor(Color.RED);//設(shè)置驗證碼字體顏色
  //生成隨機驗證碼
  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);//寫入驗證碼
 }
}

//下拉框的實現(xiàn)
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();//創(chuàng)建一個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();
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI