【发布时间】:2014-03-12 18:20:13
【问题描述】:
在这里,我创建了一个游戏,您可以在其中掷骰子并一次画一个部分的虫子。这是 哪些卷可以为您提供哪些零件以及您需要多少。
- 1 - 身体(需要一个)
- 2 - 头(需要一个;必须先有身体)
- 3 - 眼睛(需要两个,必须先有头)
- 4 - 天线(需要两个;必须先有头)
- 5 - 腿(需要六个;必须先有身体)
- 6 - 尾巴(需要一个;必须先有身体)
您必须单击身体部位附近的屏幕才能显示出来。然后我们 可以计时整个事情。
我使用面板来处理我的计时器,但我没有发现问题所在?
主程序
package dice;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Yahtzee extends JFrame implements ActionListener
{
Die[] d; // array to hold the 5 dice
FourOfAKind[] f;
JPanel buttonPanel; // panel for the timer
JPanel dicePanel; // panel to hold the dice
JPanel bugPanel;
ScoreRow[] theScores;
protected Object bug;
public static void main(String[] args) {
Yahtzee y = new Yahtzee();
}
public Yahtzee()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setLayout(new BorderLayout());
dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(5, 1));
dicePanel.setLayout(new FlowLayout());
dicePanel.setSize(new Dimension(50, 50));
add(dicePanel, BorderLayout.CENTER);
d = new Die[1];
for (int i = 0; i < 1; i++) {
d[i] = new Die(this);
dicePanel.add(d[i]);
}
bugPanel = new JPanel();
bugPanel.setLayout(new GridLayout(5, 5));
bugPanel.setLayout(new FlowLayout());
bugPanel.setSize(new Dimension(50, 50));
add(bugPanel, BorderLayout.SOUTH);
f = new FourOfAKind[1];
for (int w = 0; w < 1; w++) {
f[w] = new FourOfAKind(this);
bugPanel.add(f[w]);
}
setSize(new Dimension(715, 705));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setChoice(int choice)
{
f[0].setChoice(choice);
}
public void drawBug()
{
f[0].setChoice(d[0].getChoice());
f[0].drawBug();
}
}
我注释掉了未编译的计时器内容。
【问题讨论】:
-
有什么问题?你期望什么行为?会发生什么?
-
我期待在我画完我的 bug 后,我会点击面板并停止计时器,看看我画 bug 需要多长时间。
-
您不能启动计时器,然后构建一个新的计时器对象,然后停止该计时器。您正在用一个新对象替换您的
timer对象... -
哦,好吧,如果你不介意给我看的话,那该怎么做?