gpt4 book ai didi

Java 操作监听器问题

转载 作者:行者123 更新时间:2023-12-02 06:04:54 25 4
gpt4 key购买 nike

我有一个在 Jpanel 中为球制作动画的程序。我有两个按钮“停止”和“开始”。 Stop 使球停止移动,Go 则使球四处移动。在我的球类中,我有一个 boolean 变量,如果为 true,则球会移动,如果为 false,则球不会移动。所以我想在我的主类中,当我创建框架并将球类放入面板中时,我可以使用按钮根据按钮按下情况将变量更改为 false 或 true。

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​​ 类中的 boolean 值。有什么想法吗?

最佳答案

研究使用javax.swing.Timer。您的 while 循环将阻止 EDT,不允许调度按钮事件。

参见How to use Swing Timers .

使用计时器,您可以使用它的方法 stopstart 来启动和停止计时器。你可以看一个例子here

<小时/>

这是一个在您的代码中使用Timer 的可运行示例。上面链接中的代码更清晰,使用 Ball 对象。我很快就把这个拼凑在一起了

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class BallTask {

public static boolean run = false;
private Timer timer;
private BallPanel ballPanel = new BallPanel();

public BallTask() {
timer = new Timer(30, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (ballPanel.x < 0 || ballPanel.x > ballPanel.getWidth()) {
ballPanel.dx = -ballPanel.dx;
}
if (ballPanel.y < 0 || ballPanel.y > ballPanel.getHeight()) {
ballPanel.dy = -ballPanel.dy;
}
// Adjust ball position
ballPanel.x += ballPanel.dx;
ballPanel.y += ballPanel.dy;
ballPanel.repaint();
}
});
JPanel buttons = new JPanel();
JButton stop = new JButton("STOP");
buttons.add(stop);
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
}
});
JButton go = new JButton("GO");
buttons.add(go);
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});

JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(ballPanel);
mainPanel.add(buttons, BorderLayout.SOUTH);

JFrame window = new JFrame();
window.add(mainPanel);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);

}

private class BallPanel extends JPanel {

private int x;
private int y;
int dx = 4; // Increment on ball's x-coordinate
int dy = 4; // Increment on ball's y-coordinate
int radius = 15; // Ball radius

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 30, 30);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BallTask();
}
});
}
}

关于Java 操作监听器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com