将数据从一个传递到另一个

时间:2013-04-21 09:03:54

标签: java swing oop

有人可以帮我纠正这个Java吗?

我想将数据从登录obj JFrame传递到我的mainform课程,但我不知道怎么做!

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

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

在您的MainForm类

Login login=new Login(this);

当你想让MainForm知道时,在你的登录课程中:

this.mainForm.someListenerMethod();

答案 1 :(得分:1)

请参阅The Use of Multiple JFrames, Good/Bad Practice?登录应为模式JDialogJOptionPane。从框架中弹出对话框,然后检查下一行代码中的相关字段。

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

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&应在Event Dispatch Thread上创建和更新AWT GUI。
  3. 对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。
  4. 请参阅代码注释中的更多提示。