【发布时间】:2014-07-02 04:26:35
【问题描述】:
我创建了一个程序,每次单击按钮(执行按钮)时读取文件并计算值并将总数打印到文本字段,我将此文本字段附加到 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");
}
【问题讨论】:
-
你的
in1.close();方法是什么?显示整个代码! -
在尝试打开Frame时每次都创建JFrame类的实例还是只创建一次实例并每次都使用这个实例。 ?请检查这个。发布MVCE以获得更好的帮助
标签: java swing jtextfield