Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面
更新時(shí)間:2022年04月26日 18:14:42 作者:全?洛
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:
登陸界面:
注冊(cè)界面:
實(shí)現(xiàn)代碼如下:
一、登陸界面
package cn.bms.view; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.MatteBorder; import cn.bms.tools.GUITools; /* ?* 登錄窗口 ?*/ @SuppressWarnings("serial") public class AdminLogin extends JFrame { ?? ?private JPanel contentPanel = new JPanel(); ?? ?// Label標(biāo)簽存放背景圖片 ?? ?private JLabel label; ?? ?// 設(shè)置按鈕組件 ?? ?private JButton login = new JButton("登錄"), register = new JButton("注冊(cè)"); ?? ?private JLabel jlb1 = new JLabel("用戶名:"), jlb2 = new JLabel("密碼:"), jlbtitle = new JLabel("登錄界面"); ?? ?// 設(shè)置文本框組件 ?? ?private JTextField admin = new JTextField(), password = new JTextField(); ?? ?public AdminLogin() { ?? ??? ?this.init(); ?? ??? ?this.addListener(); ?? ?} ?? ?private void init() { ?? ??? ?this.setTitle("管理員登陸界面"); ?? ??? ?this.setSize(500, 350); ?? ??? ?GUITools.center(this); ?? ??? ?ImageIcon image1 = new ImageIcon("837878.jpg"); // 界面背景圖片 ?? ??? ?JLabel backLabel = new JLabel(); ?? ??? ?backLabel.setIcon(image1); ?? ??? ?label = new JLabel(image1); ?? ??? ?label.setBounds(0, 0, 1000, 400); ?? ??? ?// 在LayeredPane最底層上添加兩個(gè)帶圖片的標(biāo)簽,并且label2在label上方 ?? ??? ?this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); ?? ??? ?// 將內(nèi)容面板設(shè)置為透明,就能夠看見(jiàn)添加在LayeredPane上的背景。 ?? ??? ?((JPanel) this.getContentPane()).setOpaque(false); ?? ??? ?/* ?? ??? ? * 添加組件到contentPanel容器中 布局方式為自由布局。 ?? ??? ? */ ?? ??? ?contentPanel.setLayout(null); ?? ??? ?add(admin); ?? ??? ?add(password); ?? ??? ?add(login); ?? ??? ?add(register); ?? ??? ?add(jlb1); ?? ??? ?add(jlb2); ?? ??? ?add(jlbtitle); ?? ??? ?/* ?? ??? ? * 組件絕對(duì)位置 ?? ??? ? */ ?? ??? ?jlb1.setBounds(50, 130, 90, 25); ?? ??? ?jlb1.setForeground(Color.WHITE); ?? ??? ?admin.setBounds(95, 130, 300, 25); ?? ??? ?password.setBounds(95, 154, 300, 25); ?? ??? ?jlb2.setBounds(50, 154, 90, 25); ?? ??? ?jlb2.setForeground(Color.WHITE); ?? ??? ?register.setBounds(95, 225, 90, 20); ?? ??? ?login.setBounds(315, 225, 90, 20); ?? ??? ?jlbtitle.setBounds(180, 45, 200, 50); ?? ??? ?Font f = new Font("微軟雅黑", Font.BOLD, 30); ?? ??? ?jlbtitle.setFont(f); ?? ??? ?jlbtitle.setForeground(Color.BLUE); ?? ??? ?/* ?? ??? ? * 組件透明化 ?? ??? ? */ ?? ??? ?admin.setOpaque(true); ?? ??? ?password.setOpaque(true); ?? ??? ?contentPanel.setOpaque(false); ?? ??? ?getContentPane().add(contentPanel); ?? ??? ?/* ?? ??? ? * 組件邊框顏色 ?? ??? ? */ ?? ??? ?textSet(admin); ?? ??? ?textSet(password); ?? ?} ?? ?/* ?? ? * JTextField文本框設(shè)置方法. ?? ? */ ?? ?private void textSet(JTextField field) { ?? ??? ?field.setBackground(new Color(255, 255, 255)); ?? ??? ?field.setPreferredSize(new Dimension(150, 28)); ?? ??? ?MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(192, 192, 192)); ?? ??? ?field.setBorder(border); ?? ?} ?? ?/* ?? ? * 事件監(jiān)聽(tīng) ?? ? */ ?? ?private void addListener() { ?? ??? ?login.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?forLogin(admin.getText(), password.getText()); ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?register.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?forRegister(); ?? ??? ??? ?} ?? ??? ?}); ?? ?} ?? ?// 登錄方法 ?? ?public void forLogin(String admin, String pwd) { ?? ?} ?? ?// 注冊(cè)方法 ?? ?public void forRegister() { ?? ?} }
二、注冊(cè)界面
package cn.bms.view; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.MatteBorder; import cn.bms.controller.AdminLoginController; import cn.bms.tools.GUITools; /* ?* 注冊(cè)窗口 ?*/ @SuppressWarnings("serial") public class AdminRegister extends JFrame { ?? ?private JPanel contentPanel = new JPanel(); ?? ?// Label標(biāo)簽存放背景圖片 ?? ?private JLabel label; ?? ?// 設(shè)置按鈕組件 ?? ?private JButton ok = new JButton("確定注冊(cè)"), back = new JButton("返回登錄"); ?? ?private JLabel jlb1 = new JLabel("用戶名:"), jlb2 = new JLabel("密碼:"), jlb3 = new JLabel("確認(rèn)密碼:"), ?? ??? ??? ?jlbtitle = new JLabel("注冊(cè)界面"); ?? ?// 設(shè)置文本框組件 ?? ?private JTextField admin = new JTextField(), password1 = new JTextField(), password2 = new JTextField(); ?? ?public AdminRegister() { ?? ??? ?this.init(); ?? ??? ?this.addListener(); ?? ?} ?? ?private void init() { ?? ??? ?this.setTitle("管理員注冊(cè)界面"); ?? ??? ?this.setSize(500, 350); ?? ??? ?GUITools.center(this); ?? ??? ?ImageIcon image1 = new ImageIcon("837878.jpg"); // 界面背景圖片 ?? ??? ?JLabel backLabel = new JLabel(); ?? ??? ?backLabel.setIcon(image1); ?? ??? ?label = new JLabel(image1); ?? ??? ?label.setBounds(0, 0, 1000, 400); ?? ??? ?// 在LayeredPane最底層上添加兩個(gè)帶圖片的標(biāo)簽,并且label2在label上方 ?? ??? ?this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); ?? ??? ?// 將內(nèi)容面板設(shè)置為透明,就能夠看見(jiàn)添加在LayeredPane上的背景。 ?? ??? ?((JPanel) this.getContentPane()).setOpaque(false); ?? ??? ?/* ?? ??? ? * 添加組件到contentPanel容器中 布局方式為自由布局。 ?? ??? ? */ ?? ??? ?contentPanel.setLayout(null); ?? ??? ?add(admin); ?? ??? ?add(password1); ?? ??? ?add(password2); ?? ??? ?add(ok); ?? ??? ?add(back); ?? ??? ?add(jlb1); ?? ??? ?add(jlb2); ?? ??? ?add(jlb3); ?? ??? ?add(jlbtitle); ?? ??? ?/* ?? ??? ? * 組件絕對(duì)位置 ?? ??? ? */ ?? ??? ?jlb1.setBounds(40, 130, 90, 25); ?? ??? ?jlb1.setForeground(Color.WHITE); ?? ??? ?admin.setBounds(95, 130, 300, 25); ?? ??? ?password1.setBounds(95, 154, 300, 25); ?? ??? ?jlb2.setBounds(40, 154, 90, 25); ?? ??? ?jlb2.setForeground(Color.WHITE); ?? ??? ?password2.setBounds(95, 178, 300, 25); ?? ??? ?jlb3.setBounds(40, 178, 90, 25); ?? ??? ?jlb3.setForeground(Color.WHITE); ?? ??? ?ok.setBounds(315, 225, 90, 20); ?? ??? ?back.setBounds(95, 225, 90, 20); ?? ??? ?jlbtitle.setBounds(180, 45, 200, 50); ?? ??? ?Font f = new Font("微軟雅黑", Font.BOLD, 30); ?? ??? ?jlbtitle.setFont(f); ?? ??? ?jlbtitle.setForeground(Color.BLUE); ?? ??? ?/* ?? ??? ? * 組件透明化 ?? ??? ? */ ?? ??? ?admin.setOpaque(true); ?? ??? ?password1.setOpaque(true); ?? ??? ?password2.setOpaque(true); ?? ??? ?contentPanel.setOpaque(false); ?? ??? ?getContentPane().add(contentPanel); ?? ??? ?/* ?? ??? ? * 組件邊框顏色 ?? ??? ? */ ?? ??? ?textSet(admin); ?? ??? ?textSet(password1); ?? ??? ?textSet(password2); ?? ?} ?? ?/* ?? ? * JTextField文本框設(shè)置方法. ?? ? */ ?? ?private void textSet(JTextField field) { ?? ??? ?field.setBackground(new Color(255, 255, 255)); ?? ??? ?field.setPreferredSize(new Dimension(150, 28)); ?? ??? ?MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(192, 192, 192)); ?? ??? ?field.setBorder(border); ?? ?} ?? ?/* ?? ? * 事件監(jiān)聽(tīng) ?? ? */ ?? ?private void addListener() { ?? ??? ?ok.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?setRegister(admin.getText(), password1.getText(), password2.getText()); ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?back.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?new AdminLoginController().setVisible(true); ?? ??? ??? ?} ?? ??? ?}); ?? ?} ?? ?// 實(shí)現(xiàn)注冊(cè)賬戶方法 ?? ?public void setRegister(String admin, String pwd1, String pwd2) { ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Java+mysql用戶注冊(cè)登錄功能
- JAVA簡(jiǎn)單實(shí)現(xiàn)MD5注冊(cè)登錄加密實(shí)例代碼
- Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
- JavaWeb簡(jiǎn)單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)
- Java簡(jiǎn)易登錄注冊(cè)小程序
- Java簡(jiǎn)易登錄注冊(cè)功能實(shí)現(xiàn)代碼解析
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能
- JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能(服務(wù)器)
- JavaWeb實(shí)戰(zhàn)之用Servlet+JDBC實(shí)現(xiàn)用戶登錄與注冊(cè)
相關(guān)文章
Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題
這篇文章主要介紹了Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java解析xml文件和json轉(zhuǎn)換的方法(DOM4j解析)
相信大家都知道Java解析xml的方法有四種,每種方法都很不錯(cuò),今天通過(guò)本文給大家分享使用DOM4j進(jìn)行解析的方法,文章通過(guò)兩種方法給大家進(jìn)行解析,感興趣的朋友一起看看吧2021-08-08解決Spring boot2.0+配置攔截器攔截靜態(tài)資源的問(wèn)題
這篇文章主要介紹了解決Spring boot2.0+配置攔截器攔截靜態(tài)資源的問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08