为什么下面的代码不起作用?基本上,这是一个更困难的程序的简化版本,我在其中尝试创建一个可运行的初始屏幕,然后将选择按钮链接到不同的可运行的按钮,但这并不像我预期的那样运行。从另一个Runnable中运行Runnable的Java将不起作用
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();
}
}
您是否尝试过在你的第一个可运行来验证添加调试语句?正如所写的,它只是一个直接的函数调用,没有什么特别的... – Krease