关闭JFrame时如何刷新文本字段的值

时间:2014-07-02 04:26:35

标签: java swing jtextfield

我创建了一个程序来读取文件并计算值并在每次单击按钮(执行按钮)时将总计打印到文本字段,我将此文本字段附加到JFrame中。

我的代码运行良好但我的问题是每次关闭JFrame并再次单击该按钮时,值是我的JTextField保留,并且在我第二次单击执行按钮时,总计将乘以2。谁能帮我?我想在每次关闭JFrame时刷新我的JTextfield。这是我使用的代码。谢谢!

      ArrayList<String>totalResult= newArrayList<String>();
      try
      {
        FileInputStream f1 = new FileInputStream(AddressFile);
        DataInputStream in1 = new DataInputStream(f1);
        BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
        String lines;

        while((lines = br1.readLIne())!=null)
        {
           if(line.length()!=0 && (lines.charAt(0)!='#'))
           {
              String[] employee = lines.split("\\s+");
              totalResult.add(employee[2]);
           }
        }
        for(String s : totalResult)
        {
           vacationLeave = Double.parseDouble(s);
           totalVacationLeave +=vacationLeave
        } 
        try
        {
          while(!loopVacationLeave)
          try
          {
             BigDecimal roundOff = new DigDecimal(totalVacationLeave).setScale(3, 
             RoundingMode.HALF_EVEN;)
             totalVacationLeave = roundOff.doubleValue();

             if(SUMMARY_MONITORING_PERSONNEL.employeeVL7.getText()!=null)
               SUMMARY_MONITORING_PERSONNEL.employeeVL7.setText("" + 
                             totalVacationLeave);
               loopVacationLeave = true;
           }
           catch(Exception e)
           {

           }
           }
           catch(Exception e)
           {

           }
           in1.close();
           }
           catch(Exception e)
           {
             SUMMARY_MONITORING_PERSONNEL.employeeVL7.setText("No File");
           }

1 个答案:

答案 0 :(得分:2)

你正在做什么?节目。 EXIT_ON_CLOSEDO_NOTHING_ON_CLOSEHIDE_ON_CLOSEDISPOSE_ON_CLOSE,如果您要退出或处置,则无法执行此操作。 如果你想保存你的框架状态以便隐藏它,(当用户单击顶部的X按钮时)它将只对屏幕不可见,并且可以进行任何修改而不是你需要显示它。

您可以手动frame.setVisible(true|false);

also see this