Jtextfield中如何处理异常?
我正在尝试处理此代码,我想知道如何捕获 Jtextfield 中的异常,并且我还想限制可以放入 Jtextfield 中的数量。请让我知道我该怎么做,我尝试了不同的方法但没有任何效果,而且我使用桌面应用程序在 Netbeans 上创建了这个程序。这是代码
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
double price1 = (int) ((int) Double.parseDouble(carpetPricetxt.getText()));
boolean incorrect = true;
while (incorrect){
try{Double.parseDouble(price1);incorrect = false;}
catch(NumberFormatException nfe){
price1=JOptionPane.showInputDialog("Invalid input");
}
int carpetSize = (int) ((int) Double.parseDouble( CarpetLengthtxt.getText()));
double room= 0;
double carptCst= (price1/9)*carpetSize;
double labrCst= 90;
double subTotal = (price1/9)*carpetSize+labrCst;
String tax1 ="0.5%";
double totl= (price1/9)*carpetSize+labrCst+.5*subTotal;
double fruniture=0;
String zero = "0";
String one= "1";
String two= "2";
String three= "3";
String four= "4";
String five= "5";
String six= "6";
String seven= "7";
String eight= "8";
String nine= "9";
String ten= "10";
Object str= roomCountComboBox.getSelectedItem();
if (roomCountComboBox.getSelectedItem().equals(zero))
{
room= totl;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(one))
{
room= totl*1;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(two))
{
room= totl*2;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(three))
{
room= totl*3;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(four))
{
room= totl*4;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(five))
{
room= totl*5;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(six))
{
room= totl*6;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(seven))
{
room= totl*7;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(eight))
{
room= totl*8;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(nine))
{
room= totl*9;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(ten))
{
room= totl*10;
grandTotalLabel2.setText(String.valueOf(room));
}
Object str2= furnitureComboBox.getSelectedItem();
if (furnitureComboBox.getSelectedItem().equals(zero))
{
fruniture=room+totl;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(one))
{
fruniture=room+totl*1;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(two))
{
fruniture=room+totl*2;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(three))
{
fruniture=room+totl*3;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(four))
{
fruniture=room+totl*4;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(five))
{
fruniture=room+totl*5;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(six))
{
fruniture=room+totl*6;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(seven))
{
fruniture=room+totl*7;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(eight))
{
fruniture=room+totl*8;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(nine))
{
fruniture=room+totl*9;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(ten))
{
fruniture=room+totl*10;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
TaxLabel2.setText(String.valueOf(tax1));
laborCostLabel2.setText(String.valueOf(labrCst));
carpetCostLable2.setText(String.valueOf(carptCst));
totalLabel2.setText(String.valueOf(subTotal));
}
i am trying to get the work on this code i would like to know how can i will be able to catch exceptions in Jtextfield and i would also like to limit the amount that you can put in the Jtextfield. Plz let me know how can i do that i tried different things but nothing work and also i create this program on Netbeans using desktop application. here is the code
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
double price1 = (int) ((int) Double.parseDouble(carpetPricetxt.getText()));
boolean incorrect = true;
while (incorrect){
try{Double.parseDouble(price1);incorrect = false;}
catch(NumberFormatException nfe){
price1=JOptionPane.showInputDialog("Invalid input");
}
int carpetSize = (int) ((int) Double.parseDouble( CarpetLengthtxt.getText()));
double room= 0;
double carptCst= (price1/9)*carpetSize;
double labrCst= 90;
double subTotal = (price1/9)*carpetSize+labrCst;
String tax1 ="0.5%";
double totl= (price1/9)*carpetSize+labrCst+.5*subTotal;
double fruniture=0;
String zero = "0";
String one= "1";
String two= "2";
String three= "3";
String four= "4";
String five= "5";
String six= "6";
String seven= "7";
String eight= "8";
String nine= "9";
String ten= "10";
Object str= roomCountComboBox.getSelectedItem();
if (roomCountComboBox.getSelectedItem().equals(zero))
{
room= totl;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(one))
{
room= totl*1;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(two))
{
room= totl*2;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(three))
{
room= totl*3;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(four))
{
room= totl*4;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(five))
{
room= totl*5;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(six))
{
room= totl*6;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(seven))
{
room= totl*7;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(eight))
{
room= totl*8;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(nine))
{
room= totl*9;
grandTotalLabel2.setText(String.valueOf(room));
}
if (roomCountComboBox.getSelectedItem().equals(ten))
{
room= totl*10;
grandTotalLabel2.setText(String.valueOf(room));
}
Object str2= furnitureComboBox.getSelectedItem();
if (furnitureComboBox.getSelectedItem().equals(zero))
{
fruniture=room+totl;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(one))
{
fruniture=room+totl*1;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(two))
{
fruniture=room+totl*2;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(three))
{
fruniture=room+totl*3;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(four))
{
fruniture=room+totl*4;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(five))
{
fruniture=room+totl*5;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(six))
{
fruniture=room+totl*6;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(seven))
{
fruniture=room+totl*7;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(eight))
{
fruniture=room+totl*8;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(nine))
{
fruniture=room+totl*9;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
if (furnitureComboBox.getSelectedItem().equals(ten))
{
fruniture=room+totl*10;
grandTotalLabel2.setText(String.valueOf(fruniture));
}
TaxLabel2.setText(String.valueOf(tax1));
laborCostLabel2.setText(String.valueOf(labrCst));
carpetCostLable2.setText(String.valueOf(carptCst));
totalLabel2.setText(String.valueOf(subTotal));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你到底在问什么。如果你把问题收紧一点,我也许可以提供更具体的帮助。但我认为 关于文本字段的官方 Java 教程 的这一部分是相关的:
(强调我的)
作为旁注,这样
写会更好一点:
注意改进的拼写(“funiture”不是一个单词),稍微改进的命名法,以及
if< 中更简洁的谓词/代码>。
I'm not sure what, exactly, you're asking. If you tighten up your question a bit, I might be able to provide more specific help. But I think this part of the official Java tutorial on textfields is relevant:
(emphasis mine)
As a sidenote, this:
would be a bit better-written as this:
Note the improved spelling ("fruniture" is not a word), slightly-improved nomenclature, and more concise predicate in the
if
.