如何在Jtextfield中做异常?

时间:2011-07-17 21:58:18

标签: exception jtextfield

我正在尝试获取有关此代码的工作我想知道如何能够在Jtextfield中捕获异常,并且我还想限制您可以放入Jtextfield中的数量。 Plz让我知道我怎么能这样做我尝试了不同的东西,但没有任何工作,我也使用桌面应用程序在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));


    }                                         

1 个答案:

答案 0 :(得分:0)

我不确定你究竟是在问什么。如果你稍微收紧一下你的问题,我可能会提供更具体的帮助。但我认为official Java tutorial on textfields的这一部分是相关的:

  

由于JTextField类继承自JTextComponent类,因此文本字段非常灵活,几乎可以按照您喜欢的方式进行自定义。例如,您可以添加文档侦听器或文档过滤器以在文本更改时收到通知,并且在过滤器情况下可以相应地修改文本字段

(强调我的)


作为旁注,这是:

  Object str2= furnitureComboBox.getSelectedItem();
  if (furnitureComboBox.getSelectedItem().equals(zero))
  {
      fruniture=room+totl;
  ...

会像这样写得好一点:

Object selectedFurniture = furnitureComboBox.getSelectedItem();
if(selectedFurniture.equals(zero))
{
    furniture = room + total;
...

请注意改进的拼写(“fruniture”不是单词),略有改进的命名法,以及if中更简洁的谓词。