當前位置:首頁 » 編程語言 » java網頁登錄

java網頁登錄

發布時間: 2024-07-28 00:54:09

㈠ 用java語言編程實現一個用戶登錄窗口

方法一:
採用JOptionPane中的一個非常有用的靜態方法 showOptionPane();
源碼如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
public class Login1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
Object[] options = {"登錄","取消"};
int i = JOptionPane.showOptionDialog(null,createLoginPanel(),"登錄信息",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if(i==0) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") && !password.equals("")) {
mainFrame.getContentPane().add(new JLabel("用戶名:"+username+" 密碼是:"+password,JLabel.CENTER));
mainFrame.show();
}
else {
JOptionPane.showMessageDialog(null,"用戶名和密碼不能為空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
else System.exit(0);
}
static JPanel createLoginPanel() {
JPanel ret = new JPanel();

JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用戶名:",JLabel.RIGHT));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 碼:",JLabel.RIGHT));
passwordPanel.add(passwordField);

Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
ret.add(box);

ret.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填寫登錄信息"));
return ret;
}
static JFrame mainFrame = null;
static JTextField usernameField = null;
static JPasswordField passwordField = null;
}
運行:
javac -deprecation Login1.java
java Login
(因為有一個過期的API,所以用了 -deprecation 命令)

方法二,使用了兩個JFrame類共同實現,第一次顯示第一個frame,當點了登錄後且操作合法時,第一個窗口就被釋放了 dispose();再顯示第二個窗口:
源碼如下:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Login2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
//////////////////////////////////////////////////////////////
loginWindow = new JFrame("登錄信息");
loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginWindow.setBounds(350,350,250,200);
loginWindow.setResizable(false);
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用戶名:",JLabel.CENTER));
usernamePanel.add(usernameField);

JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 碼:",JLabel.CENTER));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
JPanel infoPanel = new JPanel();
infoPanel.add(box);
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填寫登錄信息"));
JButton submitButton = new JButton("登錄");
JButton cancelButton = new JButton("取消");
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();

if(!username.equals("") && !password.equals("")) {
loginWindow.dispose();
mainFrame.getContentPane().add(new JLabel("用戶名:"+username+" 密碼是:"+password,JLabel.CENTER));
mainFrame.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"用戶名和密碼不能為空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton); buttonPanel.add(cancelButton);
loginWindow.getContentPane().add(infoPanel,BorderLayout.CENTER);
loginWindow.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.EAST);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.WEST);
loginWindow.setVisible(true);
/////////////////////////////////////////////////////////////////
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
}
static JFrame loginWindow,mainFrame;
static final JTextField usernameField = new JTextField(10);
static final JPasswordField passwordField = new JPasswordField(10);
}
運行:
javac -deprecation Login2.java
java Login2

㈡ java實現簡單登錄界面

自己寫的比較規范的代碼,都有注釋:

import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監聽
import java.awt.event.ActionEvent;//事件處理
import javax.swing.JOptionPane;//消息窗口

public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;

public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}

public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉框架的同時結束程序
this.setSize(300,200);//設置框架大小為長300,寬200
this.setResizable(false);//設置框架不可以改變大小
this.setTitle("用戶登錄");//設置框架標題
this.pnluser.setLayout(null);//設置面板布局管理
this.pnluser.setBackground(Color.cyan);//設置面板背景顏色
this.lbluserLogIn.setText("用戶登錄");//設置標簽標題
this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設置標簽字體
this.lbluserLogIn.setForeground(Color.RED);//設置標簽字體顏色
this.lbluserName.setText("用戶名:");
this.lbluserPWD.setText("密 碼:");
this.btnSub.setText("登錄");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//設置標簽x坐標120,y坐標15,長60,寬20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//載入標簽到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//載入面板到框架
this.setVisible(true);//設置框架可顯
}

public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"賬號不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密碼不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"賬號或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
}

public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}

public static void main(String[] args){
new UserLogIn();
}
}

㈢ 鐢╦ava鍐欎竴涓鐧婚檰鐣岄潰浠g爜銆

姒傝堪

鍏蜂綋妗嗘灦浣跨敤jframe錛屾枃鏈妗嗙粍浠訛細JTextField錛涘瘑鐮佹嗙粍浠訛細JPasswordField錛涙爣絳劇粍浠訛細JLabel錛涘嶉夋嗙粍浠訛細JCheckBox錛涘崟閫夋嗙粍浠訛細JRadioButton錛涙寜閽緇勪歡JButton銆

鐧誨綍鐣岄潰錛

Swing 鏄涓涓涓篔ava璁捐$殑GUI宸ュ叿鍖呫

Swing鏄疛AVA鍩虹綾葷殑涓閮ㄥ垎銆

Swing鍖呮嫭浜嗗浘褰㈢敤鎴風晫闈錛圙UI錛夊櫒浠跺傦細鏂囨湰妗嗭紝鎸夐挳錛屽垎闅旂獥鏍煎拰琛ㄣ

