java用戶登錄界面
1. 如何用java編寫一個簡單用戶登陸界面
什麼都不說了 直接給你代碼吧
package com.moliying.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登錄");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("確定");
private JButton cancel = new JButton("取消");
public Login() {
frame.setSize(300, 200);
frame.setBounds(450, 300, 300, 200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
// 頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系統管理員登錄"));
c.add(titlePanel, "North");
// 中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel a1 = new JLabel("用戶名:");
a1.setBounds(50, 20, 50, 20);
JLabel a2 = new JLabel("密 碼:");
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(110, 20, 120, 20);
password.setBounds(110, 60, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel, "Center");
// 底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel, "South");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(username.getText().toString());
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
}
public static void main(String[] args) {
// new Login();
String ss = "abbabbbaabbbccba";
System.out.println(ss.split("b").length);
}
}
2. 用java語言寫一個用戶登陸界面
//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.sql.*;
//throwsException
{
JLabellbl1=newJLabel("用戶名:");
JLabellbl2=newJLabel("密碼:");
JTextFieldtxt=newJTextField(5);
JPasswordFieldpf=newJPasswordField();
JButtonbtn1=newJButton("確定");
JButtonbtn2=newJButton("取消");
publicLoginFrm(){
this.setTitle("登陸");
JPaneljp=(JPanel)this.getContentPane();
jp.setLayout(newGridLayout(3,2,5,5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==btn1){
try{
Class.forName("com.mysql.jdbc.Driver");//mysql資料庫
Connectioncon=DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl","root","1");//資料庫名為Car_zl,密碼為1
System.out.println("com:"+con);
Statementcmd=con.createStatement();
Stringsql="select*fromuserwhereUser_ID='"
+txt.getText()+"'andUser_ps='"
+pf.getText()+"'";
ResultSetrs=cmd
.executeQuery(sql);//表名為user,user_ID和User_ps是存放用戶名和密碼的欄位名
if(rs.next()){
JOptionPane.showMessageDialog(null,"登陸成功!");
}else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤!");
}catch(Exceptionex){
}
if(ae.getSource()==btn2){
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
publicstaticvoidmain(Stringarg[]){
JFrame.(true);
LoginFrmfrm=newLoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
3. 登陸界面的java代碼怎麼寫
概述
具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:
Swing 是一個為Java設計的GUI工具包。
Swing是JAVA基礎類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平台提供的設備,而是僅僅在表面上模仿它們。這意味著你可以在任意平台上使用JAVA支持的任意麵板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平台上採用統一的行為。
概念解析:
JFrame– java的GUI程序的基本思路是以JFrame為基礎,它是屏幕上window的對象,能夠最大化、最小化、關閉。
JPanel– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。
JLabel– JLabel 對象可以顯示文本、圖像或同時顯示二者。可以通過設置垂直和水平對齊方式,指定標簽顯示區中標簽內容在何處對齊。默認情況下,標簽在其顯示區內垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。
JTextField–一個輕量級組件,它允許編輯單行文本。
JPasswordField– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創建密碼(密碼)
JButton– JButton 類的實例。用於創建按鈕類似實例中的 "Login"。
4. Java編程:登陸界面
publicclassLoginextendsjavax.swing.JFrame{
/**CreatesnewformLogin*/
publicLogin(){
initComponents();
}
/**
*initializetheform.
*WARNING:DoNOTmodifythiscode.Thecontentofthismethodis
*.
*/
//GEN-BEGIN:initComponents
//<editor-folddefaultstate="collapsed"desc="GeneratedCode">
privatevoidinitComponents(){
jPanel1=newjavax.swing.JPanel();
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jTextField1=newjavax.swing.JTextField();
jLabel3=newjavax.swing.JLabel();
jPasswordField1=newjavax.swing.JPasswordField();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("u7528u6237u767bu5f55");
setBounds(newjava.awt.Rectangle(0,0,180,220));
setResizable(false);
jLabel1.setText("");
jLabel2.setText("u5e10u53f7");
jLabel3.setText("u5bc6u7801");
jButton1.setText("u786eu5b9a");
jButton1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton1ActionPerformed(evt);
}
});
jButton2.setText("u53d6u6d88");
jButton2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayoutjPanel1Layout=newjavax.swing.GroupLayout(
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(
31,
31,
31)
.addComponent(
jLabel1))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout
.createSequentialGroup()
.addComponent(
jButton1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
jButton2))
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout
.createSequentialGroup()
.addComponent(
jLabel3)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jPasswordField1))
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout
.createSequentialGroup()
.addComponent(
jLabel2)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
108,
javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(12,Short.MAX_VALUE)));
jPanel1Layout
.setVerticalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jPasswordField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18,18,18)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(66,Short.MAX_VALUE)));
javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE));
pack();
}//</editor-fold>
//GEN-END:initComponents
(java.awt.event.ActionEventevt){
//TODOaddyourhandlingcodehere:
System.exit(0);
}
(java.awt.event.ActionEventevt){
//TODOaddyourhandlingcodehere:
Stringname=jTextField1.getText();
Stringpass=newString(jPasswordField1.getPassword());
System.out.println(name);
System.out.println(pass);
if(name.equals("Java")&&pass.equals("123456")){
jLabel1.setText("登錄成功!");
}else{
jLabel1.setText("登錄失敗!");
}
}
/**
*@
*/
publicstaticvoidmain(Stringargs[]){
java.awt.EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
newLogin().setVisible(true);
}
});
}
//GEN-BEGIN:variables
//Variablesdeclaration-donotmodify
privatejavax.swing.JButtonjButton1;
privatejavax.swing.JButtonjButton2;
privatejavax.swing.JLabeljLabel1;
privatejavax.swing.JLabeljLabel2;
privatejavax.swing.JLabeljLabel3;
privatejavax.swing.JPaneljPanel1;
privatejavax.swing.JPasswordFieldjPasswordField1;
privatejavax.swing.JTextFieldjTextField1;
//Endofvariablesdeclaration//GEN-END:variables
}
5. 如何用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();
}
}
6. JAVA用戶登錄界面的編程思想和方法是什麼
登錄界面一般涉及登錄 退出
一般登錄使用用戶名和密碼,使用資料庫校驗即可(密碼可能加密)
校驗通過即成功
另外,為了鑒權和避免重復校驗,一般登錄會返回一個時效並包含登錄信息的token,根據token可直接登錄
此時,退出操作,就是刪除此token;
jwt可以參考下
7. Java製作一個用戶登錄的窗口
Java用戶登陸這塊,主要還是類:
1,邊界布局:BorderLayout。他主要分為五個布局,是JFrame(頂層容器),JDialog(創建對話框窗口的類)的默認布局方式。其最多容量為5個組件,超出5個得用其他的。設置方式為:BorderLayout.NORTH;BorderLayout.SOUTH;BorderLayout.CENTER;Borderlayout.CENTER;BorderLayout.LEFT;BorderLayout.RIGHT。
2,流式布局:FlowLayout。布局方式為從左到右,從上到下。是JPanel(輕量級容器)的默認面板布局。
3,網格布局:GridLayout。布局方式為行和列組成的網路。布局方法:setLayout(new
GridLayout(3,2,3,3));其中強兩位數字表示三行兩列,後兩位表示行與行的間距為3,列與列的間距為3.
接著,就接觸到JPanel面板。JPanel是非頂層容器,所以,一個界面只能由一個JFrame,但是可以有多個JPanel組件。其默認布局方式為流式布局。在JPanel這塊,學到了用戶登錄界面的設計。從而接觸到另外三個組件:文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
8. 求JAVA實現用戶登錄界面代碼
求麥克實現登錄用戶密碼,電費就點擊x加y就可以了。
9. java用戶登錄界面的設計
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
public static void main(String[] args) {
new Frame();
}
public Frame() throws HeadlessException {
Container contentPanel = this.getContentPane();
JPanel headerPanel = new JPanel();
headerPanel.setLayout(new FlowLayout());
headerPanel.add(new JLabel("歡迎進入學生成績管理系統"));
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(2, 2));
centerPanel.add(new JLabel("用戶名", JLabel.CENTER));
centerPanel.add(new JTextField());
centerPanel.add(new JLabel("密碼", JLabel.CENTER));
centerPanel.add(new JTextField());
JPanel footerPanel = new JPanel();
footerPanel.setLayout(new FlowLayout());
footerPanel.add(new JButton("登錄"));
footerPanel.add(new JButton("取消"));
contentPanel.add(headerPanel, BorderLayout.NORTH);
contentPanel.add(centerPanel, BorderLayout.CENTER);
contentPanel.add(footerPanel, BorderLayout.SOUTH);
this.setTitle("Login");
this.setBounds(0, 0, 300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
10. 用java程序編寫一個簡單的登錄界面怎麼寫
程序如下:
mport java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密 碼:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登錄");
JPanel pnl = new JPanel();
private int error = 0;
public MainFrame(String title) throws HeadlessException {
super(title);
init();
}
private void init() {
this.setResizable(false);
pwd.setEchoChar('*');
pnl.add(lbl1);
pnl.add(txt);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equal花憨羔窖薏忌割媳公顱s(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error < 3){
JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
public static void main(String[] args) {
MainFrame frm = new MainFrame("測試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}