简体   繁体   English

JDialog不处理

[英]JDialog doesn't dispose

I'm new into programming and I'm having some trouble with this. 我是编程新手,对此有些麻烦。

The problem is, I'm using the Swing palette to create an assignment where I'm using a JDialog to display a timer at the same time of another frame, when I dispose this frame to change to another and return to the previous one the timer in the JDialog overlaps the first one that was running, and I couldn't managed to fix it. 问题是,我使用Swing调色板创建任务,其中当我使用JDialog在另一个框架中放置计时器以将其更改为另一个框架并返回到前一个框架时,使用JDialog来显示计时器。 JDialog中的计时器与正在运行的第一个计时器重叠,我无法修复它。

Here's the code. 这是代码。

MAIN 主要

public static void main(String[] args) {
    Panel0 screen=new Panel0();
    screen.setTitle("");
    screen.setLocationRelativeTo(screen);
    screen.setVisible(true);

}

1st FRAME 第一帧

public class Panel0 extends javax.swing.JFrame {
Panel s=new Panel();

private void fisica1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    s.time();
    s.setTitle("FISIC I");
    s.setLocationRelativeTo(s);
    s.setVisible(rootPaneCheckingEnabled);
    s.dialog.setVisible(rootPaneCheckingEnabled);
    dispose();
}          

2nd FRAME 第二帧

public class Panel extends javax.swing.JFrame {    

private void EndActionPerformed(java.awt.event.ActionEvent evt) {                                    
    dialog.dispose();
    dialog.setDefaultCloseOperation(0);

    Panel0 pan=new Panel0();
    pan.setLocationRelativeTo(p1);
    pan.setVisible(rootPaneCheckingEnabled);
    dispose();

}   

void time(){        

    t=new Timer(1,new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (startTime<0) {
                startTime=System.currentTimeMillis();                    
            }                
               long now = System.currentTimeMillis();
               long clockTime = now - startTime;                
            if (clockTime >= duration) {
                    clockTime = duration;
                    t.stop();
            }                
             SimpleDateFormat sdf=new SimpleDateFormat("mm:ss:SS");
                              clock.setText(sdf.format(duration-clockTime));                                  


       }
    });        
    t.setInitialDelay(0);            
                if (!t.isRunning()) {
                    startTime = -1;
                    t.start();
                }
}

I omitted the inizialization of the Timer and such, because I don't think that's the problem. 我省略了Timer等的inizinization,因为我不认为这是问题所在。

To clarify something: Once I close the 2nd frame the 1st opens and gives me options to repeat this process over and over, and everytime the JDialog named "dialog" overlaps with its data (you can see the numbers of the clock overlaping). 澄清一下:关闭第2帧后,第1帧将打开,并为我提供重复执行此过程的选项,并且每次名为“ dialog”的JDialog与其数据重叠时(您可以看到时钟数重叠)。

dispose() does not means that you will "destroy" the object or clear its state. dispose()并不意味着您将“破坏”对象或清除其状态。 It means that you will release graphics resources attached to that frame (low level window handle and stuff). 这意味着您将释放附加到该框架的图形资源(低级窗口句柄和填充物)。 It still can be reused with setVisible(true) 仍然可以使用setVisible(true)重复使用

I assume that you want to reuse our popup - this is just fine, but I think that you are forgetting to stop the "disposed" timer thus every new timer you create on action will be exposed to so called "racing conditions". 我假设您想重用我们的弹出窗口-很好,但是我认为您忘记了停止“已配置”计时器,因此您在操作中创建的每个新计时器都将面临所谓的“竞赛条件”。

Timers are simple background task and they must be stopped explicitly - it will not be done by itself. 计时器是简单的后台任务,必须明确地停止它们-它本身不会完成。

Every call to s.time(); 每次调用s.time(); starts new timer without stopping previous one. 在不停止前一个计时器的情况下启动新计时器。

Simply speaking: you have multiple timers updating the same text field. 简而言之:您有多个计时器来更新同一文本字段。

Solution: Stop previous timer before running new OR restart previous timer. 解决方案:在运行新计时器之前停止上一个计时器,或者重新启动上一个计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM