警告: [deprecation] JPasswordField中的getText()已过时
很久之前的代码不会改了 有没有大佬能帮我改一下,在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("注册");
}
}
}