1. 程式人生 > >如何用java圖形化介面實現一個登入視窗

如何用java圖形化介面實現一個登入視窗

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

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(); } }