2015-09-28 25 views
-1

我正在做一個像超級市場一樣工作的程序,您可以選擇想要購買的產品,並且可以從某個產品中提取您想要的數量,但我不會不知道如何通過textBox進行工作。我該如何讓文本框在JFrame中獲取數量

代碼的印象是:http://i.imgur.com/MjLbEwG.png

其在數量textBox中,當用戶把量的一些數字,他將獲得在總的產品和價格與其他地區一起出現結果。

+1

請不要讓你的問題更難回答的問題比它需要。不要發佈「代碼圖片」,發佈**實際代碼**,以便讓人們複製並粘貼它,然後進行分析。 –

+0

如果選擇JCheckBox,Integer.parseInt((JTextField reference).getText())* price ... – RamanSB

+0

查看[如何使用Spinners](http://docs.oracle.com/javase/tutorial/ uiswing/components/spinner.html)和[如何使用帶格式的文本字段](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) – MadProgrammer

回答

2

您可以將文本框的內容解析爲一個int。

int total = Integer.parseInt(textbox.getText()); 
4

請勿使用JTextField。

相反,您可以使用JSpinner。閱讀Swing教程How to Use Spinners中的部分。

關於Detecting Spinner Changes的部分將幫助您如何使用ChangeListener,以便您可以監控數量何時發生變化,然後您可以計算總金額。

2

是,使用的Integer.parseInt但在try/catch塊,就像這樣:

try{ 
    int quantity1 = Integer.parseInt(yourJTextfield.getText()); 
    //.......... get the other quantities here 
} 
catch(NumberFormatException e){ 
    // Something went wront here, the user typed a non-numeric value and this exception will be raised, show an alert for example 
} 
+0

或者您可以使用JSpinner並且只使用數字值只能輸入,所以你永遠不用擔心這一點。你也可以控制範圍,所以你不需要做任何編輯檢查。 – camickr

相關問題