将JComponents访问到另一个java非GUI类中

时间:2016-02-18 16:29:30

标签: java swing

如何在另一个非GUI类中访问类的任何JComponent(e.i. JTextField)?我想访问它们,以便我可以在非GUI类中实现所有函数,然后在GUI监听器中调用这些函数。 例如:

public class New_Client {
     private JFrame frmNewClient;
     private JTextField txt_FirstName,txt_LastName,txt_Address,txt_Phone;
     private JButton submit;
     public New_Client() {
         //initializing frame and other components
          ...
        txt_FirstName = new JTextField();
        txt_FirstName.setBounds(350,220,300,80);
        frmNewClient.getContentPane().add(txt_FirstName);

        submit = new JButton("Submit");
        submit.setSize(115,55);
        submit.setLocation(800,420);
        frmNewClient.getContentPane().add(submit);

        submit.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            ReceptionsitClass.insertRecords();
        }
    });
}

}

另一个课程如下:

public class Person {

private int id;
private String firstName, lastName, address, phone;

public Person(int ID, String fName, String lName, String addr, String tel){
    this.id = ID;
    this.firstName = fName;
    this.lastName = lName;
    this.address = addr;
    this.phone = tel;
}

public int getID(){
    return this.id;
}

}

另一个类是ReceptionistClass,它实现了insert函数:

public class ReceptionistClass {

public void InsertRecord() {

    Connection.getDBConnection().Connect();
    //String FirstName = txt_FirstName.getText(); 
    // How can I access txt_FirstName.getText() so that I can pass that value to the query and store it...
   ...
  // Person p = New Person(ID, FirstName,LastName, Adress, Phone);
}

}

2 个答案:

答案 0 :(得分:0)

有一些问题和一些方法可以使它正确。 最简单的方法(不推荐)使字段静态和公共:

  public static JTextField txt_FirstName,txt_LastName,txt_Address,txt_Phone;

比你可以在任何类中访问它

String FirstName = New_Client.txt_FirstName.getText();

更好的方法是让它不是静态的,但是你必须在你的类中拥有这个New_Client的实例并使用它:

    New_Client new_client=null; // initialize it before use
//...
    String FirstName = new_client.txt_FirstName.getText();

还有其他更合适的方式正确。我建议MVC模型 - 视图 - 控制器。您应该将数据与视图和控件分开。对于简单的SWING应用程序来说,它看起来有点过分,但这是正确的方法。

答案 1 :(得分:0)

你正在倒退。

构建GUI时,应使用model / view / controller pattern。您不仅要将您的关注点分成模型,视图和一个或多个控制器,而且还要按此顺序进行开发。首先,创建模型,然后创建视图,最后创建控制器。

所以,让我们来看看你的模型。我已经包括了所有的吸气剂和制定者。

public class Person {

    private int id;
    private String firstName, lastName, address, phone;

    public Person(int ID, String fName, String lName, String addr,
            String tel) {
        this.id = ID;
        this.firstName = fName;
        this.lastName = lName;
        this.address = addr;
        this.phone = tel;
    }

    public int getID() {
        return this.id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

接下来,您必须将模型传递给视图。这是您观点的大部分更正版本。

public class NewClient {
    private JFrame frmNewClient;
    private JTextField txt_FirstName, txt_LastName, txt_Address, txt_Phone;
    private JButton submit;

    public NewClient() {
        final Person person = new Person(100, "John", "Smith", "", "");

        // initializing frame and other components

        frmNewClient = new JFrame("New Client");
        frmNewClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        txt_FirstName = new JTextField();
        txt_FirstName.setText(person.getFirstName());
        panel.add(txt_FirstName);

        submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                person.setFirstName(txt_FirstName.getText().trim());
                ReceptionistClass rc = new ReceptionistClass();
                rc.insertRecord(person);
            }
        });
        panel.add(submit);

        frmNewClient.add(panel);
        frmNewClient.pack();
        frmNewClient.setVisible(true);
    }
}

最后,您的控制器类将您的Person编写到数据库中。

public class ReceptionistClass {

    public void insertRecord(Person person) {

        Connection.getDBConnection().Connect();
        // You don't care about the view here.
        // All you care about is the Person instance.
    }
}

我希望这可以帮助您整理代码,以便您可以一次专注于一小部分。