求JAVA计时的源码加注释JAVA猜数字游戏 jframe图形化界面
求JAVA计时的源码加注释JAVA猜数字游戏jframe图形化界面求JAVA计时的源码加注释JAVA猜数字游戏要求:JFrame图形化界面随机产生一个数,猜中即为赢。猜不...
求JAVA计时的源码加注释JAVA猜数字游戏
jframe图形化界面求JAVA计时的源码加注释JAVA猜数字游戏
要求:JFrame图形化界面
随机产生一个数,猜中即为赢。
猜不中,将提示大了还是小了,直到猜中为止。
从点击猜数按钮开始计时,直到猜中计时停止,给出所用时间。 展开
jframe图形化界面求JAVA计时的源码加注释JAVA猜数字游戏
要求:JFrame图形化界面
随机产生一个数,猜中即为赢。
猜不中,将提示大了还是小了,直到猜中为止。
从点击猜数按钮开始计时,直到猜中计时停止,给出所用时间。 展开
2个回答
展开全部
//多给点财富吧
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Guess extends JFrame
{
public static void main(String[] args)
{
Guess g=new Guess("猜数字游戏");
}
private void reset()
{
kaishi.setEnabled(true);
jishi.setText("");
tishi.setText("");
tick=0;
}
private void processInput()
{
int n=Integer.valueOf(input.getText());
if(n<suijishu)
{
if(n>currentmin)
{
currentmin=n+1;
}
tishi.setText(String.format("小了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else if(n>suijishu)
{
if(n<currentmax)
{
currentmax=n-1;
}
tishi.setText(String.format("大了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else
{
JOptionPane.showMessageDialog(this,"恭喜你,猜对了!再来一次吧!","恭喜恭喜",JOptionPane.INFORMATION_MESSAGE);
timer1.stop();
reset();
}
}
Guess(String t)
{
super(t);
setLayout(new FlowLayout());
jishi=new JLabel();
tishi=new JLabel();
input=new JTextField(10);
kaishi=new JButton("开始猜");
timer1=new javax.swing.Timer(Guess.HaoMiao,null);
c=Calendar.getInstance();
input.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
//processInput();
}
public void insertUpdate(DocumentEvent e)
{
processInput();
}
public void removeUpdate(DocumentEvent e)
{
//processInput();
}
}
);
timer1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//秒数加1
tick++;
c.setTimeInMillis(1000*tick);
//提示已经用了的时间
jishi.setText(String.format("%1$s:%2$s",c.get(Calendar.MINUTE),c.get(Calendar.SECOND)));
}
});
kaishi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
kaishi.setEnabled(false);
input.setText("");
//产生一个范围在Min-Max之间的随机整数
suijishu=(int)(Math.random()*(Guess.Max-Guess.Min+1))+Guess.Min;
currentmin=Guess.Min;
currentmax=Guess.Max;
tishi.setText(String.format("请猜一个范围在%1$d-%2$d之间的数字",Guess.Min,Guess.Max));
timer1.start();
}
});
add(jishi);
add(input);
add(tishi);
add(kaishi);
setSize(500,80);
//使窗口显示在屏幕中央
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static final int Min=1;
private static final int Max=500;
private static final int HaoMiao=1000;
private JLabel jishi,tishi;
private JTextField input;
private JButton kaishi;
private javax.swing.Timer timer1;
private Calendar c;
private int suijishu,currentmin,currentmax;
private long tick=0;
}
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Guess extends JFrame
{
public static void main(String[] args)
{
Guess g=new Guess("猜数字游戏");
}
private void reset()
{
kaishi.setEnabled(true);
jishi.setText("");
tishi.setText("");
tick=0;
}
private void processInput()
{
int n=Integer.valueOf(input.getText());
if(n<suijishu)
{
if(n>currentmin)
{
currentmin=n+1;
}
tishi.setText(String.format("小了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else if(n>suijishu)
{
if(n<currentmax)
{
currentmax=n-1;
}
tishi.setText(String.format("大了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else
{
JOptionPane.showMessageDialog(this,"恭喜你,猜对了!再来一次吧!","恭喜恭喜",JOptionPane.INFORMATION_MESSAGE);
timer1.stop();
reset();
}
}
Guess(String t)
{
super(t);
setLayout(new FlowLayout());
jishi=new JLabel();
tishi=new JLabel();
input=new JTextField(10);
kaishi=new JButton("开始猜");
timer1=new javax.swing.Timer(Guess.HaoMiao,null);
c=Calendar.getInstance();
input.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
//processInput();
}
public void insertUpdate(DocumentEvent e)
{
processInput();
}
public void removeUpdate(DocumentEvent e)
{
//processInput();
}
}
);
timer1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//秒数加1
tick++;
c.setTimeInMillis(1000*tick);
//提示已经用了的时间
jishi.setText(String.format("%1$s:%2$s",c.get(Calendar.MINUTE),c.get(Calendar.SECOND)));
}
});
kaishi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
kaishi.setEnabled(false);
input.setText("");
//产生一个范围在Min-Max之间的随机整数
suijishu=(int)(Math.random()*(Guess.Max-Guess.Min+1))+Guess.Min;
currentmin=Guess.Min;
currentmax=Guess.Max;
tishi.setText(String.format("请猜一个范围在%1$d-%2$d之间的数字",Guess.Min,Guess.Max));
timer1.start();
}
});
add(jishi);
add(input);
add(tishi);
add(kaishi);
setSize(500,80);
//使窗口显示在屏幕中央
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static final int Min=1;
private static final int Max=500;
private static final int HaoMiao=1000;
private JLabel jishi,tishi;
private JTextField input;
private JButton kaishi;
private javax.swing.Timer timer1;
private Calendar c;
private int suijishu,currentmin,currentmax;
private long tick=0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询