-1

谁能帮我纠正这个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();
    }
}
4

2 回答 2

1

请参阅使用多个 JFrame,好/坏做法? 登录应该是 modalJDialogJOptionPane. 从框架中弹出对话框,然后检查下一行代码中的相关字段。

尝试查看此代码以获取提示。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

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(this);
        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();
    }
}

class Login extends JDialog 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(Frame f){
        // make it modal
        super(f, true);
        initGUI();
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_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();
        // never store passwords as a String, left as an exercise for the user
        pass = new String(tfPass.getPassword());
        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(new JLabel("Login"));
        //con.add(p1);
        JPanel p2=new JPanel(new GridLayout(3,2));
        p2.add(new JLabel("Type:"));
        p2.add(cbotype=new JComboBox());
        cbotype.addItem("Employee");cbotype.addItem("Patron");
        p2.add(new JLabel("User:"));
        p2.add(tfUser=new JTextField(20));
        p2.add(new JLabel("Password:"));
        p2.add(tfPass=new JPasswordField(20));
        //con.add(p2);

        JPanel p3=new JPanel(new FlowLayout());
        p3.add(btnLogin =  new JButton("Login"));
        p3.add(btnCancel = new JButton("Cancel"));

        JPanel p=new JPanel(new BorderLayout());
        // Don't use magic numbers!  There are defined constants
        // for these constraints.
        p.add(p1,BorderLayout.NORTH);
        p.add(p2,BorderLayout.CENTER);
        p.add(p3,BorderLayout.SOUTH);
        add(p);
        pack();

        btnLogin.addActionListener(this);
        btnCancel.addActionListener(this);
    }
    public String getUser(){
        return user;
    }
    public String getPass(){
        return pass;
    }
}

其他提示

  1. 不要扩展框架或对话框,只需使用它们的实例。
  2. Swing 和 AWT GUI 应该在 Event Dispatch Thread 上创建和更新。
  3. 对代码块使用一致且合乎逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
  4. 请参阅代码注释中的更多提示。
于 2013-04-21T09:30:52.867 回答
1

一种方法:当你调用 Login 构造函数时,就是将 MainForm 实例解析给它。您的 Login 类将保留对 MainForm 的引用,并且当您需要让 MainForm 知道时,从 Login 类中调用 MainForm 上的方法。这与听众的工作方式几乎相同......

在您的 MainForm 课程中

Login login=new Login(this);

当您想让 MainForm 知道时,在您的 Login 类中:

this.mainForm.someListenerMethod();
于 2013-04-21T09:11:18.120 回答