java圖形用戶登錄界面_Java簡單登錄圖形界面

一.登錄界面

1.程序代碼

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 import java.awt.*;//導入awt包

2 import javax.swing.*;//導入swing包

3 import java.awt.event.ActionListener;//導入awt包中的監聽器事件包

4 import java.awt.event.ActionEvent;//導入awt包中的ActionEvent事件包

5

6 public class EnterScreen extends JFrame {

7 static int s=0;

8 public EnterScreen() {

9 Yanzhencode vcode = new Yanzhencode();

10 setSize(300,290);//設計窗體的大小

11 setTitle("請登錄");

12 setBackground(Color.RED);//設置背景顏色

13 JLabel a=new JLabel("登錄名"); //實例化JLabel對象

14 JLabel b=new JLabel("密 碼");

15 JLabel g=new JLabel("忘記用戶名/密碼?");

16 JLabel h=new JLabel("驗證碼");

17 g.setForeground(Color.BLUE);

18 JTextField c=new JTextField(15);//實例化用戶名文本框

19 JPasswordField d=new JPasswordField(15);//實例化密碼框

20 JTextField k=new JTextField(4);//實例化驗證碼框

21 d.setEchoChar('*');//將輸入密碼框中的密碼以*顯示出來

22 JButton e=new JButton("登錄");

23 JButton f=new JButton("快速注冊");

24 e.setBackground(Color.YELLOW);//設置登錄按鈕字體顏色

25 f.setForeground(Color.GRAY);//設置快速登錄按鈕填充色

26 setVisible(true);//使窗體可視化

27 Container m=getContentPane();//獲取一個容器

28 getContentPane().setBackground(Color.WHITE);//設置窗體填充色

29 // 將用戶名、密碼的Jlabel和用戶名JTextField文本框、密碼JPasswordField密碼框以及確定JButton、快速注冊JButton添加到container容器里面 //

30 m.add(a);

31 m.add(b);

32 m.add(c);

33 m.add(d);

34 m.add(e);

35 m.add(f);

36 m.add(g);

37 m.add(h);

38 m.add(k);

39 m.add(vcode);

40 setBounds(300,300,300,300);//設置窗體的長寬各為300、300 讓其顯示在左上方的300、300處

41 m.setLayout(null);

42 // a、b、c、d、e、f顯示在container容器中的位置坐標

43 a.setBounds(10,40,50,18);

44 b.setBounds(10,80,50,18);

45 c.setBounds(60,40,130,18);

46 d.setBounds(60,80,130,18);

47 h.setBounds(10,120,50,18);

48 k.setBounds(60,120,80,18);

49 e.setBounds(90,180,100,30);

50 f.setBounds(90,220,100,30);

51 g.setBounds(190,75,100,30);

52 vcode.setBounds(140,110,80,30);

53 e.addActionListener(new ActionListener() {//對登錄按鈕添加監聽事件

54

55 @SuppressWarnings("deprecation")

56 @Override

57 public void actionPerformed(ActionEvent arg0) {

58 // TODO Auto-generated method stub

59

60 if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==1) {//equals函數進行用戶名和密碼的匹配

61 JOptionPane.showMessageDialog(null,"登錄成功");

62

63 new NewFrame();//進入到NewFrame這個窗體中

64 }else if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==0) {

65 JOptionPane.showMessageDialog(null,"驗證碼輸入錯誤");

66 }else {

67 JOptionPane.showMessageDialog(null, "登錄失敗,用戶名、密碼或驗證碼輸入錯誤");

68 }

69 }

70

71 });

72 f.addActionListener(new ActionListener(){//對快速注冊按鈕添加監聽事件

73 @SuppressWarnings("deprecation")

74 @Override

75 public void actionPerformed(ActionEvent arg0) {

76 // TODO Auto-generated method stub

77 new zhuce();//進入都到zhuce窗體中

78 }

79

80 });

