代码运行多次
问题描述:
下面是和我正在执行的代码运行多次的示例代码。代码运行多次
第一类:
public class Test2 extends JFrame {
public static int asd = 0;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnShowtest = new JButton("ShowTest2");
btnShowtest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Test1 a1 = new Test1();
a1.setVisible(true);
dispose();
}
});
contentPane.add(btnShowtest, BorderLayout.CENTER);
}
}
当按钮被按下时这一个打开:
public class Test1 extends JFrame {
public static int ab = 1;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test1() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
contentPane.add(btnrun, BorderLayout.CENTER);
}
}
当按下按钮,它执行该代码:
public class Test3 {
public static void error(){
JOptionPane.showMessageDialog(null, "error");
Test2.asd += 1;
System.err.print(Test2.asd);
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
}
}
现在,每当我按时间该按钮再次执行Test3.error();多次。 例如,如果我按下按钮10次然后Test3.error();运行10次。
我猜这是一个简单的修复,但我无法弄清楚。
编辑:
当我按下它运行“Test3.error()”代码,然后关闭所有的帧,并创建另一个主框架btnrun按钮。
当我回到btnrun并再次按下时,它运行“Test3.error()”2次而不是一次。 2个JOptionePanes并创建两个新的大型机。
如果我再做一遍,它会运行“Test3.error()”3次,并继续这样做。
我想要的是它总是运行“Test3.error()”只是一次,但由于某种原因它没有。
又如:
public class Frame1 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 frame = new Frame1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnRun = new JButton("Run");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Run.run();
}
});
contentPane.add(btnRun, BorderLayout.CENTER);
}
}
并运行该代码:
public class Run {
public static void run(){
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Frame1 newtet = new Frame1();
newtet.setVisible(true);
}
}}
同样的问题。
每次我按下运行按钮,它会执行代码的次数是我过去按下的次数。
按下按钮一旦它消除Frame1并重新创建Frame1。 再按一次,它连续执行2次代码。 (作主帧1,创建帧1,diposes帧1,并创建帧1)
答
是的,你在这里添加的动作侦听器:
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
这意味着“当单击该按钮,呼叫Test3.error()
。 “您是否期望执行一次后自动移除动作侦听器?
编辑:好吧,我想我已经弄清楚发生了什么事。这就是问题所在:
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
对于每现有的框架,你处置该帧,并创建一个新的Test2()
。因此,如果您单击按钮时屏幕上有3个框架(Test1,Test2和Test3),则最终会有三个新帧Test2
。我怀疑你不是这个意思。你的意思是呢?
for (Frame f : Frame.getFrames()){
f.dispose();
}
Test2 newtet = new Test2();
newtet.setVisible(true);
我很困惑,这是什么问题?此外,如果我按下按钮10次,我预计这个按钮执行10次。 – Alexandre 2012-01-31 19:26:37
代码应该总是运行一次,但每次我再次运行它会添加另一个“实例” – Jixi 2012-01-31 19:31:36