2012-12-02 88 views
0

我想要將一個類中的文本框中的值傳遞給另一個類中的另一個文本框。我有一個名爲PurchaseSystem和另一個PaymentSystem的類,我想將PurchaseSystem中的值傳遞給PaymentSystem。將變量從一個文本框傳遞到另一個文本框

private void btnMakePaymentActionPerformed(java.awt.event.ActionEvent evt) {            
    String selected; 
    new PaymentSystem().setVisible(true); 

    PaymentSystem information; 

    information = new PaymentSystem(); 
    information.itemChoosen = txtDisplayItem.getText(); 
    information.itemPrice = txtDisplayPrice.getSelectedText(); 
    information.setVisible(true); 

}  


public class PaymentSystem extends javax.swing.JFrame { 

public String itemChoosen, itemPrice, itemQuantity, itemSubTotal; 
/** 
* Creates new form PaymentSystem 
*/ 
public PaymentSystem() { 
    initComponents(); 

    itemTextBox.setText(itemChoosen); 
    priceTextBox.setText(itemPrice); 
}    

這是我迄今所做但PurchaseSystem類的值不會出現在PaymentSystem類文本框。請幫助

+0

你同時聲明類的靜態內部類? – DRastislav

+0

@DRastislav我只在Paymentsystem類中有一組公共字符串,因爲我使用它來將值存儲在購買系統類 – user1642943

回答

0

您需要添加一個更新將值傳遞給PaymentSystem的方法。目前,您只能在其構造函數中設置PaymentSystem的值。這些變化沒有被分配String領域itemChoosenitemPrice

0

setText(itemChoosen)是隻要創建PaymentSystem對象簡稱體現。那時字符串itemChoosen是空的。

我會實現PaymentSystem的方法來設置itemTextBox文本,所以你可以稱之爲一個代替information.itemChoosen

0

可以更改PaymentSystem類的構造函數如下

class PaymentSystem{ 
    private String itemPrice =null; 
    private String itemChoosen = null; 
    public PaymentSystem(String itemChoosen,String itemPrice){ 
     this.itemPrice = itemPrice; 
     this.itemChoosen = itemChoosen; 
    } 
    //rest of the class 
} 

時初始化PaymentSystem類傳遞兩個字符串值。所以你可以使用這些值。

0

您需要根據對象進行推理。不是按照課程。每次您在做new PaymentSystem()時,都會創建一個新對象,該對象具有自己的文本框,與另一個PaymentSystem實例的文本框不同。

讓我們舉個例子:

Bottle greenBottle = new Bottle(); // creates a first bottle 
greenBottle.setMessage("hello"); 
Bottle redBottle = new Bottle(); // creates another bottle, which can have its own message 
System.out.println(redBottle.getMessage()); 

在上面的代碼,null將被打印出來,而不是「你好」,因爲你在一個瓶子裏保存的消息,並正從另一瓶的消息。

如果你想存儲的消息在一個瓶子,並在以後檢索它,你需要存儲的瓶子一個變量:

private Bottle theBottle; 

// in some method: 
theBottle = new Bottle(); // creates the bottle 
theBottle.setMessage("hello"); 

// in some other method 
System.out.println(theBottle.getMessage()); // displays hello 
相關問題