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)建站云計(jì)算的互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)13年的服務(wù)器租用、成都移動(dòng)云計(jì)算中心、云服務(wù)器、網(wǎng)站空間、網(wǎng)站系統(tǒng)開(kāi)發(fā)經(jīng)驗(yàn),已先后獲得國(guó)家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)許可證。專業(yè)提供云主機(jī)、網(wǎng)站空間、域名與空間、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
CS結(jié)構(gòu)系統(tǒng)的退出如下:public void init() {
this.setTitle("用戶登錄界面");
this.add(createCenterPane());
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
this.setSize(new Dimension(450, 335));
this.setLocationRelativeTo(null);
// this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int choose = JOptionPane.showConfirmDialog(null, "是否要退出登錄界面?",
"系統(tǒng)提示:", JOptionPane.YES_NO_OPTION);
if (choose == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
}其中this為JFrame對(duì)象。BS結(jié)構(gòu)的退出直接用windows.close()方法就行了!
import?java.awt.Dimension;
import?java.awt.Toolkit;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JOptionPane;
import?javax.swing.JPasswordField;
import?javax.swing.JTextField;
public?class?Test26?{
public?static?void?main(String[]?args)?{
final?String?userName?=?"abc";
final?String?passwrod?=?"123";
JFrame?jFrame?=?new?JFrame("登陸界面");
Dimension?dimension?=?Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth()?-?200)?/?2,?((int)dimension.getHeight()?-?300)?/?2,?200,?150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel?label1?=?new?JLabel("姓名");
label1.setBounds(10,?10,?100,?30);
jFrame.add(label1);
JLabel?label2?=?new?JLabel("密碼");
label2.setBounds(10,?40,?100,?30);
jFrame.add(label2);
final?JTextField?text1?=?new?JTextField();
text1.setBounds(50,?15,?130,?20);
jFrame.add(text1);
final?JPasswordField?text2?=?new?JPasswordField();
text2.setBounds(50,?45,?130,?20);
jFrame.add(text2);
JButton?button?=?new?JButton("Login");
button.setBounds(10,?75,?170,?40);
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if(userName.equals(text1.getText())??passwrod.equals(text2.getText()))?{
JOptionPane.showMessageDialog(null,?"登陸成功誤",?"提示",?JOptionPane.INFORMATION_MESSAGE);
}?else?{
JOptionPane.showMessageDialog(null,?"錯(cuò)誤",?"提示",?JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
我有一個(gè)微信公眾號(hào),經(jīng)常會(huì)分享一些Java技術(shù)相關(guān)的干貨,還有一些學(xué)習(xí)資源。
如果你喜歡我的分享,可以用微信搜索“Java團(tuán)長(zhǎng)”或者“javatuanzhang”關(guān)注。
第一個(gè)java文件LogoingDemo.java
import?java.util.Scanner;
public?class?LogoingDemo?{
public?static?void?main(String[]?args)?{
System.out.println("請(qǐng)輸入用戶名");
Scanner?sc??=?new?Scanner(System.in);
String?name?=?sc.nextLine();
System.out.println("請(qǐng)輸入密碼");
String?psw??=?sc.nextLine();
sc.close();
CheckDemo?cd?=?new?CheckDemo(name,psw);//用戶名和密碼傳入驗(yàn)證類
boolean?bo?=?cd.check();//調(diào)用方法進(jìn)行驗(yàn)證
if(bo){
System.out.println("登錄成功");
}else{
System.out.println("登錄失敗:提示?用戶名?admin?密碼123");
}
}
}
第二個(gè)java文件CheckDemo.java
public?class?CheckDemo?{
public?String?name;
public?String?psw;
public?CheckDemo(String?name,String?psw){//構(gòu)造器
this.name?=?name;
this.psw?=?psw;
}
public?boolean?check()?{
//?用戶名?密碼不能為空.?用戶名=admin?密碼?=?123
if?(name?!=?null??psw?!=?null??name.equals("admin")??psw.equals("123"))?{
return?true;
}
return?false;
}
}
效果
請(qǐng)輸入用戶名
admin
請(qǐng)輸入密碼
123
登錄成功
------------------------------------
請(qǐng)輸入用戶名
add
請(qǐng)輸入密碼
123
登錄失敗:提示?用戶名?admin?密碼123