为什么以下代码不起作用?基本上,这是一个更难的程序的简化版本,在该程序中,我试图制作一个可运行的初始屏幕,其中包含一些选项,然后将按钮链接到不同的可运行文件,但这并没有按我预期的那样运行。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Runnables {
static Runnable runone;
static Runnable runtwo;
static JFrame frame = new JFrame();
static JButton button1 = new JButton("Initial screen");
static JButton button2 = new JButton("After button click screen");
public static void main(String[] args) {
runone = new Runnable() {
@Override
public void run() {
frame.removeAll();
frame.revalidate();
frame.repaint();
frame.add(button2);
}
};
runtwo = new Runnable() {
@Override
public void run() {
frame.setSize(800, 600);
frame.setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
runone.run();
System.out
.println("This is performed, but the button doesnt change");
}
});
frame.add(button1);
}
};
runtwo.run();
}
}