很久之前的代碼不會改了 有沒有大佬能幫我改一下,在76行那getText()已過時,怎么把這個方法換一下?
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Login extends JFrame implements ActionListener{
public static void main(String[] args) {
Login lo = new Login("登陸");//主方法,有這個程式才會運行
}
//圖形界面初始化陳述句,有這個視窗才會有布局
JLabel label1 = new JLabel("用戶名:");
JLabel label2=new JLabel("密碼:");
static JTextField text1 = new JTextField(10);;
JPasswordField text2 = new JPasswordField(10);;
JButton button1 = new JButton("登陸");
JButton button2=new JButton("取消");
JButton button3=new JButton("注冊");
public Login(String s) {
init(); //視窗布局初始化方法,就是下邊那個init方法
setLayout(null);
setTitle("登錄");//設定標題
setBounds(400,50,600,500);//設定視窗位置及大小
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//視窗布局初始化方法
void init() {
//設定文字顏色等
label1.setForeground(Color.black);
label2.setForeground(Color.black);
label1.setFont(new Font("宋體",Font.BOLD,20));
label2.setFont(new Font("宋體",Font.BOLD,20));
//將布局添加到視窗
add(label1);
add(label2);
add(text1);
add(text2);
add(button1);
add(button2);
add(button3);
//設定各標簽大小及位置
label1.setBounds(110,70,100,40);
text1.setBounds(230,70,250,40);
label2.setBounds(110,130,100,40);
text2.setBounds(230,130,250,40);
button1.setBounds(110,320,100,40);
button2.setBounds(250,320,100,40);
button3.setBounds(390,320,100,40);
//給按鈕添加動作監聽器
button1.addActionListener((ActionListener) this);
button2.addActionListener((ActionListener) this);
button3.addActionListener((ActionListener) this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
//獲取輸入值
String us=text1.getText();
String pa=text2.getText();
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); //鏈接資料庫驅動
//鏈接資料庫
Connection conn = (Connection) DriverManager.getConnection("jdbc:derby:Student;create=true");
System.out.println(conn);
//查詢驗證資料庫并登錄
String sql = "Select * from user_u where Uname=? and Upass=? ";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, us);
ps.setString(2, pa);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(this, "登陸成功,歡迎來到主頁面",
"確定", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
new YWindow();
}else{
JOptionPane.showMessageDialog(this, "用戶名or密碼錯誤",
"確定", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
Login lo = new Login("登陸");
}
conn.close();
ps.close();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}catch (SQLException e1) {
e1.printStackTrace();
}
}else if (e.getSource() == button2) {
//判斷是否點擊取消按鈕
JOptionPane.showMessageDialog(this, "退出應用程式", "退出",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);//結束程式
}else{
//點擊注冊,跳轉到注冊頁面
this.dispose();
new ZhuCe("注冊");
}
}
}
uj5u.com熱心網友回復:
new String(text2.getPassword());uj5u.com熱心網友回復:
String pa=text2.getPassword().toString().trim();
uj5u.com熱心網友回復:
具體怎么弄啊 我把那兩行代碼換了編譯錯誤啊轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/46094.html
標籤:Java SE
