gpt4 book ai didi

java - 在 Java 中,我应将 Timer 类事件监听器附加到哪些对象?

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

我正在尝试创建一个打地鼠游戏。我已经使用 swing 来创建背景并添加带有事件监听器的痣图像,每次单击它们时都会增加分数,但我在设置它们是否应该可见时遇到问题。我认为最好的方法是使用计时器来设置/重置 boolean 值(vis)。随机化图像可见的时间段是理想的选择。我曾多次尝试使用 Swing 计时器,但似乎不起作用。我在哪里实例化计时器,以及将在计时器倒计时后执行代码的事件监听器附加到什么?

package whackmole;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class WhackAMole extends JFrame {

public WhackAMole() {
createAndShowGUI();
}

static int score = 0;
public static JLabel scoreDisplay;
boolean vis;

public static void main(String[] args) throws Exception {
// run asynchronously
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static void createAndShowGUI() {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(600, 600));
Holes holes = new Holes(frame);
frame.getContentPane().add(holes);
holes.setLayout(null);
frame.pack();
frame.setVisible(true);

scoreDisplay = new JLabel("Score: " + score);
scoreDisplay.setBounds(239, 11, 84, 38);
holes.add(scoreDisplay);

Mole mole = new Mole(68, 92, true);
mole.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
score++;
scoreDisplay.setText("Score: " + score);
}
});
holes.add(mole);

Mole mole2 = new Mole(181, 320, false);
mole2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
score++;
scoreDisplay.setText("Score: " + score);
}
});
holes.add(mole2);

Mole mole3 = new Mole(414, 439, true);
mole3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
score++;
scoreDisplay.setText("Score: " + score);
}
});
holes.add(mole3);

Mole mole4 = new Mole(297, 203, false);
mole4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
score++;
scoreDisplay.setText("Score: " + score);
}
});
holes.add(mole4);
}
}

最佳答案

在这种情况下,您可以实例化您的 Timer具有固定速率和实现 ActionListener 的类。

public class Example extends JPanel implements ActionListener {
private static final int RATE = 1000 / 8; // ~8 Hz
private final Timer timer = new Timer(RATE, this);
}

在此完整的 exampleGameButtonJToggleButton 的子类,ActionListener 的实现只是切换随机选择的 GameButton 的状态.

private final List<GameButton> buttons = new ArrayList<GameButton>(MAX);
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == timer) {
int index = random.nextInt(game.max());
GameButton gb = buttons.get(index);
gb.setSelected(!gb.isSelected());
}
...
}

为了区分状态,该示例使用 Unicode 字形,但您可以使用 setIcon()setSelectedIcon()

关于java - 在 Java 中,我应将 Timer 类事件监听器附加到哪些对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634043/

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