1. 求用java編寫的登陸界面!

這是我剛做的,JAVA布局與其它語言相比難度較大,建議你將兩張圖片PS成一張圖片,做為背景,加入到JFRAME中,以降低難度。

要是這個你喜歡,不要忘記多給我加分啊!!!

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.border.*;

{

JTextFieldf1;

JTextFieldf2;

JButtonb1;

JButtonb2;

Stringpower;//表示許可權

StringimgePath="e:/workspace/GUI/14.gif";

JPanelp5;

Imageimg=Toolkit.getDefaultToolkit().createImage(imgePath);

LoginIn(){

Containercp=getContentPane();

Labell1=newLabel("用戶:");

Labell2=newLabel("密碼:");

JPanelp1=newJPanel();

JPanelp2=newJPanel();

JPanelp3=newJPanel();

JPanelp4=newJPanel();

p5=newJPanel(){

protectedvoidpaintChildren(Graphicsg){

g.drawImage(img,0,0,this);

super.paintChildren(g);

}

};

f1=newJTextField(15);

f2=newJPasswordField(15);

b1=newJButton("登錄");

b2=newJButton("重置");

p1.setBackground(Color.orange);

p2.add(l1);

p2.add(f1);

p2.setBorder(newMatteBorder(0,0,0,0,Color.BLACK));

p2.setBackground(Color.ORANGE);

p3.add(l2);

p3.add(f2);

p3.setBackground(Color.ORANGE);

p3.setBorder(newMatteBorder(0,0,0,0,Color.BLACK));

p4.add(b1);

p4.add(b2);

p4.setBorder(newMatteBorder(-3,-3,-3,-3,Color.CYAN));

p4.setBackground(Color.ORANGE);

p5.setLayout(newFlowLayout(FlowLayout.CENTER,20,20));

p5.add(p2);

p5.add(p3);

p5.add(p4);

cp.add(p5,BorderLayout.CENTER);

b1.addActionListener(newEnter());

b2.addActionListener(newReWrite());

addWindowListener(newwinClose());

}

publicstaticvoidmain(String[]args){

LoginInlog=newLoginIn();

log.setTitle("系統登錄");

log.setSize(360,250);

log.setVisible(true);

}

{

publicvoidactionPerformed(ActionEvente)

{

if((f1.getText()).equals("admin")&&(f2.getText()).equals("123"))

{

JOptionPane.showMessageDialog(null,"登錄成功!用戶許可權是adimistrator");

power="adminstrator";

}

elseif((f1.getText()).equals("snake")&&(f2.getText()).equals("123456"))

{

JOptionPane.showMessageDialog(null,"登錄成功!登錄成功!用戶許可權是user");

power="adminstrator";

}

elseJOptionPane.showMessageDialog(null,"登錄失敗,請重新登錄!");

}

}

{

publicvoidactionPerformed(ActionEvente)

{

f1.setText("");

f2.setText("");

f1.requestFocus();

}

}

{

publicvoidwindowClosing(WindowEvente)

{

(e.getWindow()).dispose();

System.exit(0);

}

}

}

2. 用java寫一個登陸界面代碼

這是我以前寫的系統登錄框的login
package java1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Rectangle;
import java.awt.Font;

public class login extends JFrame
{
public login() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)
{
login frm=new login();
frm.setBounds(300,200,300,260);
frm.setVisible(true);

}

private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jLabel1.setForeground(Color.red);
jLabel1.setText("用戶名");
jLabel1.setBounds(new Rectangle(25, 72, 68, 29));
jButton2.setBounds(new Rectangle(142, 159, 81, 30));
jButton2.setText("取消");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setBounds(new Rectangle(38, 159, 81, 29));
jButton1.setText("確定");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(109, 79, 136, 23));
jPasswordField1.setBounds(new Rectangle(109, 115, 136, 23));
this.getContentPane().add(jLabel1);
jLabel3.setFont(new java.awt.Font("隸書", Font.PLAIN, 24));
jLabel3.setText("學 生 考 試 系 統");
jLabel3.setBounds(new Rectangle(22, 15, 268, 47));
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jPasswordField1);
jLabel2.setForeground(Color.red);
jLabel2.setText("密碼");
jLabel2.setBounds(new Rectangle(26, 112, 68, 29));
}

JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel3 = new JLabel();
JPasswordField jPasswordField1 = new JPasswordField();
main2 wo=new main2();
int sum;

//判斷許可權的方法
public void success()
{
try{
ResultSet rs1;
String s1="select * from 管理許可權 where name='"+jTextField1.getText()+"' and password='"+jPasswordField1.getText()+"'";
rs1=sqlcx.Rs_jiluji(s1);
rs1.first();
sum=Integer.parseInt((String)(rs1.getString(3)));
System.out.println(sum);
very();
}
catch(Exception c)
}

//根據許可權設置窗口
public void very()
{
if (sum==1)
{JOptionPane.showOptionDialog(this, "您現在是以教師許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);

}
else
{
JOptionPane.showOptionDialog(this, "你現在是以學生許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
wo.jButton2.setVisible(false);
wo.jButton3.setVisible(false);
wo.jButton4.setVisible(false);
wo.jButton5.setVisible(false);
wo.jButton6.setVisible(false);
wo.jButton7.setVisible(false);
wo.jButton8.setVisible(false);
wo.jMenu2.setVisible(false);
wo.jMenu3.setVisible(false);
wo.jMenu4.setVisible(false);
}

}

public void jButton1_actionPerformed(ActionEvent e)
{
//判斷用戶名是否為空
if (jTextField1.getText().length() == 0) {
JOptionPane.showOptionDialog(this, "用戶名不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
//判斷密碼是否為空
else if (jPasswordField1.getText().length()==0)
{
JOptionPane.showOptionDialog(this,"密碼不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
try
{
ResultSet rs;//聲明記錄集
String sql="select * from 管理許可權 where name='"+jTextField1.getText()+"'";
rs = sqlcx.Rs_jiluji(sql);
if (rs.next())
{
if(jPasswordField1.getText().equals(rs.getString(2)))
{

success();
wo.setBounds(60,40,700,600);
wo.setVisible(true);
this.dispose();
}
else
{
JOptionPane.showOptionDialog(this,"密碼錯誤", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
else
{
JOptionPane.showOptionDialog(this,"沒有所要找的用戶名", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}

}
catch(Exception c){
JOptionPane.showOptionDialog(this,"連接資料庫失敗", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
}

3. java登錄界面

應用的還是web的啊..

我給你寫了個應用的哈

你在C盤建個test.txt文件

裡面寫
username:用戶名(這里可以隨便寫哈)
password:同上哈
注意是要換行的哦。。

比如
username:tiger
password:tiger

然後你建個Login的類
然後把下面的代碼弄進去
運行就是了
注意登陸的時候你文件裡面設定的什麼用戶名和密碼就輸入什麼哈
輸入錯誤就會提示輸入錯誤的

import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
*
* @author thinkpad
*/
public class Login extends javax.swing.JFrame {

/** Creates new form Login */
private static String username;
private static String password;
public Login() {
initComponents();
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt"));
username = br.readLine().split("\\:")[1];
password = br.readLine().split("\\:")[1];
System.out.println(username + password);
} catch(Exception e) {
e.printStackTrace();
}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("宋體", 0, 18)); // NOI18N
jLabel1.setText("Login");

jLabel2.setText("Username:");

jLabel3.setText("Password:");

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
(evt);
}
});

jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Reset");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.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(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPasswordField1, 0, 0, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void (java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField1.setText("");
this.jPasswordField1.setText("");
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String inputUsername = this.jTextField1.getText();
String inputPassword = String.valueOf(this.jPasswordField1.getPassword());
if(inputUsername.equals(username) && inputPassword.equals(password)) {
JOptionPane.showMessageDialog(this, "Login success!");
} else {
JOptionPane.showMessageDialog(this, "Login failed!");
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame.(true);
Login login = new Login();
login.setVisible(true);
login.setLocationRelativeTo(null);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

4. 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);
}
}

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 求一個好的登錄界面

在你窗口構造之前加入這段代碼,可以換成其他風格的,比如windows、mac等等,比Java原裝的好看。
try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // Windows風格

// UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel") ; // Mac風格

// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel") ; // Java默認風格

} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch ( ex) {
ex.printStackTrace();
}

7. 登陸界面的java代碼怎麼寫

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用戶名");
JLabel lbl2=new JLabel("密碼");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("確定");
JButton btn2=new JButton("取消");

public LoginFrm()
{
this.setTitle("登陸");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陸成功!");
}
else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤!");
} catch(Exception ex){}

if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}

public static void main(String arg[])
{
JFrame.(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}

8. java編寫登陸界面

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;

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 extends JFrame{

private JPanel jp1,jp2,jp3;
private JButton jbtnLogin;
private JLabel jlbUser,jlbPasswd;
private JPasswordField jpfPasswd;
private JTextField jtfUser;

public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}

public Login()
{
jp1 = new JPanel(new BorderLayout());
jp2 = new JPanel(new GridLayout(2,2));
jp3 = new JPanel();
jbtnLogin = new JButton("登錄");
jlbUser = new JLabel("用戶名:");
jlbPasswd = new JLabel("密碼:");
jtfUser = new JTextField();
jpfPasswd = new JPasswordField();
jp3.add(jbtnLogin);
jp2.add(jlbUser);
jp2.add(jtfUser);
jp2.add(jlbPasswd);
jp2.add(jpfPasswd);
jp1.add(jp2);
jp1.add(jp3,"South");
this.add(jp1);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(width/2-200, height/2-150);
this.setSize(400, 150);
}
}

9. 登錄界面的java代碼,分別有教師登錄,管理員登錄,學生登錄,右邊是用戶名和密碼,見圖。

分三個包,自己建個包,導進去就ok了,資料庫是access的。
package 登錄;

import java.awt.EventQueue;

public class Cilent {

private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cilent window = new Cilent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Cilent() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("登陸界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);

JLabel lblNewLabel = new JLabel("用戶名");
lblNewLabel.setBounds(38, 43, 80, 34);
frame.getContentPane().add(lblNewLabel);

textField = new JTextField();
textField.setBounds(155, 42, 227, 37);
frame.getContentPane().add(textField);
textField.setColumns(10);

JLabel label = new JLabel("密碼");
label.setBounds(38, 115, 80, 34);
frame.getContentPane().add(label);

passwordField = new JPasswordField();
passwordField.setBounds(155, 115, 227, 37);
frame.getContentPane().add(passwordField);

JButton btnNewButton = new JButton("登錄");
btnNewButton.setBounds(60, 187, 115, 34);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
UserCheck UC=new UserCheck(textField.getText(),String.valueOf(passwordField.getPassword()));
if(UC.getI()!=0) //有此用戶
{
frame.setVisible(false);
}
else
{
textField.setText("");
passwordField.setText("");
}
}
});

JButton button = new JButton("取消");
button.setBounds(242, 187, 115, 34);
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
textField.setText("");
passwordField.setText("");
}
});
}
}
/*****************************************************************/
package 登錄;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import 操作處理.UsersCL;

