版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/twentyonepilots/article/details/81269381
辛明影老师的Java选修课相关。
记忆测试小游戏内容及流程见这一篇博文:C语言 记忆测试(Memory Test)小游戏的实现
时间进度条的添加没有成功,因此先不添加进度条,详见Java jProgressBar进度条的玄学;
(1)数字串输出
调用java.util.Random包中的random.nextInt()方法,通过一个循环产生指定长度的数字串。
(2)数字串消失
新建一个线程,通过线程的sleep()方法使数字串停留一定的时间,之后对所在的JLable进行清空内容的处理使数字串消失。
(3)数字串输入
在数字串显示在屏幕上的时候,对进行输入的文本字段进行.setEditable(false)操作使其不能输入内容,玩家只能记忆;数字串消失后通过.setEditable(true)使文本字段恢复编辑。
(4)游戏的判定
输入结束按钮的响应的事件除了包括以上的部分内容,还包括:判断输入的数字串是否和给定的相同,分别弹出不同的对话框:如果相同,数字串加长时间加长,游戏继续;如果不同,游戏结束,给出分数(猜对的数字串)长度之和。
参考资料:
- JAVA中修改JOptionPane的字体样式(Font),但是按钮的字体设置不成功。
java中怎么实现让一串字出现停留一会又消失的、 java sleep延时效果出不来、java 等待一秒方法,注意必须新建一个线程,在线程中sleep;
源代码如下:
package memorytest;
import java.awt.Font;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
public class MemoryTestFrame extends javax.swing.JFrame {
int time =1000; //设置记忆时间
int len = 3; //设置数字串长度
String num = ""; //初始化数字串
String num_c = ""; //复制字符串便于比较
int score = 0; //设置分数
/**
* Creates new form MemoryTestFrame
*/
public MemoryTestFrame() {
this.setBounds(500, 250, 500, 450);
this.setTitle("Memory Test");
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("楷体", 0, 54)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("记忆测试");
jLabel2.setFont(new java.awt.Font("幼圆", 0, 24)); // NOI18N
jLabel2.setText("Remember the numbers and input them to continue!");
jTextField1.setFont(new java.awt.Font("宋体", 0, 36)); // NOI18N
jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 30)); // NOI18N
jLabel3.setText("Input numbers:");
jButton1.setFont(new java.awt.Font("微软雅黑", 0, 18)); // NOI18N
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("微软雅黑", 0, 18)); // NOI18N
jButton2.setText("Ready&Begin");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Aharoni", 1, 54)); // NOI18N
jLabel4.setText("Memory Test");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(152, 152, 152)
.addComponent(jLabel4)
.addGap(137, 137, 137))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 589, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 588, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 589, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(214, 214, 214)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(99, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(151, Short.MAX_VALUE))
);
//将按按钮之前的部分组件设置为不可见
jButton1.setVisible(false);
jLabel1.setVisible(false);
jLabel3.setVisible(false);
jTextField1.setVisible(false);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//主要部件在按按钮之后出现
jButton2.setVisible(false);
jButton1.setVisible(true);
jLabel1.setVisible(true);
jLabel3.setVisible(true);
jTextField1.setVisible(true);
//生成随机的数字串
Random random = new Random();
for (int i = 0; i < len; i++) {
num += random.nextInt(10);
}
//使数字串出现一段时间后消失
Thread t = new Thread(new Runnable() {
public void run() {
try {
jLabel1.setText(num);
jTextField1.setEditable(false); //数字串出现的时候不能输入
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
jTextField1.setEditable(true);
jLabel1.setText(""); //清空字符串
num_c = num;
num = "";
}
});
t.start(); //启动线程
time += 1000; //时间加一秒
len += 2; // 数字多两个
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
UIManager.put("OptionPane.messageFont",
new FontUIResource(new Font("Times New Roman", 0, 33)));//设置对话框字体
if (jTextField1.getText().compareTo(num_c) == 0) {
score += num_c.length(); //分数是猜对数字之和
JOptionPane.showMessageDialog(null,
"Right!Continue.", "Succeed",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,
"Gameover!Your score is:" + score, "Fail",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
jTextField1.setText("");
//生成随机的数字串
Random random = new Random();
for (int i = 0; i < len; i++) {
num += random.nextInt(10);
}
//使数字串出现一段时间后消失
Thread t = new Thread(new Runnable() {
public void run() {
try {
jLabel1.setText(num);
jTextField1.setEditable(false);//数字串出现的时候不能输入
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
jTextField1.setEditable(true);
jLabel1.setText(""); //清空字符串
num_c = num;
num = "";
}
});
t.start(); //启动线程
time += 1000; //时间加一秒
len += 2; // 数字多两个
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MemoryTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MemoryTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MemoryTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MemoryTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MemoryTestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}