81 //判斷輸入驗證碼是否正確

82 if(k.getText()== null) {

83 s=0;

84 }else if(vcode.getCode() == null) {

85 s=1;

86 }else if(vcode.getCode() .equals(k.getText())) {

87 s=1;

88 }else {

89 s=0;

90 }

91 }

92

93 public static void main(String[] args) {

94 new EnterScreen();

95

96 }

97

98 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

2.登錄界面截圖

25e332bbfad2916c188f79d8b276fb84.png

2c91a7412b067c9c2fc99c30dd5594eb.png

二.快速注冊界面

1.程序代碼

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 import java.awt.*;

2 import javax.swing.*;

3 import java.awt.event.ActionListener;

4 import java.awt.event.ActionEvent;

5 import java.sql.*;

6 import java.awt.Container;

7

8 import javax.swing.JButton;

9 import javax.swing.JLabel;

10 import javax.swing.JPasswordField;

11 import javax.swing.JTextField;

12

13 public class zhuce extends JFrame {

14 public zhuce() {

15 setSize(300,250);//設計窗體的大小

16 JLabel a=new JLabel("用戶名"); //實例化JLabel對象

17 JLabel b=new JLabel("密 碼");

18 JLabel h=new JLabel("再次確認密碼");

19 JTextField c=new JTextField(15);//實例化用戶名文本框

20 JPasswordField d=new JPasswordField(15);//實例化密碼文本框

21 JPasswordField hd=new JPasswordField(15);//實例化密碼文本框

22 d.setEchoChar('*');//設置密碼隱藏制度

23 JButton e=new JButton("確定");

24 JButton f=new JButton("重置");

25 JButton g=new JButton("返回");

26 setVisible(true);

27 //獲取一個容器

28 Container m=getContentPane();

29 // 將用戶名、密碼的Jlabel和用戶名JTextField文本框、密碼JPasswordField密碼框以及確定JButton、快速注冊JButton添加到container容器里面

30 m.add(a);

31 m.add(b);

32 m.add(h);

33 m.add(hd);

34 m.add(c);

35 m.add(d);

36 m.add(e);

37 m.add(f);

38 m.add(g);

39 //設置窗體的長寬各為300、250 讓其顯示在左上方的300、250處

40 setBounds(300,250,300,250);

41 m.setLayout(null);

42 // a、b、c、d、e、f顯示在container容器中的位置坐標

43 a.setBounds(10,40,50,18);

44 b.setBounds(10,80,50,18);

45 h.setBounds(5,120,80,18);

46 c.setBounds(60,40,200,18);

47 d.setBounds(60,80,200,18);

48 hd.setBounds(90,120,180,18);

49 e.setBounds(110,160,60,30);

50 f.setBounds(30,160,60,30);

51 g.setBounds(190,160,60,30);

52 g.addActionListener(new ActionListener() {//對返回按鈕添加監聽事件

53 @SuppressWarnings("deprecation")

54 @Override

55 public void actionPerformed(ActionEvent arg0) {

56 // TODO Auto-generated method stub

57 new EnterScreen();

58

59 }

60 });

61 f.addActionListener(new ActionListener() {//對確認按鈕添加監聽事件

62 @SuppressWarnings("deprecation")

63 @Override

64 public void actionPerformed(ActionEvent arg0) {

65 // TODO Auto-generated method stub

66 new EnterScreen();

67

68 }

69 });

70 e.addActionListener(new ActionListener() {//對確認按鈕添加監聽事件

71 @SuppressWarnings("deprecation")

72 @Override

73 public void actionPerformed(ActionEvent arg0) {

74 // TODO Auto-generated method stub

75 new zhuce();

76

77 }

78 });

79 }

80 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

2.快速注冊界面截圖

7bd68c996bd5d5b1a0e28e3f858048ff.png

三.驗證碼部分

1.驗證碼程序

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 import java.awt.Color;

2 import java.awt.Dimension;

3 import java.awt.Font;

4 import java.awt.FontMetrics;

5 import java.awt.Graphics;

6 import java.awt.Graphics2D;

7 import java.awt.event.MouseEvent;

8 import java.awt.event.MouseListener;

9

10 import java.util.Random;

11

12 import javax.swing.JComponent;

13

14 public class Yanzhencode extends JComponent implements MouseListener {

15

16 private String codes; //自動生成的驗證碼

17

18 private int width, height = 40; //設置驗證碼高度、寬度

19

20 private int codesLength = 4; //設置代碼長度

21

22 private Random random = new Random(); //生成數字的方法

23

24 public Yanzhencode() {

25 width = this.codesLength * 16 + (this.codesLength - 1) * 10; //根據驗證碼長度設置寬度

26 setPreferredSize(new Dimension(width, height)); //設置背景大小

27 setSize(width, height); //設置驗證碼長度和寬度

28 this.addMouseListener(this);

29 setToolTipText("點擊可更換驗證碼");

30 }

31 //得到生成的驗證碼

32 public int getCodesLength() {

33 return codesLength;

34 }

35

36

37 //設置驗證碼的長度

38 public void setCodesLength(int codeLength) {

39 if(codesLength < 4) {

40 this.codesLength = 4;

41 } else {

42 this.codesLength = codeLength;

43 }

44

45 }

46

47 public String getCode() {

48 return codes;

49 }

50

51

52 //讓驗證碼產生隨機的顏色

53 public Color getRandColor(int min, int max) {

54

55 if (min > 255)

56 min = 255;

57 if (max > 255)

58 max = 255;

59 int red = random.nextInt(max - min) + min;

60 int green = random.nextInt(max - min) + min;

61 int blue = random.nextInt(max - min) + min;

62 return new Color(red, green, blue);

63 }

64 // 設置驗證碼具體的數字或字母是什么

65 protected String generateCode() {

66 char[] codes = new char[this.codesLength];

67 for (int i = 0, len = codes.length; i < len; i++) {

68 if (random.nextBoolean()) {

69 codes[i] = (char) (random.nextInt(10) + 48);

70 } else {

71 codes[i] = (char) (random.nextInt(26) + 97);

72 }

73 }

74 this.codes = new String(codes);

75 return this.codes;

76 }

77

78

79 @Override

80 protected void paintComponent(Graphics g) {

81 super.paintComponent(g);

82 if(this.codes == null || this.codes.length() != this.codesLength) { //判斷生成的驗證碼是否為空或超出長度

83 this.codes = generateCode();

84 }

85 width = this.codesLength * 16 + (this.codesLength - 1) * 10;

86 super.setSize(width, height); //接口使用,驗證碼字體大小

87 super.setPreferredSize(new Dimension(width, height));//接口使用,驗證碼背景大小

88 Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //設置字體和字體大小

89 g.setFont(mFont); //設置對象

90 //繪制出驗證碼的背景的矩形輪廓

91 Graphics2D g2d = (Graphics2D) g;

92 g2d.setColor(getRandColor(200, 250));

93 g2d.fillRect(0, 0, width, height);

94 g2d.setColor(getRandColor(180, 200));

95 g2d.drawRect(0, 0, width - 1, height - 1);

96 //繪制出驗證碼背景的線

97 int i = 0, len = 150;

98 for (; i < len; i++) {

99 int x = random.nextInt(width - 1);

100 int y = random.nextInt(height - 1);

101 int x1 = random.nextInt(width - 10) + 10;

102 int y1 = random.nextInt(height - 4) + 4;

103 g2d.setColor(getRandColor(180, 200));

104 g2d.drawLine(x, y, x1, y1);

105 }

106

107

108

109 //繪制出驗證碼的具體字母

110 i = 0; len = this.codesLength;

111 FontMetrics fm = g2d.getFontMetrics();

112 int base = (height - fm.getHeight())/2 + fm.getAscent();

113 for(;i

114 int b = random.nextBoolean() ? 1 : -1;

115 g2d.rotate(random.nextInt(10)*0.01*b);

116 g2d.setColor(getRandColor(20, 130));

117 g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base);

118 }

119 }

120

121 //下一個驗證碼

122 public void nextCode() {

123 generateCode();

124 repaint();

125 }

126

127 @Override

128 public void mouseClicked(MouseEvent e) {

129

130 nextCode();

131 }

132

133 @Override

134 public void mousePressed(MouseEvent e) {

135 // TODO Auto-generated method stub

136

137 }

138

139 @Override

140 public void mouseReleased(MouseEvent e) {

141 // TODO Auto-generated method stub

142

143 }

144

145 @Override

146 public void mouseEntered(MouseEvent e) {

147 // TODO Auto-generated method stub

148

149 }

150

151 @Override

152 public void mouseExited(MouseEvent e) {

153 // TODO Auto-generated method stub

154

155 }

156 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

2.驗證碼實現效果

51e56c6a91da9592a4db0b1aaedceee5.png

點擊驗證碼圖片后,驗證碼會更換

1ecf39095e55cdcfb80abc9561c3c891.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445807.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445807.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445807.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

北大青鳥java y2_Struts-2 北大青鳥 Y2學年 項目案例使用 2框架開發租房網站 Java Develop 249萬源代碼下載- www.pudn.com...

文件名稱: Struts-2下載 收藏√ [5 4 3 2 1 ]開發工具: Java文件大小: 10225 KB上傳時間: 2016-01-03下載次數: 0提 供 者: 姜鵬詳細說明&#xff1a;北大青鳥 Y2學年 項目案例使用Struts 2框架開發租房網站-My English LOW文件列表(點擊判斷是否您需要的文件&#xff0c…

java int 包_int readInt()

int readInt()描述 (Description)java.io.ObjectInputStream.readInt()方法讀取32位int。聲明 (Declaration)以下是java.io.ObjectInputStream.readInt()方法的聲明。public int readInt()參數 (Parameters)NA返回值 (Return Value)此方法不返回值。異常 (Exception)EOFExcepti…

java i o是什么流_Java I/O流的總結

I/O的類結構圖I/O的分類根據處理的數據類型分為&#xff1a;字節流和字符流。根據數據流向分為&#xff1a;輸入流和輸出流。流又可分為節點流和處理流。節點流直接與數據源相連處理流與節點流一起使用&#xff0c;在節點流的基礎上&#xff0c;再嵌套一層。提高文件的讀取效率…

java i18n實例_Java國際化(i18n)格式化日期

本篇文章幫大家學習java國際化(i18n)格式化日期&#xff0c;包含了Java國際化(i18n)格式化日期使用方法、操作技巧、實例演示和注意事項&#xff0c;有一定的學習價值&#xff0c;大家可以用來參考。DateFormat類提供了各種格式來格式化日期。 以下是一些格式的列表。DateForma…

java placeholder_java – 如何在JTextfield中設置像Placeholder一樣的文本

我用來覆蓋文本字段繪制方法,直到我最終得到更多的自定義文本字段然后我真的想…然后我發現this prompt API易于使用,不需要你擴展任何組件.它還有一個很好的“伙伴”API這已經被包含在SwingLabs,SwingX library中,這使得它甚至可以使用……例如(這使用SwingX-1.6.4)import jav…

java web聊天室私聊map_java websocket聊天室示例(springboot)

【實例簡介】【實例截圖】【核心代碼】package com.example.demo;import java.io.IOException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.ConcurrentHashMap;import javax.websocket.OnClose;import …

Java 內存映射讀取文件_Java內存映射 大文件輕松處理|chu

前言內存映射文件(Memory-mapped File)&#xff0c;指的是將一段虛擬內存逐字節映射于一個文件&#xff0c;使得應用程序處理文件如同訪問主內存(但在真正使用到這些數據前卻不會消耗物理內存&#xff0c;也不會有讀寫磁盤的操作)&#xff0c;這要比直接文件讀寫快幾個數量級。…

LeetCode - Easy - 118. Pascal‘s Triangle

Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle. In Pascal’s triangle, each number is the sum of the two numbers directly above it. Example…

LeetCode - Easy - 119. Pascal‘s Triangle II

Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex, return the rowIndexth row of the Pascal’s triangle. Notice that the row index starts from 0. In Pascal’s triangle, each number is the sum of the tw…

jenv java_mac 上使用jenv 管理的多個java 版本

由于服務器是java1.7&#xff0c; mac上是1.8&#xff0c;因此mac編譯的java代碼會在服務器上報錯。因此&#xff0c;需要修改mac上java版本&#xff0c;自己折騰了很久&#xff0c;放棄&#xff0c;決定使用jenv 管理&#xff01; 結果是非常方便使用步驟&#xff1a;1、安裝 …

mysql 源碼 緩存_MySQL源碼:MYSQL存儲過程/函數的分析原理及緩存機制

前言&#xff1a;我個人認為&#xff0c;有關MYSQL存儲過程/函數在MYSQL中的實現比較粗糙&#xff0c;可擴展性不夠好&#xff0c;其實現的耦合性太高&#xff0c;所以主要講一些它的原理方面的內容&#xff0c;但有可能在某些方面理解不夠好或者有些不正確的地方&#xff0c;歡…

如何單元測試Java的private方法

問題 Java類中private方法通常只能被其所屬類的調用&#xff0c;其他類只能望而卻步&#xff0c;單元測試private方法也就一籌莫展。 嘗試解法&#xff1a; 在測試時&#xff0c;手動將private改為public&#xff0c;測試完后再將其改回。將測試方法寫進private方法的所屬類…

圖論與java_算法筆記_150:圖論之雙連通及橋的應用(Java)

1 問題描述DescriptionIn order to get from one of the F (1 < F < 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often bein…

如何用JUnit單元測試List

問題 JUnit測試List時差強人意。 解法 引入依賴 hamcrest-library包含許多有用方法來測試List數據類型。 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>&l…

java數據包解析_請教http請求數據包如何解析 重組

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓下面是我捕獲到的請求報文片段dst_ip:/121.52.228.134ack:trueack_num:3064957366date:POST /messagebroker/amf HTTP/1.1Host: s16.xxhzw.game.yy.comUser-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/…

webqq java_WebQQ登錄詳解

第二次登錄請求方式:POST地址:http://d.web2.qq.com/channel/login2POST正文:r%7B%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22{0}%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22{1}%22%2C%22  psessionid%22%3Anull%7D&clientid{2}&psessionidnull…

LeetCode - Easy - 155. Min Stack

Topic StackDesign Description https://leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack.pop() – Removes the element on top of the st…

java judgefilecode_VScode出現無法打開“X”: 找不到文件(file:///XXXX) 的解決辦法

如標題&#xff0c;被這個問題整了好長時間了&#xff0c;調試的時候如果有語法錯誤只能顯示相應的的行數&#xff0c;沒有辦法定位到出錯的行數上。(由于用處不是很大并且沒有找到解決辦法&#xff0c;所以就一直放著沒管23333)直到最近看到一位大佬的解決辦(重寫正則表達式)法…

LeetCode - Easy - 169. Majority Element

Topic ArrayDivide and ConquerBit Manipulation Description https://leetcode.com/problems/majority-element/ Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume t…

java 靜態方法 異常_java空指針異常與靜態方法

從一道經典面試題說起&#xff0c;public class HaHa {public static void haha(){System.out.println("haha");}public static void main(String[] args){((HaHa)null).haha();}}打印結果 haha。這段題考查兩點知識&#xff0c;java的空指針異常和靜態方法。1&#…