c语言记忆测试游戏,Java 记忆测试(Memory Test)小游戏的实现及GUI设计

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")

//

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();

}//

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 */

//

/* 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);

}

//

/* 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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值