Swing鎻愪緵璁稿氭瘮AWT鏇村ソ鐨勫睆騫曟樉紺哄厓緔犮傚畠浠鐢ㄧ函Java鍐欐垚錛屾墍浠ュ悓Java鏈韜涓鏍峰彲浠ヨ法騫沖彴榪愯岋紝榪欎竴鐐逛笉鍍廇WT銆傚畠浠鏄疛FC鐨勪竴閮ㄥ垎銆傚畠浠鏀鎸佸彲鏇存崲鐨勯潰鏉垮拰涓婚橈紙鍚勭嶆搷浣滅郴緇熼粯璁ょ殑鐗規湁涓婚橈級錛岀劧鑰屼笉鏄鐪熺殑浣跨敤鍘熺敓騫沖彴鎻愪緵鐨勮懼囷紝鑰屾槸浠呬粎鍦ㄨ〃闈涓婃ā浠垮畠浠銆傝繖鎰忓懗鐫浣犲彲浠ュ湪浠繪剰騫沖彴涓婁嬌鐢↗AVA鏀鎸佺殑浠繪剰闈㈡澘銆傝交閲忕駭緇勪歡鐨勭己鐐瑰垯鏄鎵ц岄熷害杈冩參錛屼紭鐐瑰氨鏄鍙浠ュ湪鎵鏈夊鉤鍙頒笂閲囩敤緇熶竴鐨勮屼負銆

姒傚康瑙f瀽錛

JFrame_ java鐨凣UI紼嬪簭鐨勫熀鏈鎬濊礬鏄浠JFrame涓哄熀紜錛屽畠鏄灞忓箷涓妛indow鐨勫硅薄錛岃兘澶熸渶澶у寲銆佹渶灝忓寲銆佸叧闂銆

JPanel_ Java鍥懼艦鐢ㄦ埛鐣岄潰(GUI)宸ュ叿鍖卻wing涓鐨勯潰鏉垮瑰櫒綾伙紝鍖呭惈鍦╦avax.swing 鍖呬腑錛屽彲浠ヨ繘琛屽祵濂楋紝鍔熻兘鏄瀵圭獥浣撲腑鍏鋒湁鐩稿悓閫昏緫鍔熻兘鐨勭粍浠惰繘琛岀粍鍚堬紝鏄涓縐嶈交閲忕駭瀹瑰櫒錛屽彲浠ュ姞鍏ュ埌JFrame紿椾綋涓銆傘

JLabel_ JLabel 瀵硅薄鍙浠ユ樉紺烘枃鏈銆佸浘鍍忔垨鍚屾椂鏄劇ず浜岃呫傚彲浠ラ氳繃璁劇疆鍨傜洿鍜屾按騫沖歸綈鏂瑰紡錛屾寚瀹氭爣絳炬樉紺哄尯涓鏍囩懼唴瀹瑰湪浣曞勫歸綈銆傞粯璁ゆ儏鍐典笅錛屾爣絳懼湪鍏舵樉紺哄尯鍐呭瀭鐩村眳涓瀵歸綈銆傞粯璁ゆ儏鍐典笅錛屽彧鏄劇ず鏂囨湰鐨勬爣絳炬槸寮濮嬭竟瀵歸綈錛涜屽彧鏄劇ず鍥懼儚鐨勬爣絳懼垯姘村鉤灞呬腑瀵歸綈銆

JTextField_涓涓杞婚噺綰х粍浠訛紝瀹冨厑璁哥紪杈戝崟琛屾枃鏈銆

JPasswordField_ 鍏佽告垜浠杈撳叆浜嗕竴琛屽瓧鍍忚緭鍏ユ嗭紝浣嗛殣鈃忔槦鍙(*) 鎴栫偣鍒涘緩瀵嗙爜(瀵嗙爜)

JButton_ JButton 綾葷殑瀹炰緥銆傜敤浜庡壋寤烘寜閽綾諱技瀹炰緥涓鐨 "Login"銆

熱點內容
hotspot源碼 發布:2024-11-26 01:33:05 瀏覽:282
日本電報密碼是多少 發布:2024-11-26 01:33:00 瀏覽:441
雷電緩存目錄 發布:2024-11-26 01:30:05 瀏覽:951
androidcanvas縮放 發布:2024-11-26 01:21:36 瀏覽:239
微信商家掃我為什麼輸入密碼 發布:2024-11-26 01:20:40 瀏覽:661
移動光貓無線路由器密碼多少 發布:2024-11-26 01:18:35 瀏覽:854
怎麼緩存一直播 發布:2024-11-26 01:17:38 瀏覽:448
sql表中添加欄位 發布:2024-11-26 01:17:38 瀏覽:618
安卓的懸浮窗在哪裡開 發布:2024-11-26 01:15:18 瀏覽:431
節點演算法 發布:2024-11-26 01:10:58 瀏覽:370