【发布时间】:2019-02-01 10:18:01
【问题描述】:
public static void main(String[] args) {
ControlledBall ball2 = new ControlledBall(12,2);
JFrame window = new JFrame("Controlled Ball");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton stop = new JButton("Stop");
stop.setSize(4,400);
stop.setVisible(true);
stop.setText("Stop");
stop.addActionListener(new Action());
我在最后一行显示“受控球。无法从静态上下文引用”
当我尝试以下技术而不是调用 stop() 方法时,我只是更改我需要更改的值:
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
x= 0;
y = 0;
}
});
我得到错误 non-static field 'x' cannot be referenced from a static context...
问题是,如何从主方法更改在另一个方法中声明的 x 和 y 的值?
【问题讨论】:
标签: java button listener action