如何让textBox在JFrame中获取数量

时间:2015-09-28 01:35:06

标签: java swing jframe

我正在做一个像一个超市一样工作的程序,你可以选择你想要购买的东西,你可以从一些产品中选择你想要的数量,但我不知道如何通过textBox进行工作。< / p>

代码的图像是:http://i.imgur.com/MjLbEwG.png

在数量文本框中,当用户输入一定数量的数量时,他将获得产品的价格,并且总计与结果的其余部分一起显示。

3 个答案:

答案 0 :(得分:4)

不要使用JTextField。

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

Detecting Spinner Changes部分将帮助您了解如何使用ChangeListener,以便您可以监控数量何时发生变化,然后就可以计算总金额。

答案 1 :(得分:3)

是的,使用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
}

答案 2 :(得分:2)

您可以将文本框的内容解析为int。

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