java guess_guess.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import java.io.FileNotFoundException;

import java.util.Scanner;

/**

*

* @author spring

*/

public class guess extends javax.swing.JFrame {

/**

* Creates new form guess

*/

public guess() {

initComponents();

success = true;

}

/**

* 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() {

menuBar1 = new java.awt.MenuBar();

menu1 = new java.awt.Menu();

menu2 = new java.awt.Menu();

IN = new javax.swing.JTextField();

OUT = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

menu1.setLabel("File");

menuBar1.add(menu1);

menu2.setLabel("Edit");

menuBar1.add(menu2);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

IN.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

INActionPerformed(evt);

}

});

OUT.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

OUTActionPerformed(evt);

}

});

jButton1.setText("开始");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("排行榜");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

try {

jButton2ActionPerformed(evt);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

jLabel1.setText("输入");

jLabel2.setText("输出");

jLabel3.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N

jLabel3.setText("猜数游戏");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,

layout.createSequentialGroup().addContainerGap(152, Short.MAX_VALUE)

.addGroup(layout

.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(

javax.swing.GroupLayout.Alignment.TRAILING,

layout.createSequentialGroup().addComponent(jLabel2).addGap(18, 18, 18)

.addComponent(OUT, javax.swing.GroupLayout.PREFERRED_SIZE, 137,

javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,

layout.createSequentialGroup().addComponent(jLabel1).addGap(18, 18, 18)

.addComponent(IN, javax.swing.GroupLayout.PREFERRED_SIZE, 137,

javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGap(216, 216, 216))

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup().addGap(116, 116, 116).addComponent(jButton1)

.addGap(140, 140, 140).addComponent(jButton2))

.addGroup(layout.createSequentialGroup().addGap(201, 201, 201).addComponent(jLabel3)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout

.createSequentialGroup().addContainerGap(51, Short.MAX_VALUE)

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 38,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(28, 28, 28)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(IN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel1))

.addGap(28, 28, 28)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(OUT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel2))

.addGap(40, 40, 40).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton1).addComponent(jButton2))

.addGap(53, 53, 53)));

pack();

}//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

start = new java.util.Date();

System.out.println(start);

if (success) {

b = (int) (Math.random() * 100 + 1);/// b是产生的随机数

c = 0;

success = false;

IN.setText("");

OUT.setText("");

System.out.println(b);

}

int x = 0;

try {

x = Integer.parseInt(IN.getText());

} catch (NumberFormatException e) {

return;

}

if (x < 1 || x > 100) {

OUT.setText("超出范围");

c++;

} else if (x > b) {

OUT.setText(x + "太大了");

c++;

} else if (x < b) {

OUT.setText(x + "太小了");

c++;

} else {

end = new java.util.Date();

diff = end.getTime() - start.getTime();

OUT.setText("猜对了,共猜了" + ++c + "次");

success = true;

Username s = new Username(c);

s.setVisible(true);

}

}

private void INActionPerformed(java.awt.event.ActionEvent evt) {

// TOD

}

private void OUTActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) throws FileNotFoundException {

Rank m = new Rank();

m.setVisible(true);

// this.setVisible(false); // TODO add your handling code here:

}

/**

* @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(guess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(guess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(guess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(guess.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 guess().setVisible(true);

}

});

}

private static java.util.Date start, end;

private long diff;

private int b;

private int c;

private boolean success;

// Variables declaration - do not modify

private javax.swing.JTextField IN;

private javax.swing.JTextField OUT;

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 java.awt.Menu menu1;

private java.awt.Menu menu2;

private java.awt.MenuBar menuBar1;

// End of variables declaration

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值