【发布时间】:2016-06-08 05:22:05
【问题描述】:
我是编程新手,但遇到了一些麻烦。
问题是,我正在使用 Swing 调色板创建一个分配,我使用 JDialog 在另一个框架的同时显示一个计时器,当我将此框架更改为另一个框架并返回到前一个 JDialog 中的计时器与第一个正在运行的计时器重叠,我无法修复它。
这是代码。
主要
public static void main(String[] args) {
Panel0 screen=new Panel0();
screen.setTitle("");
screen.setLocationRelativeTo(screen);
screen.setVisible(true);
}
第一帧
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();
}
第二帧
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();
}
}
我省略了 Timer 的初始化等,因为我认为这不是问题。
澄清一下:一旦我关闭第二帧,第一帧就会打开并给我选项来一遍又一遍地重复这个过程,每次名为“对话框”的 JDialog 与其数据重叠(你可以看到时钟的数字重叠)。
【问题讨论】:
-
“这是代码。”代码丢失!如需更好的帮助,请尽快发布minimal reproducible example 或Short, Self Contained, Correct Example。
标签: java swing dispose jdialog