Jtextfield中如何处理异常?

发布于 2024-11-24 07:38:03 字数 5795 浏览 4 评论 0原文

我正在尝试处理此代码,我想知道如何捕获 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-12-01 07:38:03

我不确定你到底在问什么。如果你把问题收紧一点,我也许可以提供更具体的帮助。但我认为 关于文本字段的官方 Java 教程 的这一部分是相关的:

由于 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;
...

注意改进的拼写(“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:

Because the JTextField class inherits from the JTextComponent class, text fields are very flexible and can be customized almost any way you like. For example, you can add a document listener or a document filter to be notified when the text changes, and in the filter case you can modify the text field accordingly.

(emphasis mine)


As a sidenote, this:

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

would be a bit better-written as this:

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

Note the improved spelling ("fruniture" is not a word), slightly-improved nomenclature, and more concise predicate in the if.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文