0
我有一个程序在Jpanel中激活一个球。我有两个按钮停止和去。停止停止球移动,去为球移动。在我的球类中,我有一个布尔变量,如果它是真的,那么球移动,如果它是假的,球不移动。所以当我创建框架并在面板中放置球类时,我想在我的主类中,我可以使用按钮将变量更改为false或true,具体取决于按钮的按下。Java动作监听器问题
public class BallTask extends JPanel implements ActionListener{
public static boolean run = false;
public BallTask(){
this.setPreferredSize(new Dimension(width, height));
Thread gameThread = new Thread() {
public void run() {
while (run) {
.... working code
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new BallTask());
JPanel buttons = new JPanel();
JButton stop = new JButton("STOP");
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
run = false;
}
});
JButton go = new JButton("GO");
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
run = true;
}
});
buttons.add(go);
buttons.add(stop);
window.add(buttons,BorderLayout.SOUTH);
window.pack();
window.setVisible(true);
}
});
我对代码的问题是,按钮实际上并没有改变BallTask类中的布尔值。有任何想法吗?
几个工作示例进行了审查[这里](http://stackoverflow.com/q/9849950/230513)。 – trashgod
那么问题是什么? –