呃,内容过长,我分成两段吧,这是后半部分 // the only listener I really use @Override public void mouseClicked(MouseEvent e) { // if we click on a label that does not have a value ignore it if (value == -1) { return; } // validate that I am the good label if (cl[nbClicked].value == value) { // remove the circle and display the bumber setText(valueToDisplay); // ok we will check the next one nbClicked++; score += 5 * ((6 - remtime)*10); // 加分 // check if we are done if (nbClicked == cl.length) { statusLabel.setText("你赢了!\n"); // 弹出窗口,告知用户将进行难度更大的一轮 JOptionPane.showMessageDialog(this, "恭喜过关,即将进入难度更大的一轮,\r\n本轮你需要记住" + (cirnum + 1) + "个数字!\r\n准备好了吗?"); cirnum++; // 增加难度 run(); // 自动开始新的 一轮 // ok ready to start a new game btnStart.setEnabled(true); } else { statusLabel.setText("已猜对" + nbClicked + "个,共" + cl.length + "个。"); } } else { // 如果出错~~ // display the values of the selected label this is done by // setting them to their values // the method will reset valueToDisplay for (int i = 0; i < cl.length; i++) { cl[i].setValue(cl[i].value); } badChoice(); // 在出错的label上标红、画叉 statusLabel.setText("你输了~~\n"); JOptionPane.showMessageDialog(this, "Game Over! \r\n你共获得了" + score + "分"); // 判断用户得分是否满足入榜条件,score就是得分 // 如满足弹框要求用户输入名字,并更新排行榜文本 // 然后弹出一次排行榜,让用户看一下排名 // 佳旺!这里靠你了!!~~ // 重置 cirnum = 4;// 重置数字数量为4 btnStart.setEnabled(true); // 点亮“开始游戏”,准备下一轮 } } // 如果选错,打X,并显示为红色 void badChoice() { displayX = true; // flag next repaint will have to paint the X setForeground(Color.RED); // and the culprit will display it's value // in RED } // unused MouseListener methods public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent arg0) { } public void mouseMoved(MouseEvent arg0) { } // a compareTo method to sort the label according to their instance // variable value public int compareTo(CircleLabel otherCircleLabel) { // just return if < or > to the one we compare return value - otherCircleLabel.value; } } // the panel that will be put South to select number of Circle to display // and the go button class BottomPanel extends JPanel { // 3 radio buttons for 4, 5, 6 circles // JRadioButton[] radio = new JRadioButton[3]; // Constructor receives as parameter our JFrame which is the listener BottomPanel(ActionListener al) { // 1 row and 5 columns: the title, 3 radio, the button, and empty // label super(new GridLayout(1, 6)); // add a background color setBackground(Color.PINK); // //删掉这里 // // add a label to explain what the RadioButtons are for // add(new JLabel("圈数:")); // // a buttonGroup so only 1 radio can be selected at the same time // ButtonGroup bg = new ButtonGroup(); // // add our RadioButton // for (int i = 0; i < radio.length; i++) // { // // label of Radio index + 4 // radio[i] = new JRadioButton("" + (i + 4)); // radio[i].setBackground(Color.PINK); // // add RadioButton to the group and then to the panel // bg.add(radio[i]); // add(radio[i]); // } // // make the firt one selected // radio[0].setSelected(true); // add the button btnStart.addActionListener(al); // an empty label add(new JLabel("")); btnExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 退出按钮 System.exit(0); } }); btnConf.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent e) { // 弹出配置游戏窗口 GameConfig gc=new GameConfig(); gc.setLocation(200, 200); gc.setSize(300, 150); gc.show(); } }); btnRank.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 显示排行榜 JOptionPane .showMessageDialog( GuessWhereAreTheNumbers.this, "第一名: 10000 \r\n第二名: 10000 \r\n第三名: 10000 \r\n第四名: 10000 \r\n第五名: 10000 \r\n", "排行榜", 1); } }); btnHelp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 显示排行榜 JOptionPane.showMessageDialog(GuessWhereAreTheNumbers.this, "这里填写游戏的帮助", "游戏帮助", 1); } }); add(btnStart); add(btnConf); add(btnRank); add(btnHelp); add(btnExit); } } }
测试了以下,进入下一轮应启动线程,而不是简单的run(). 修改一下,我测试可以了。 把类CircleLabel的方法 public void mouseClicked(MouseEvent e)里的语句run() 改成:new Thread(GuessWhereAreTheNumbers.this).start();
// the only listener I really use
@Override
public void mouseClicked(MouseEvent e)
{
// if we click on a label that does not have a value ignore it
if (value == -1)
{
return;
}
// validate that I am the good label
if (cl[nbClicked].value == value)
{
// remove the circle and display the bumber
setText(valueToDisplay);
// ok we will check the next one
nbClicked++;
score += 5 * ((6 - remtime)*10); // 加分
// check if we are done
if (nbClicked == cl.length)
{
statusLabel.setText("你赢了!\n");
// 弹出窗口,告知用户将进行难度更大的一轮
JOptionPane.showMessageDialog(this,
"恭喜过关,即将进入难度更大的一轮,\r\n本轮你需要记住" + (cirnum + 1)
+ "个数字!\r\n准备好了吗?");
cirnum++; // 增加难度
run(); // 自动开始新的 一轮
// ok ready to start a new game
btnStart.setEnabled(true);
} else
{
statusLabel.setText("已猜对" + nbClicked + "个,共" + cl.length
+ "个。");
} } else
{
// 如果出错~~
// display the values of the selected label this is done by
// setting them to their values
// the method will reset valueToDisplay
for (int i = 0; i < cl.length; i++)
{
cl[i].setValue(cl[i].value);
}
badChoice(); // 在出错的label上标红、画叉
statusLabel.setText("你输了~~\n");
JOptionPane.showMessageDialog(this, "Game Over! \r\n你共获得了"
+ score + "分"); // 判断用户得分是否满足入榜条件,score就是得分
// 如满足弹框要求用户输入名字,并更新排行榜文本
// 然后弹出一次排行榜,让用户看一下排名 // 佳旺!这里靠你了!!~~ // 重置
cirnum = 4;// 重置数字数量为4
btnStart.setEnabled(true); // 点亮“开始游戏”,准备下一轮
}
} // 如果选错,打X,并显示为红色
void badChoice()
{
displayX = true; // flag next repaint will have to paint the X
setForeground(Color.RED); // and the culprit will display it's value
// in RED
} // unused MouseListener methods
public void mouseEntered(MouseEvent e)
{
} public void mouseExited(MouseEvent e)
{
} public void mousePressed(MouseEvent e)
{
} public void mouseReleased(MouseEvent e)
{
} public void mouseDragged(MouseEvent arg0)
{
} public void mouseMoved(MouseEvent arg0)
{
} // a compareTo method to sort the label according to their instance
// variable value
public int compareTo(CircleLabel otherCircleLabel)
{
// just return if < or > to the one we compare
return value - otherCircleLabel.value;
}
} // the panel that will be put South to select number of Circle to display
// and the go button
class BottomPanel extends JPanel
{
// 3 radio buttons for 4, 5, 6 circles
// JRadioButton[] radio = new JRadioButton[3]; // Constructor receives as parameter our JFrame which is the listener
BottomPanel(ActionListener al)
{
// 1 row and 5 columns: the title, 3 radio, the button, and empty
// label
super(new GridLayout(1, 6));
// add a background color
setBackground(Color.PINK); // //删掉这里
// // add a label to explain what the RadioButtons are for
// add(new JLabel("圈数:"));
// // a buttonGroup so only 1 radio can be selected at the same time
// ButtonGroup bg = new ButtonGroup();
// // add our RadioButton
// for (int i = 0; i < radio.length; i++)
// {
// // label of Radio index + 4
// radio[i] = new JRadioButton("" + (i + 4));
// radio[i].setBackground(Color.PINK);
// // add RadioButton to the group and then to the panel
// bg.add(radio[i]);
// add(radio[i]);
// }
// // make the firt one selected
// radio[0].setSelected(true); // add the button
btnStart.addActionListener(al); // an empty label
add(new JLabel("")); btnExit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// 退出按钮
System.exit(0);
}
});
btnConf.addActionListener(new ActionListener()
{
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e)
{
// 弹出配置游戏窗口
GameConfig gc=new GameConfig();
gc.setLocation(200, 200);
gc.setSize(300, 150);
gc.show();
}
});
btnRank.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// 显示排行榜
JOptionPane
.showMessageDialog(
GuessWhereAreTheNumbers.this,
"第一名: 10000 \r\n第二名: 10000 \r\n第三名: 10000 \r\n第四名: 10000 \r\n第五名: 10000 \r\n",
"排行榜", 1);
}
});
btnHelp.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// 显示排行榜
JOptionPane.showMessageDialog(GuessWhereAreTheNumbers.this,
"这里填写游戏的帮助", "游戏帮助", 1);
}
});
add(btnStart);
add(btnConf);
add(btnRank);
add(btnHelp);
add(btnExit);
}
}
}
修改一下,我测试可以了。
把类CircleLabel的方法 public void mouseClicked(MouseEvent e)里的语句run() 改成:new Thread(GuessWhereAreTheNumbers.this).start();
代码很整洁,个人感觉应该是照着例子写的吧?(别介意,只是感觉,新手写的代码一般都很乱的)
我也是一个新手,同样也在做一个射击类型的小游戏。在网上等待别人帮忙调错是很慢的,技术上的却是得请别人帮忙,可逻辑上的错误一般是自己可以解决的。
你说“第一关过关后,自动跳到第二关,却不能正确显示数字了。”
那你就加一个system.out.println("测试");语句放到运行第二关的线程程序中,看看控制台是否打印就知道是不是线程未启动的错误了。如果运行,那就是Jpanel显示的问题。
另外说明一下,对于错误的描述尽可能的多一些,这样利于判断错误的原因。
System.out.println("hello");
看程序进入了那部分,