我知道关于动作监听器等存在很多问题。 但是,对于我的特定问题,没有任何人可以帮助我...无论我尝试采用哪种方式,我总是会出错。 这是我简单的弹跳球程序:
public class ControlledBall extends JPanel implements Runnable {
int diameter;
long delay;
private int x;
private int y;
private int vx;
private int vy;
public ControlledBall(int xvelocity, int yvelocity) {
diameter = 30;
delay = 40;
x = 1;
y = 1;
vx = xvelocity;
vy = yvelocity;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g.setColor(Color.blue);
g.fillOval(x,y,30,30);
g.setColor(Color.black);
g2.drawOval(x,y,30,30); //draw
}
public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("Something Went Wrong!");
}
move();
repaint();
}
}
public void move() {
if(x + vx < 0 || x + diameter + vx > getWidth()) {
vx *= -1;
}
if(y + vy < 0 || y + diameter + vy > getHeight()) {
vy *= -1;
}
x += vx;
y += vy;
}
public void stop(){
x=0;
y=0;
}
public class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
stop();
}
}
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.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
window.add(stop);
JButton start = new JButton("Start");
start.setSize(100,100);
start.setVisible(true);
start.setText("Start");
window.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
c.ipadx = 400;
c.ipady = 400;
c.gridx = 10;
c.gridy = 10;
window.add(ball2,c);
c.ipadx = 50;
c.ipady = 20;
c.gridx = 10;
c.gridy = 10;
window.pack();
window.setSize(600,600);
window.setLocation(250,200);
window.setVisible(true);
new Thread(ball2).start();
}
}
正如您所看到的,我已经尝试了几种不同的技术,但都没有奏效。任何建议将不胜感激。 谢谢
我得到的主要错误是:
non static field cannot be referenced from a static context
我认为那是因为我正在从main方法运行它。
答案 0 :(得分:2)
您真正想做的第一件事是摆脱run
方法(以及对Runnable
的依赖)。这确实是在Swing中执行定期更新的不适当方法。
Swing是单线程的,并且不是线程安全的,您当前的方法面临跨线程边界进行脏读/写操作的风险。
相反,您想使用Swing Timer
,有关更多详细信息,请参见How to Use Swing Timers。
接下来要做的是添加一个start
方法并更新stop
方法以支持使用Swing Timer
...
public class ControlledBall extends JPanel {
//...
private Timer timer;
public void stop() {
if (timer == null) {
return;
}
timer.stop();
timer = null;
x = 0;
y = 0;
}
public void start() {
if (timer != null) {
return;
}
timer = new Timer(delay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
});
timer.start();
}
然后,只需更新您的开始和停止按钮即可调用这些方法...
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ball2.stop();
}
});
//...
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ball2.start();
}
});