import java.awt.*; \x0d\x0aimport javax.swing.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0aclass LoginFrm extends JFrame implements ActionListener \x0d\x0a{ \x0d\x0aJLabel lbl1=new JLabel("用戶名"); \x0d\x0aJLabel lbl2=new JLabel("密碼"); \x0d\x0aJTextField txt=new JTextField(15); \x0d\x0aJPasswordField pf=new JPasswordField(); \x0d\x0aJButton btn1=new JButton("確定"); \x0d\x0aJButton btn2=new JButton("取消"); \x0d\x0a\x0d\x0apublic LoginFrm() \x0d\x0a{ \x0d\x0athis.setTitle("登陸"); \x0d\x0aJPanel jp=(JPanel)this.getContentPane(); \x0d\x0ajp.setLayout(new GridLayout(3,2,10,10)); \x0d\x0ajp.add(lbl1);jp.add(txt); \x0d\x0ajp.add(lbl2);jp.add(pf); \x0d\x0ajp.add(btn1);jp.add(btn2); \x0d\x0abtn1.addActionListener(this); \x0d\x0abtn2.addActionListener(this); \x0d\x0a} \x0d\x0a\x0d\x0apublic void actionPerformed(ActionEvent ae) \x0d\x0a{ \x0d\x0aif(ae.getSource()==btn1) \x0d\x0a{ \x0d\x0atry \x0d\x0a{ \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); \x0d\x0aConnection con=DriverManager.getConnection("jdbc:odbc:MyDB","",""); \x0d\x0aStatement cmd=con.createStatement(); \x0d\x0aResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'"); \x0d\x0aif(rs.next()) \x0d\x0a{ \x0d\x0aJOptionPane.showMessageDialog(null,"登陸成功!"); \x0d\x0a} \x0d\x0aelse \x0d\x0aJOptionPane.showMessageDialog(null,"用戶名或密碼錯(cuò)誤!"); \x0d\x0a} catch(Exception ex){} \x0d\x0a\x0d\x0aif(ae.getSource()==btn2) \x0d\x0a{ \x0d\x0atxt.setText(""); \x0d\x0apf.setText(""); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0apublic static void main(String arg[]) \x0d\x0a{ \x0d\x0aJFrame.setDefaultLookAndFeelDecorated(true); \x0d\x0aLoginFrm frm=new LoginFrm(); \x0d\x0afrm.setSize(400,200); \x0d\x0afrm.setVisible(true); \x0d\x0a} \x0d\x0a}
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括青田網(wǎng)站建設(shè)、青田網(wǎng)站制作、青田網(wǎng)頁制作以及青田網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,青田網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到青田省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
程序如下:
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,"密碼輸入錯(cuò)誤,請?jiān)僭囈淮?);
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);
}
}
編程的注意事項(xiàng):
1、Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強(qiáng)大和簡單易用兩個(gè)特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
2、 Java具有簡單性、面向?qū)ο?、分布式、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
3、2006年11月13日,Java技術(shù)的發(fā)明者Sun公司宣布,將Java技術(shù)作為免費(fèi)軟件對外發(fā)布。Sun公司正式發(fā)布的有關(guān)Java平臺(tái)標(biāo)準(zhǔn)版的第一批源代碼,以及Java迷你版的可執(zhí)行源代碼。從2007年3月起,全世界所有的開發(fā)人員均可對Java源代碼進(jìn)行修改。
CS結(jié)構(gòu)系統(tǒng)的退出如下:public void init() {\x0d\x0a this.setTitle("用戶登錄界面");\x0d\x0a this.add(createCenterPane());\x0d\x0a this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);\x0d\x0a this.setSize(new Dimension(450, 335));\x0d\x0a this.setLocationRelativeTo(null);\x0d\x0a // this.setVisible(true);\x0d\x0a this.addWindowListener(new WindowAdapter() {\x0d\x0a public void windowClosing(WindowEvent e) {\x0d\x0a int choose = JOptionPane.showConfirmDialog(null, "是否要退出登錄界面?",\x0d\x0a "系統(tǒng)提示:", JOptionPane.YES_NO_OPTION);\x0d\x0a if (choose == JOptionPane.YES_OPTION) {\x0d\x0a System.exit(1);\x0d\x0a }\x0d\x0a }\x0d\x0a });\x0d\x0a }其中this為JFrame對象。BS結(jié)構(gòu)的退出直接用windows.close()方法就行了!
/* Main.java
*
* Created on __DATE__, __TIME__
*/
import javax.swing.JOptionPane;
/**
*
* @author __USER__
*/
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
initComponents();
}
/** 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.
*/
//GEN-BEGIN:initComponents
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jPanel1 = new javax.swing.JPanel(); //標(biāo)簽
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton(); //登錄按鈕
jButton2 = new javax.swing.JButton(); //
jTextField1 = new javax.swing.JTextField(); //文本框
jTextField2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //關(guān)閉時(shí)的操作,即退出程序
setTitle("登錄界面"); //設(shè)置標(biāo)題:歡迎使用股票管理系統(tǒng)
setResizable(false); //設(shè)置窗口不可調(diào)節(jié)大小
jLabel1.setFont(new java.awt.Font("微軟雅黑", 0, 18)); //設(shè)置標(biāo)簽的字體
jLabel1
.setText("您好,請輸入信息");
jLabel2.setText("用戶名");
jLabel3.setText("用戶密碼");
jButton1.setText("登錄");
jButton1.addActionListener(new java.awt.event.ActionListener() { //采用匿名內(nèi)部類來實(shí)現(xiàn)按鈕監(jiān)聽功能
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("退出");
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( //層次布局管理器,下面n長的代碼,就是設(shè)置位置,自己看吧,呵呵,這個(gè)必須的,就不多寫了
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(159, Short.MAX_VALUE)
.addComponent(jLabel1).addGap(140, 140,
140))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(110, 110, 110)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(54, 54, 54)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jTextField2,
javax.swing.GroupLayout.DEFAULT_SIZE,
191,
Short.MAX_VALUE)
.addGap(
112,
112,
112))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
191,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(141, 141, 141)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(89, 89, 89)
.addComponent(
jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE,
72,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(145, Short.MAX_VALUE)));
jPanel1Layout
.setVerticalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(55, 55, 55)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jTextField2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE,
33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(48, Short.MAX_VALUE)));
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.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()));
layout.setVerticalGroup(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)));
pack();
setLocationRelativeTo(null); //居中
}// /editor-fold
//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //事件處理,不建議這樣做
// TODO add your handling code here:
String name = this.jTextField1.getText().trim(); //獲得用戶名,去空格
String password = this.jTextField2.getText().trim(); //獲得密碼,去空格
if (name.equals("admin") password.equals("123456")) { //如果符合條件.....其實(shí)這里應(yīng)該去和數(shù)據(jù)庫關(guān)聯(lián),這是死代碼
// Showinformation sf=new Showinformation(); //我估計(jì)是另外一個(gè)框架,就是登錄成功之后的框架
// sf.setVisible(true);
// sf.show(); //這個(gè)方法就直接秒殺,不用了,因?yàn)閟f.setVisible(true)已經(jīng)被調(diào)用了,這里就多次一舉了
} else { //用戶名或者密碼驗(yàn)證失敗
JOptionPane.showMessageDialog(this, "用戶名或者密碼錯(cuò)誤啦~", "提示", //提示框
JOptionPane.INFORMATION_MESSAGE);
this.jTextField1.setText(""); //清空輸入框
this.jTextField2.setText("");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { //事件處理,其實(shí)可以將jButton1ActionPerformed放在一起處理,只要獲得按鈕的標(biāo)簽內(nèi)容 (JButton)(evt.getSuorse()).getText()就做對比就ko了
// TODO add your handling code here:
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { //好方法,呵呵使用該方式的原因是:awt是單線程模式的,所有awt的組件只能在(推薦方式)事件處理線程中訪問,從而保證組件狀態(tài)的可確定性。java核心編程技術(shù)中每個(gè)實(shí)例基本都用到了
public void run() {
new Main().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// 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.JTextField jTextField1;
private javax.swing.JTextField jTextField2;}
// End of variables declaration//GEN-END:variables
你修改一下吧,從記事本獲得用戶名判斷一下就行了
當(dāng)前名稱:java登錄按鈕設(shè)計(jì)代碼 java用戶登錄界面設(shè)計(jì)代碼
本文路徑:http://jinyejixie.com/article8/ddiseop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站營銷、軟件開發(fā)、App開發(fā)、面包屑導(dǎo)航、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)