爲什麼下面的代碼不起作用?基本上,這是一個更困難的程序的簡化版本,我在其中嘗試創建一個可運行的初始屏幕,然後將選擇按鈕鏈接到不同的可運行的按鈕,但這並不像我預期的那樣運行。從另一個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