Java實現(xiàn)簡單GUI登錄和注冊界面
更新時間:2022年04月26日 18:14:42 作者:全?洛
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)簡單GUI登錄和注冊界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java實現(xiàn)簡單GUI登錄和注冊界面的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:
登陸界面:

注冊界面:

實現(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("注冊");
?? ?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最底層上添加兩個帶圖片的標(biāo)簽,并且label2在label上方
?? ??? ?this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
?? ??? ?// 將內(nèi)容面板設(shè)置為透明,就能夠看見添加在LayeredPane上的背景。
?? ??? ?((JPanel) this.getContentPane()).setOpaque(false);
?? ??? ?/*
?? ??? ? * 添加組件到contentPanel容器中 布局方式為自由布局。
?? ??? ? */
?? ??? ?contentPanel.setLayout(null);
?? ??? ?add(admin);
?? ??? ?add(password);
?? ??? ?add(login);
?? ??? ?add(register);
?? ??? ?add(jlb1);
?? ??? ?add(jlb2);
?? ??? ?add(jlbtitle);
?? ??? ?/*
?? ??? ? * 組件絕對位置
?? ??? ? */
?? ??? ?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)聽
?? ? */
?? ?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) {
?? ?}
?? ?// 注冊方法
?? ?public void forRegister() {
?? ?}
}二、注冊界面
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;
/*
?* 注冊窗口
?*/
@SuppressWarnings("serial")
public class AdminRegister extends JFrame {
?? ?private JPanel contentPanel = new JPanel();
?? ?// Label標(biāo)簽存放背景圖片
?? ?private JLabel label;
?? ?// 設(shè)置按鈕組件
?? ?private JButton ok = new JButton("確定注冊"), back = new JButton("返回登錄");
?? ?private JLabel jlb1 = new JLabel("用戶名:"), jlb2 = new JLabel("密碼:"), jlb3 = new JLabel("確認(rèn)密碼:"),
?? ??? ??? ?jlbtitle = new JLabel("注冊界面");
?? ?// 設(shè)置文本框組件
?? ?private JTextField admin = new JTextField(), password1 = new JTextField(), password2 = new JTextField();
?? ?public AdminRegister() {
?? ??? ?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最底層上添加兩個帶圖片的標(biāo)簽,并且label2在label上方
?? ??? ?this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
?? ??? ?// 將內(nèi)容面板設(shè)置為透明,就能夠看見添加在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);
?? ??? ?/*
?? ??? ? * 組件絕對位置
?? ??? ? */
?? ??? ?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)聽
?? ? */
?? ?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);
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?// 實現(xiàn)注冊賬戶方法
?? ?public void setRegister(String admin, String pwd1, String pwd2) {
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Intellij Idea 多模塊Maven工程中模塊之間無法相互引用問題
這篇文章主要介紹了Intellij Idea 多模塊Maven工程中模塊之間無法相互引用問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java解析xml文件和json轉(zhuǎn)換的方法(DOM4j解析)
相信大家都知道Java解析xml的方法有四種,每種方法都很不錯,今天通過本文給大家分享使用DOM4j進(jìn)行解析的方法,文章通過兩種方法給大家進(jìn)行解析,感興趣的朋友一起看看吧2021-08-08
解決Spring boot2.0+配置攔截器攔截靜態(tài)資源的問題
這篇文章主要介紹了解決Spring boot2.0+配置攔截器攔截靜態(tài)資源的問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08

