欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年05月15日 08:48:10   作者:鯨魚姑娘  
這篇文章主要為大家詳細介紹了java圖形化界面實現(xiàn)登錄窗口,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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登錄");

  //創(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);

  //確認按鈕
  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);
      //驗證成功創(chuàng)建一個主窗口
      MainFrame frame = new MainFrame();
     }
     else
     {
      //如果錯誤則彈出一個顯示框
      JOptionPane pane = new JOptionPane("用戶或密碼錯誤");
      JDialog dialog = pane.createDialog(this,"警告");
      dialog.show();
     }
    }
   }
   else
   {
    //調用系統(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();
  //設置干擾點
  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);//寫入驗證碼
 }
}

//下拉框的實現(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();
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • springmvc常用注解標簽詳解

    springmvc常用注解標簽詳解

    本篇文章主要介紹了springmvc常用注解標簽詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot集成JPA持久層框架,簡化數(shù)據(jù)庫操作

    SpringBoot集成JPA持久層框架,簡化數(shù)據(jù)庫操作

    JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化規(guī)范。主要是為了簡化持久層開發(fā)以及整合ORM技術,結束Hibernate、TopLink、JDO等ORM框架各自為營的局面。JPA是在吸收現(xiàn)有ORM框架的基礎上發(fā)展而來,易于使用,伸縮性強。
    2021-06-06
  • 一篇文章帶你入門Java?UML的類圖

    一篇文章帶你入門Java?UML的類圖

    這篇文章主要為大家詳細介紹了Java?UML的類圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java實現(xiàn)學生信息管理系統(tǒng)(使用數(shù)據(jù)庫)

    Java實現(xiàn)學生信息管理系統(tǒng)(使用數(shù)據(jù)庫)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)學生信息管理系統(tǒng),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java 動態(tài)生成類和實例, 并注入方法操作示例

    Java 動態(tài)生成類和實例, 并注入方法操作示例

    這篇文章主要介紹了Java 動態(tài)生成類和實例, 并注入方法操作,結合實例形式分析了Java 動態(tài)生成類和實例以及動態(tài)注入相關操作技巧,需要的朋友可以參考下
    2020-02-02
  • java ant包中的org.apache.tools.zip實現(xiàn)壓縮和解壓縮實例詳解

    java ant包中的org.apache.tools.zip實現(xiàn)壓縮和解壓縮實例詳解

    這篇文章主要介紹了java ant包中的org.apache.tools.zip實現(xiàn)壓縮和解壓縮實例詳解的相關資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot--Banner的定制和關閉操作

    SpringBoot--Banner的定制和關閉操作

    這篇文章主要介紹了SpringBoot--Banner的定制和關閉操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2018-05-05
  • 深入學習Java 動態(tài)代理

    深入學習Java 動態(tài)代理

    Java 動態(tài)代理機制的出現(xiàn),使得 Java 開發(fā)人員不用手工編寫代理類,只要簡單地指定一組接口及委托類對象,便能動態(tài)地獲得代理類。下面小編和大家來一起學習一下吧
    2019-05-05
  • Springboot中@ConfigurationProperties輕松管理應用程序的配置信息詳解

    Springboot中@ConfigurationProperties輕松管理應用程序的配置信息詳解

    通過@ConfigurationProperties注解,可以將外部配置文件中的屬性值注入到JavaBean中,簡化了配置屬性的讀取和管理,這使得SpringBoot應用程序中配置文件的屬性值可以映射到POJO類中,實現(xiàn)類型安全的屬性訪問,此方法避免了手動讀取配置文件屬性的需要
    2024-10-10
  • SpringBoot上傳文件到本服務器 目錄與jar包同級問題

    SpringBoot上傳文件到本服務器 目錄與jar包同級問題

    這篇文章主要介紹了SpringBoot上傳文件到本服務器 目錄與jar包同級問題,需要的朋友可以參考下
    2018-11-11

最新評論