谁能帮我纠正这个Java?
我想将登录 obj 中的数据传递JFrame
给我的mainform
班级,但我不知道该怎么做!
这是我的代码:
登录.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
class Login extends JFrame implements ActionListener{
private JTextField tfUser;
private JPasswordField tfPass;
private JButton btnLogin,btnCancel;
private JComboBox cbotype;
private String user,pass;
private Connection con;
private Statement stmt;
private ResultSet rs;
public Login(){
super("Login");
initGUI();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnLogin) pLogin();
else if(e.getSource()==btnCancel) cancel();
}
public void cancel(){ dispose();
}
public void pLogin(){
if(isLogin())
setVisible(false);
System.out.print(user);
}
public boolean isLogin(){
String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
pass=tfPass.getText();
user=tfUser.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");
if(rs.next ()){
rs.first();
if(rs.getString(2).compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,sms);
return true;
}else{
sms="Incorrect Password! Please, try it again!";
}
}else
{sms="Incorrect User or Password! Please, try again!";}
JOptionPane.showMessageDialog(null,sms);
}catch(SQLException e){ e.printStackTrace();
}catch(ClassNotFoundException e1){e1.printStackTrace();
}return false;
}
public void initGUI(){
JPanel p1=new JPanel(new FlowLayout());
p1.add(Interface.title(new JLabel("Login"),"images/login_0.png"));
//con.add(p1);
JPanel p2=new JPanel(new GridLayout(3,2));
p2.add(Interface.label(new JLabel("Type:")));
p2.add(cbotype=new JComboBox());
cbotype.addItem("Employee");cbotype.addItem("Patron");
p2.add(Interface.label(new JLabel("User:")));
p2.add(tfUser=new JTextField(20));
p2.add(Interface.label(new JLabel("Password:")));
p2.add(tfPass=new JPasswordField(20));
//con.add(p2);
JPanel p3=new JPanel(new FlowLayout());
p3.add(Interface.button(btnLogin=new JButton("Login"),"images/login_1.png"));
p3.add(Interface.button(btnCancel=new JButton("Cancel"),"images/login_2.png"));
JPanel p=new JPanel(new BorderLayout());
p.add(p1,"North");
p.add(p2,"Center");
p.add(p3,"South");
add(p);
pack();
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
}
public String getUser(){
return user;
}
public String getPass(){
return pass;
}
public static void main(String[] args) {
new Login();
}
}
MainForm.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainForm extends JFrame implements ActionListener{
private Container con;
private String user;
private JButton btnshow;
public MainForm() {
initGUI();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void initGUI(){
Login login=new Login();
user=login.getUser();
//login.dispose();
System.out.print(user);
con=getContentPane();
con.add(btnshow=new JButton("Show"));
btnshow.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnshow) {System.out.print(user);
}
}
public static void main(String[] args) {
new MainForm();
}
}