将JTextField从Frame传递给java中的另一个类

时间:2015-10-06 13:39:12

标签: java constructor

public class mainWindow extends JFrame {
private JTextField jtfCustomerName=new JTextField(8);
private JTextField jtfPassword=new JTextField(20);
Button login;

public mainWindow(){

    JPanel p1=new JPanel(new GridLayout(3,2));
    p1.add(new JLabel("Customer name:"));
    p1.add(jtfCustomerName);
     p1.add(new JLabel("Password:"));
    p1.add(jtfPassword);
    login=new Button("login");
    p1.add(login);
    add(p1,BorderLayout.CENTER);
    login.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            System.out.println("test..");



        }
    });



}


public String getAccNum(){
    return jtfCustomerName.getText();
}
 public String getPassword(){
    return jtfPassword.getText();
}
    }

所以这是一个简单的GUI类,用户可以输入accountNum和密码,然后单击一个按钮。我想将accountNum和密码发送到另一个类。

public class CustomerLoginGUIAPP {


   public static void main(String[] args) {


        mainWindow main=new mainWindow();
        main.setVisible(true);
        main.setSize(600,400);
        main.setLocation(300,300);
        System.out.println("Account num is " + ????);
        }}

这是主要类,我想在这里获取帐号和密码。我尝试了几种方法,我都得到NULL值。我读了一些帖子,我知道我需要使用构造函数。我不太了解其他帖子,所以我不得不寻求帮助。

public class customer {


private String AccNum;
private String password;

public customer(String AccNum,String password){
this.AccNum=AccNum;
this.password=password;

}    

public String getAccNum(){
return AccNum;
}

public String getPassword(){
return password;
}
 public void setAccNum(String AccNum) {
    this.AccNum = AccNum;
}

public void setPassword(String password) {
    this.password = password;
} 
}

因为我知道我应该使用构造函数,所以我创建了这个类。但我不知道如何真正使用它。我知道基本构造函数(客户c =新客户(" s123"," abdcd");然后如果我想获得accNum,我可以使用" c.getAccNum() &#34)

2 个答案:

答案 0 :(得分:0)

您应该能够将对象强制转换为mainWindow。这样您就可以输入公共方法。

//这样做:

public class mainWindow {
....
private static customer person; // NEW

public static void main(String[] args) {
    mainWindow main=new mainWindow();
    main.setVisible(true);
    main.setSize(600,400);
    main.setLocation(300,300);
    login.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
          System.out.println("test..");
          final int accNum = getAccNum();
          final String password = getPassword();
          createCustomer(accNum, password);
        }
    });
}

private static void createCustomer(int accNum, String password) {
    person = new customer(accNum, password);
    System.out.println("It works -> Customer: " + person.getAccNum() + " - " + person.getPassword());
}

答案 1 :(得分:0)

一个好的模式是让你的窗口创建某种结果对象,当窗口消失时,显示窗口的人可以得到它。我将您的MainWindow类更改为:

public class MainWindow extends JFrame {
    private JTextField jtfCustomerName=new JTextField(8);
    private JTextField jtfPassword=new JTextField(20);
    private Customer result;

    public MainWindow(){

        JPanel p1 = new JPanel(new GridLayout(3,2));
        p1.add(new JLabel("Customer name:"));
        p1.add(jtfCustomerName);
        p1.add(new JLabel("Password:"));
        p1.add(jtfPassword);
        Button login = new Button("login");
        p1.add(login);
        add(p1,BorderLayout.CENTER);
        login.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                result = new Customer(jtfCustomerName.getText(), jtfPassword.getText()); //set the result here
                setVisible(false); //hide this window
            }
        });
    }

    public Customer getResult()
    {
        return result;
    }
}

然后您在CustomerLoginGUIAPP中访问它,如下所示:

public class CustomerLoginGUIAPP {
    public static void main(String[] args) {
        MainWindow main= new MainWindow();
        main.setSize(600,400);
        main.setLocation(300,300);
        main.setVisible(true);
        Customer customer = main.getResult(); //get the saved result
        System.out.println("Account num is " + customer.getAccNum());
    }
}

然后让您的Customer类保持不变。