良い一日、
私はJavaプログラミングの初心者で、現在これに関するクラスを受講しています。現在、独自のクラスを作成するプロジェクトに取り組んでいます。これまでのところ、私は成功しています。私のアプリケーションには、2 つの 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」クラスに値を設定した後、フォームが閉じて、「 lbl_company_name 」というラベルがあるJFrame2が開きます。JFrame1 を使用して以前に設定した " company " クラスのフィールドの値で、このラベルの text プロパティを設定したいと思います。
これは私がこれまでに試したことであり、ラベルは一連の null に変更されています。これは、「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();
}
}
どうぞよろしくお願いいたします。