国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java圖形化界面實現登錄窗口

2024-07-14 08:40:48
字體:
來源:轉載
供稿:網友

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

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武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 磐安县| 贺州市| 搜索| 武城县| 吉木乃县| 敖汉旗| 虞城县| 泽州县| 万盛区| 静乐县| 恭城| 大安市| 陆良县| 唐海县| 斗六市| 永城市| 西青区| 上虞市| 涟水县| 林甸县| 依安县| 土默特左旗| 红原县| 新郑市| 山西省| 红河县| 南丹县| 吴堡县| 拜泉县| 商河县| 大方县| 汾西县| 报价| 大埔区| 延安市| 雷波县| 凤台县| 陵川县| 故城县| 张北县| 汝阳县|