下面是按你的要求写的 现实界面的转换 如果用户名和密码正确进入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)
}
}
meo aemie
ae
emi oe la
aenia em a
eja sd e
好了给分吧渗嫌空丛瞎者饥
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)