2016-08-24 42 views
-2

是否可以使用來自一個jLabel的值來計算放置在另一個jLabel中的新值?我正在製作一個飛機預訂平臺,並且已經在一張jLabel中對票價進行了一些計算。我想用,爲了做到以下幾點:如何使用從一個jLabel到另一個的值

do{ 


     jLabel25.get(); 


    } while(jRadioButton5.isSelected()); 

所以基本上我想用一個do while循環,以便找到折扣,但我怎麼能包括所有的JLabel裏面的可能值? (所以我不必重複計算)。

+2

您需要提供更多的細節。 JLabels不計算任何東西。描述你已有的信息,你想要的結果,以及你目前得到的結果。 – VGR

+0

@VGR I上面包含更多細節^ –

+0

每次價格或折扣因素髮生變化時,您都必須重複計算。這是無法避免的。 – VGR

回答

1

我不完全確定你在問什麼,但我會盡我所能去幫助。

假設你有兩個標籤:(!「世界,你好」)

JLabel label1 = new JLabel l("Hello!"); 
JLabel label2 = new JLabel l("Hello world!"); 

如果你現在要LABEL1的(「你好」)中的內容設置成標籤2,你可以做到以下幾點:

label2.setText(label1.getText()); 

由於您在問題描述中使用了「計算」一詞,因此我假設您的標籤包含您想要進行計算的數字。

您可以將標籤方法.getText()與Integer.parseInt(String s)或Double.parseInt(String s)結合使用,具體取決於您擁有的值的類型。例如:

JLabel label1 = new JLabel l("5"); 
JLabel label2 = new JLabel l(""); 

int i = Integer.parseInt(label1.getText()); // takes the string from label1 and transforms it to an integer using the parseInt() method. 
// i now has the value 5 

i = i*5; // an example of some calculation you mentioned you wanted to do 

label2.setText(i +""); // transforms the integer value to string and sets it into label2 

我不知道我是否正確理解您的問題,所以請詳細說明您是否需要其他幫助:)。