[英]How Do I Retrieve Class Fields Set in JFrame 1 from JFrame 2
美好的一天,
我是Java编程的新手,目前正在上一门课程。 现在,我们正在一个项目上,我们要负责创建自己的类。 到目前为止,我已经成功了。 我的应用程序具有两种JFrame形式( JFrame1和JFrame2 )。 我正在使用NetBeans 7.3.1 IDE,并将JFrame1设置为主类。
当我运行应用程序时,JFrame1打开,我使用以下代码通过按钮设置类“ company ”中字段的值:
public class JFrame1 extends javax.swing.JFrame
{
//Create company object and assign it to myCompany
company myCompany = new company(null, null, null, null, null);
private void btn_okActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//Create variables to hold the user input from the form
String name = txt_company_name.getText();
String street = txt_address_street.getText();
String city = txt_address_city.getText();
String state = txt_address_state.getText();
String zip = txt_address_zipcode.getText();
//Assign the form data to the fields in the company class
myCompany.set_company_name(name);
myCompany.set_company_street(street);
myCompany.set_company_city(city);
myCompany.set_company_state(state);
myCompany.set_company_zipcode(zip);
//Display a friendly message informing user that input has been accepted
//then hide this form and display JFrame2.
JOptionPane.showMessageDialog(null, "Company data entered successfully, "
+ "application will now open.\nClick OK to proceed.", "THANK YOU!",
JOptionPane.INFORMATION_MESSAGE);
new JFrame2().setVisible(true); //Create new instance of my JFrame2 form and make it visible
this.setVisible(false); //Hide this form from view
}
}
在“ company ”类中设置值之后,该窗体关闭并打开我的JFrame2 ,其中有一个名为“ lbl_company_name ”的标签。 我想使用先前使用JFrame1设置的“ company ”类的字段的值来设置此标签的text属性。
到目前为止,这是我尝试过的方法,并且标签更改为一系列空值。 我感觉这是因为我使用“ new”关键字创建一个新对象-而不是拥有我需要的数据的对象。 我对吗?
public class JFrame2 extends javax.swing.JFrame
{
//Create company object and assign it to myCompany
company myCompany = new company(null, null, null, null, null);
public void showCompanyInfo()
{
// Define variables and assign to them fields from my Company class
String name = myCompany.get_company_name();
String street = myCompany.get_company_street();
String city = myCompany.get_company_city();
String state = myCompany.get_company_state();
String zip = myCompany.get_company_zipcode();
// Use the variables above to manipulate the display of a label
lbl_company_name.setText(name + " | " + street + "," + city + "," + state + " " + zip);
}
/**
* Creates new form JFrame2
*/
public JFrame2()
{
initComponents();
jPanel1.setVisible(false);
showCompanyInfo();
}
}
非常感谢您的协助。
忘记特定的代码解决方案,而应该专注于基本问题:
setCompany(Company myCompany)
将它从一个传递到另一个,就像设置任何字段一样。 作为一种非常粗略的方法,我可以快速提出以下建议。 在您的JFrame1类中,添加一个吸气剂:
public class JFrame1 extends javax.swing.JFrame
{
//Create company object and assign it to myCompany
company myCompany = new company(null, null, null, null, null);
public company getCompany() {
return myCompany; // return your myCompany object where everything is set from fields
}
//... rest of your code in JFrame1...
然后在您的JFrame2中,删除company
字段(您现在正在那里创建一个新实例-因此所有null!),并将一个company
参数添加到showCompanyInfo()
方法中:
public class JFrame2 extends javax.swing.JFrame
{
public void showCompanyInfo(company c)
{
// Define variables and assign to them fields from my Company class
String name = c.get_company_name();
String street = c.get_company_street();
String city = c.get_company_city();
String state = c.get_company_state();
String zip = c.get_company_zipcode();
// Use the variables above to manipulate the display of a label
lbl_company_name.setText(name + " | " + street + "," + city + "," + state + " " + zip);
}
/**
* Creates new form JFrame2
*/
public JFrame2()
{
initComponents();
jPanel1.setVisible(false);
showCompanyInfo(jPanel1.getCompany());
}
}
附带说明一下,对类名使用驼峰式大小写,即company
是类的坏名, Company
更好。 也可以使用camel-case作为方法,snake-case(带有下划线)不能很好地与Java Beans概念配合使用, get_company_name()
所有get_company_name()
重命名为getCompanyName()
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.