當前位置:編程學習大全網 - 源碼下載 - java GUI 登陸界面

java GUI 登陸界面

界面代碼寫的不錯 其實連數據庫也不復雜。 如果希望不轉換界面,登陸和登陸成功或失敗後的信息在同意界面 可以設置個int類型的變量 來控制顯示信息

下面是按妳的要求寫的 現實界面的轉換 如果用戶名和密碼正確進入MyFrame1窗口 如果不正確進入MyFrame2窗口。

連數據庫的時候先添加數據庫驅動包,如果連接數據庫有問題 多打印壹下相關數據 很容易發現錯誤之處的

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class 登陸界面 extends JFrame {

private static final long serialVersionUID = 1L;

private JPasswordField passwordField;

private JTextField textField;

public static void main(String args[]) {

try {

登陸界面 frame = new 登陸界面();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the frame

*/

public 登陸界面() {

super();

getContentPane().setLayout(null);

setBounds(100, 100, 500, 375);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton();

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String dir = "com.mysql.jdbc.Driver";//數據庫驅動類,我這是MySQL的 ,連數據庫記得導入驅動的jar包

Connection conn = null;

Statement stat = null;

ResultSet rs = null;

try{

Class.forName(dir);

String url = "jdbc:mysql://localhost:3306/hospital?user=root&password=root";//數據庫名 hospital用戶名和密碼都是root

conn = DriverManager.getConnection(url);

stat = conn.createStatement();

//得到用戶名

String name = textField.getText().trim();

//得到密碼

char[] pass = passwordField .getPassword();

String password = new String(pass);

//輸出用戶名密碼

System.out.println(name);

System.out.println(password);

//數據庫表user,字段用戶名為username,密碼password,都是varchar型

String sql = "select username,password from user where username = '" + name + "'";

rs = stat.executeQuery(sql.toString());

if(rs.next()){

//獲得數據庫裏面的用戶名和密碼

String sname = rs.getString("username");

String spass = rs.getString("password");

//對輸入的用戶名和密碼和數據庫裏的進行判斷

if((rs != null ) && (name.equals(sname)) && (password.equals(spass))) {

//登陸成功釋放登陸界面資源,顯示a窗口

登陸界面.this.dispose();

new MyFrame1().launch();

System.out.println("chenggong");

} else {

//密碼錯誤,顯示b窗口

登陸界面.this.dispose();

new MyFrame2().launch();

System.out.println("mimacuowu");

}

} else {

//用戶名錯誤和其他情況,顯示b窗口

登陸界面.this.dispose();

new MyFrame2().launch();

System.out.println("yonghumingcuowu");

}

}catch(Exception e1){

//數據庫連接失敗,顯示b窗口

登陸界面.this.dispose();

new MyFrame2().launch();

System.out.println("數據庫連接錯誤");

}finally{

try{

if(rs!=null) rs.close();

if(stat!=null) stat.close();

if(conn!=null) conn.close();

}catch(Exception e1){

}

}

}

});

button.setText("確定");

button.setBounds(62, 273, 101, 25);

getContentPane().add(button);

final JButton button_1 = new JButton();

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {System.exit(0);

}

});

button_1.setText("取消");

button_1.setBounds(325, 273, 101, 25);

getContentPane().add(button_1);

final JLabel label = new JLabel();

label.setText("用戶名");

label.setBounds(23, 60, 60, 15);

getContentPane().add(label);

final JLabel label_1 = new JLabel();

label_1.setText("密碼");

label_1.setBounds(23, 172, 60, 15);

getContentPane().add(label_1);

textField = new JTextField();

textField.setBounds(48, 115, 90, 21);

getContentPane().add(textField);

passwordField = new JPasswordField();

passwordField.setBounds(42, 216, 96, 25);

getContentPane().add(passwordField);

}

}

class MyFrame1 extends Frame {

int orgX1;

int orgY1;

int orgX2;

int orgY2;

public void launch() {

setBounds(500,500,500,500);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

e.getWindow().dispose();

System.exit(0);

}

});

}

public void paint(Graphics g) {

//Color c =g.getColor();

g.setColor(Color.red);

g.drawLine(100, 50, 100, 80);

}

}

class MyFrame2 extends Frame {

int orgX1;

int orgY1;

int orgX2;

int orgY2;

public void launch() {

setBounds(500,500,500,500);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

e.getWindow().dispose();

System.exit(0);

}

});

}

public void paint(Graphics g) {

//Color c =g.getColor();

g.setColor(Color.red);

g.draw3DRect(100, 100, 100, 100,true);

}

}

  • 上一篇:gamt語法怎麽突破瓶頸
  • 下一篇:超級少年& ltU & gt的拼音歌詞
  • copyright 2024編程學習大全網