import java.util.*;
import java.awt.*;
import java.awt.event.*;
class WindowButton extends Frame implements ActionListener {
int number,cunt;
int bTemp = 0;
float timeRead,timeEnd,time;
Label 提示条1,提示条2,提示条3;
Button enter,getNumber,again;
TextField 输入框1,输入框2,输入框3,输入框4;
WindowButton() {
super();
//面板 panel1 组件定义
Panel panel1 = new Panel();
setLayout(new FlowLayout());
getNumber = new Button("得到一个随机数");
提示条1 = new Label("输入猜测");
提示条1.setBackground(Color.RED);
输入框1 = new TextField("0",10);
enter = new Button("确定");
//面板 panel 布局设定
panel1.add(getNumber);
panel1.add(提示条1);
panel1.add(输入框1);
panel1.add(enter);
//面板 panel 注册组件监听器
getNumber.addActionListener(this);
输入框1.addActionListener(this);
enter.addActionListener(this);
panel1.setBounds(100,100,150,150);
//面板 panel2 组件定义
Panel panel2 = new Panel();
again = new Button("重玩");
提示条2 = new Label("用时");
提示条2.setBackground(Color.GRAY);
提示条3 = new Label("次数");
提示条3.setBackground(Color.GRAY);
输入框2 = new TextField("0");
输入框2.setEditable(false);
输入框3 = new TextField("0");
输入框3.setEditable(false);
输入框4 = new TextField("等级");
输入框4.setEditable(false);
//面板 panel2 布局设定
panel2.add(提示条2);
panel2.add(输入框2);
panel2.add(提示条3);
panel2.add(输入框3);
panel2.add(输入框4);
panel2.add(again);
//面板 panel2 组件监听器注册
again.addActionListener(this);
//窗口布局
setLayout(new FlowLayout());
add(panel1);
add(panel2);
setVisible(true);
setSize(400,300);
}
public void actionPerformed(ActionEvent e) {
cunt++;
输入框3.setText(String.valueOf(cunt));
if(e.getSource() == getNumber) {
number = (int) (Math.random() * 100) + 1;
提示条1.setText("输入你的猜测");
}
else if(e.getSource() == enter) {
int guessNum = 0;
try{
guessNum = Integer.parseInt(输入框1.getText());
if(guessNum > number) {
提示条1.setText("猜大了!");
输入框1.setText("");
}
else if(guessNum < number) {
提示条1.setText("猜小了!");
输入框1.setText("");
}
else if(guessNum == number) {
bTemp = 1;
提示条1.setText("猜对了!");
输入框1.setText("");
}
}catch(NumberFormatException event) {
提示条1.setText("格式错误 ,请输入整型数字字符!!!");
}
}
输入框4.setText("");
if(true) {
if(cunt >= 0 && cunt <= 5) {
输入框4.setText("大师!");
}
else if(cunt > 5 && cunt <= 10) {
输入框4.setText("门徒!");
}
else if(cunt > 20 && cunt <= 30) {
输入框4.setText("老手!");
}
else if(cunt > 30 && cunt <= 40) {
输入框4.setText("新手!");
}
else if(cunt > 40) {
输入框4.setText("速度慢,要多练习哟!");
}
}
}
}
class Run extends WindowButton{
public void run(){
timeRead = System.currentTimeMillis();
WindowButton win = new WindowButton();
win.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});
timeEnd = System.currentTimeMillis();
time = timeRead - timeEnd;
输入框2.setText(String.valueOf(time));
System.out.println(time);
}
}
public class guessNumber {
public static void main(String args[]){
Run run = new Run();
run.run();
}
}
import java.awt.*;
import java.awt.event.*;
class WindowButton extends Frame implements ActionListener {
int number,cunt;
int bTemp = 0;
float timeRead,timeEnd,time;
Label 提示条1,提示条2,提示条3;
Button enter,getNumber,again;
TextField 输入框1,输入框2,输入框3,输入框4;
WindowButton() {
super();
//面板 panel1 组件定义
Panel panel1 = new Panel();
setLayout(new FlowLayout());
getNumber = new Button("得到一个随机数");
提示条1 = new Label("输入猜测");
提示条1.setBackground(Color.RED);
输入框1 = new TextField("0",10);
enter = new Button("确定");
//面板 panel 布局设定
panel1.add(getNumber);
panel1.add(提示条1);
panel1.add(输入框1);
panel1.add(enter);
//面板 panel 注册组件监听器
getNumber.addActionListener(this);
输入框1.addActionListener(this);
enter.addActionListener(this);
panel1.setBounds(100,100,150,150);
//面板 panel2 组件定义
Panel panel2 = new Panel();
again = new Button("重玩");
提示条2 = new Label("用时");
提示条2.setBackground(Color.GRAY);
提示条3 = new Label("次数");
提示条3.setBackground(Color.GRAY);
输入框2 = new TextField("0");
输入框2.setEditable(false);
输入框3 = new TextField("0");
输入框3.setEditable(false);
输入框4 = new TextField("等级");
输入框4.setEditable(false);
//面板 panel2 布局设定
panel2.add(提示条2);
panel2.add(输入框2);
panel2.add(提示条3);
panel2.add(输入框3);
panel2.add(输入框4);
panel2.add(again);
//面板 panel2 组件监听器注册
again.addActionListener(this);
//窗口布局
setLayout(new FlowLayout());
add(panel1);
add(panel2);
setVisible(true);
setSize(400,300);
}
public void actionPerformed(ActionEvent e) {
cunt++;
输入框3.setText(String.valueOf(cunt));
if(e.getSource() == getNumber) {
number = (int) (Math.random() * 100) + 1;
提示条1.setText("输入你的猜测");
}
else if(e.getSource() == enter) {
int guessNum = 0;
try{
guessNum = Integer.parseInt(输入框1.getText());
if(guessNum > number) {
提示条1.setText("猜大了!");
输入框1.setText("");
}
else if(guessNum < number) {
提示条1.setText("猜小了!");
输入框1.setText("");
}
else if(guessNum == number) {
bTemp = 1;
提示条1.setText("猜对了!");
输入框1.setText("");
}
}catch(NumberFormatException event) {
提示条1.setText("格式错误 ,请输入整型数字字符!!!");
}
}
输入框4.setText("");
if(true) {
if(cunt >= 0 && cunt <= 5) {
输入框4.setText("大师!");
}
else if(cunt > 5 && cunt <= 10) {
输入框4.setText("门徒!");
}
else if(cunt > 20 && cunt <= 30) {
输入框4.setText("老手!");
}
else if(cunt > 30 && cunt <= 40) {
输入框4.setText("新手!");
}
else if(cunt > 40) {
输入框4.setText("速度慢,要多练习哟!");
}
}
}
}
class Run extends WindowButton{
public void run(){
timeRead = System.currentTimeMillis();
WindowButton win = new WindowButton();
win.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});
timeEnd = System.currentTimeMillis();
time = timeRead - timeEnd;
输入框2.setText(String.valueOf(time));
System.out.println(time);
}
}
public class guessNumber {
public static void main(String args[]){
Run run = new Run();
run.run();
}
}