java圖形化界面實現(xiàn)登錄窗口
更新時間:2018年05月15日 08:48:10 作者:鯨魚姑娘
這篇文章主要為大家詳細(xì)介紹了java圖形化界面實現(xiàn)登錄窗口,具有一定的參考價值,感興趣的小伙伴們可以參考一下
登錄窗口一般很常見,現(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC中Json數(shù)據(jù)格式轉(zhuǎn)換
本文主要介紹了SpringMVC中Json數(shù)據(jù)格式轉(zhuǎn)換的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Spring中配置和讀取多個Properties文件的方式方法
本篇文章主要介紹了Spring中配置和讀取多個Properties文件的方式方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例
這篇文章主要介紹了java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-10-10
java中Sources目錄Resources目錄的區(qū)別解讀
這篇文章主要介紹了java中Sources目錄Resources目錄的區(qū)別解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
SpringCloud Zuul在何種情況下使用Hystrix及問題小結(jié)
這篇文章主要介紹了SpringCloud Zuul在何種情況下使用Hystrix 及問題小結(jié),感興趣的朋友跟隨小編一起看看吧2018-11-11
關(guān)于HashMap相同key累加value的問題
這篇文章主要介紹了關(guān)于HashMap相同key累加value的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

