在gui用户输入用户名和密码,然后单击确定按钮。但问题是我不知道如何使用ok按钮连接我的Connector类。当用户输入正确的字段然后连接到数据库时,限制该按钮 主要方法
public class Main {
public static void main(String[] args) {
Gui obj = new Gui();
Connector conn = new Connector(obj.Setproperty());
}
}
Gui Class
public class Gui extends JFrame implements ActionListener{
private JLabel label1;
private JLabel label2;
private JTextField t1;
private JTextField t2;
private JButton ok;
private JPanel p1,p2;
Properties property;
public Gui(){
super("GUI");
p1 = new JPanel();
p1.setLayout(new GridLayout(0,2));
p2=new JPanel();
label1 = new JLabel("User Name");
t1 = new JTextField();
label2 = new JLabel("Password");
t2 = new JTextField();
ok= new JButton("OK");
ok.addActionListener(this);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
p1.add(label1);
p1.add(t1);
p1.add(label2);
p1.add(t2);
p2.add(ok);
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
property = new Properties();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
}
}
public Properties Setproperty(){
property.setProperty("UNAME",t1.getText());
property.setProperty("PWD",t2.getText());
return property;
}
}
连接器类
public class Connector {
Connection conn;
Statement state;
String username;
String pass;
String url;
public Connector(Properties p) {
username = p.getProperty("UNAME");
pass = p.getProperty("PWD");
url= "jdbc:mysql://localhost/world";
}
public boolean open() {
try {
conn = DriverManager.getConnection(url, username, pass);
state = conn.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
if(conn==null){
System.out.println("Connection is NULL or server not connected");
return false;
}
System.out.println("Connection Successfull");
return true;
}
}