package guess;
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class guess {
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class DrawFrame extends JFrame
{
private static int count=0;//猜测次数
private int rightNumber;//随机产生的供猜测的数字
private JTextField num;//输入的数字
private JLabel tip;//猜测次数提示
private JPanel panel;//中心输入框部分面板
private JLabel result;//猜测结果提示
private JButton button0=new JButton("开始");
private JButton button1=new JButton("确认");
private JButton button2=new JButton("重新开始");
private JButton button3=new JButton("退出");
private JButton button4=new JButton("玩法");
private JButton button5=new JButton("提示");
public DrawFrame()
{
setBounds(700,200,450,200);
setTitle("一个猜数游戏");
rightNumber=(int)(Math.random()*40+10);
//添加顶部次数提示
tip=new JLabel("你已经猜了"+count+"次",JLabel.LEFT);
//添加中心输入框部分
panel = new JPanel();
JLabel input=new JLabel("输入猜测的数");
panel.add(input);
num=new JTextField(20);
panel.add(num);
result=new JLabel();//显示猜测结果
panel.add(result);
//添加按钮
JPanel buttons= new JPanel();
ActionListener listener1=new ComfirmListener();
button1.addActionListener(listener1);
ActionListener listener2=new OtherListener2();
button2.addActionListener(listener2);
button3.addActionListener(listener2);
button4.addActionListener(listener2);
button5.addActionListener(listener2);
//将按钮添加到panel中
buttons.add(button0);
buttons.add(button1);
buttons.add(button2);
buttons.add(button3);
buttons.add(button4);
buttons.add(button5);
add(tip,"North");
add(panel,"Center");
add(buttons,"South");
}
//确认按钮的监听器类
class ComfirmListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//猜测正确
if(Integer.parseInt(num.getText())==rightNumber)
{
//设置文本框不可编辑
num.setEditable(false);
//提示猜测次数+1
tip.setText("你已经猜了"+(++count)+"次");
//调整背景色为默认背景色
Color defaultColor = getBackground();
panel.setBackground(defaultColor);
//显示猜测结果
panel.setBackground(Color.green);
result.setText("恭喜你猜对啦!!");
}
//小于
else if(Integer.parseInt(num.getText())<rightNumber)
{
panel.setBackground(Color.red);
tip.setText("你已经猜了"+(++count)+"次");
result.setText("此次结果为:太小");
}
else
{
panel.setBackground(Color.red);
tip.setText("你已经猜了"+(++count)+"次");
result.setText("此次结果为:太大");
}
}
}
//重新开始和退出按钮以及帮助的监听器
class OtherListener2 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//重新开始
if(event.getSource()==button2)
{
//清除文本框内容
num.setText("");
//重新抽数字
rightNumber=(int)(Math.random()*40+10);
//调整背景色为默认背景色
Color defaultColor = getBackground();
panel.setBackground(defaultColor);
//猜测次数增加
count=0;
tip.setText("你已经猜了"+count+"次");
//清除原猜测结果
result.setText("");
}
else if(event.getSource()==button4) {
JOptionPane.showMessageDialog(null, "玩法:请从10~50中用最少的次数猜一个随机的数字。祝你好运!");
}
else if(event.getSource()==button5) {
JOptionPane.showMessageDialog(null,"提示:其一种解法用到高中确定区间零点的常用方法。");
JOptionPane.showMessageDialog(null,"提示:其最优解是华罗庚发表的0.618优选法。");
}
//退出则关闭窗口
else
{
setVisible(false);
}
}
}
}