/**@author 20111024
* 檢測登錄的用戶在資料庫中有無,若沒有,則提示沒有此用戶,
* 若有,則判斷級別:普通用戶還是管理員。
*/
public class UserCheck {

private int i=0; //用戶級別:0不是用戶、1是管理員、2是普通用戶
UserCheck(String name ,String password)
{
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
String query="select * from users where name='"+name+"' and passwd='"+password+"'";

rs=stmt.executeQuery(query);
if(rs.next())
{
//資料庫中有此用戶,訪問成功
i=Integer.parseInt(rs.getString(3));
UsersCL UL=new UsersCL(i);
}
else
{
i=0; //沒有用戶是默認是0級
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getI() {
return i;
}
}
/********************************************************************************************/
package 操作處理;

import java.awt.EventQueue;

public class UsersCL implements ActionListener{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private int i=0;
private JLabel label_3;
private JTextField textField_4;
public UsersCL(int i) {
this.i=i;
frame = new JFrame();
frame.setTitle("用戶處理界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
frame.setVisible(true);

JLabel lblNewLabel = new JLabel("學號");
lblNewLabel.setBounds(24, 32, 74, 29);
frame.getContentPane().add(lblNewLabel);

JLabel label = new JLabel("姓名");
label.setBounds(24, 71, 74, 29);
frame.getContentPane().add(label);

JLabel label_1 = new JLabel("年齡");
label_1.setBounds(24, 110, 74, 29);
frame.getContentPane().add(label_1);

label_3 = new JLabel("性別");
label_3.setBounds(24, 149, 74, 29);
frame.getContentPane().add(label_3);

JLabel label_2 = new JLabel("狀態");
label_2.setBounds(24, 195, 74, 29);
frame.getContentPane().add(label_2);

textField = new JTextField();
textField.setBounds(101, 34, 113, 25);
frame.getContentPane().add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(101, 73, 113, 25);
frame.getContentPane().add(textField_1);

textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(101, 112, 113, 25);
frame.getContentPane().add(textField_2);

textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setColumns(10);
textField_3.setBounds(101, 199, 288, 25);
frame.getContentPane().add(textField_3);

textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(101, 149, 113, 25);
frame.getContentPane().add(textField_4);

if(1==i)
{
JButton btnNewButton = new JButton("追加");
btnNewButton.setBounds(276, 41, 113, 29);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
btnNewButton.setActionCommand("追加");

JButton button_1 = new JButton("刪除");
button_1.setBounds(276, 145, 113, 29);
frame.getContentPane().add(button_1);
button_1.addActionListener(this);
button_1.setActionCommand("刪除");
}

JButton button = new JButton("查詢");
button.setBounds(276, 91, 113, 29);
frame.getContentPane().add(button);
button.addActionListener(this);
button.setActionCommand("查詢");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name,age,sex,query=null;
int num,age1,count=0;
num=Integer.parseInt(textField.getText());
name=textField_1.getText();
age1=Integer.parseInt(textField_2.getText());
sex=textField_4.getText();

if(e.getActionCommand().equals("追加"))
{
query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";
count=1;
}
else if(e.getActionCommand().equals("查詢"))
{
query="select * from students where XSB="+num+";";
count=2;
}
else if(e.getActionCommand().equals("刪除"))
{
query="delete from students where XSB="+num+" and name="+"'"+name+"'";
count=3;
}

Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
String query1=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
if(count==1)
{
query1="select * from students where XSB="+num+";";
rs=stmt.executeQuery(query1);
if(rs.next())
textField_3.setText("已經由此記錄,不能追加!");
else
{
stmt.executeUpdate(query);
textField_3.setText("已經追加完成!");
}
}
else if(2==count)
{
stmt.executeQuery(query);
rs=stmt.executeQuery(query);
if(rs.next())
{
textField_3.setText("已查找到此記錄!");
}
else
{
textField_3.setText("沒有此記錄,可以追加!");
}
}
else if(3==count)
{
query1="select * from students where XSB="+num+" and name="+"'"+name+"'";
rs=stmt.executeQuery(query1);
if(rs.next())
{
stmt.executeUpdate(query);
textField_3.setText("已刪除此記錄!");
}
else
textField_3.setText("無此記錄!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
//關閉資源
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (Exception e2) {
// TODO: handle exception
}
con=null;
}
}
}
}

10. 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();
}
}