java猜数字小程序,看不懂 ,能不能帮写个注释啊?
publicclassGussNumberextendsFrame{privatestaticGussNumbermainFrame=newGussNumber();pr...
public class GussNumber extends Frame{
private static GussNumber mainFrame = new GussNumber();
private static Button yes = new Button("确定输入");
private static Button reTry = new Button("重新测试");
private static Label lb = new Label("请输入你猜测的数字(0~100):");
private static Label ts = new Label("欢迎使用......");
private static Double r_num = new Double(Math.random()*100);
private static Integer num = new Integer(r_num.intValue());
private static int t_num;
private static TextField tf = new TextField("",5);
public GussNumber(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main (String[] args) {
yes.addActionListener(new ButtonListener());
reTry.addActionListener(new ButtonListener());
System.out.println("Starting GussNumber...");
mainFrame.setBackground(Color.WHITE);
mainFrame.setSize(400,200);
mainFrame.setLayout(null);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("猜数字");
mainFrame.add(lb);
lb.setBounds(70,150,200,20);
mainFrame.add(tf);
tf.setBounds(270,150,50,20);
mainFrame.add(yes);
yes.setBounds(240,170,60,25);
mainFrame.add(ts);
ts.setBounds(70,130,200,20);
mainFrame.add(reTry);
reTry.setBounds(100,170,60,25);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==yes){
try{
t_num = Integer.parseInt(tf.getText());
}catch(NumberFormatException e){
Graphics g = mainFrame.getGraphics();
ts.setText("请输入正常的数字!");
}
if(t_num == num){
ts.setText("答对啦! 恭喜你~");
Graphics g = mainFrame.getGraphics();
}
if(t_num > num){
ts.setText("大了点哦! 继续努力~");
Graphics g = mainFrame.getGraphics();
}
if(t_num < num){
ts.setText("试试换个大点的数字~");
Graphics g = mainFrame.getGraphics();
}
}
if(event.getSource()==reTry){
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
r_num = new Double(Math.random()*100);
num = new Integer(r_num.intValue());
tf.setText("");
}
}
}
} 展开
private static GussNumber mainFrame = new GussNumber();
private static Button yes = new Button("确定输入");
private static Button reTry = new Button("重新测试");
private static Label lb = new Label("请输入你猜测的数字(0~100):");
private static Label ts = new Label("欢迎使用......");
private static Double r_num = new Double(Math.random()*100);
private static Integer num = new Integer(r_num.intValue());
private static int t_num;
private static TextField tf = new TextField("",5);
public GussNumber(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main (String[] args) {
yes.addActionListener(new ButtonListener());
reTry.addActionListener(new ButtonListener());
System.out.println("Starting GussNumber...");
mainFrame.setBackground(Color.WHITE);
mainFrame.setSize(400,200);
mainFrame.setLayout(null);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("猜数字");
mainFrame.add(lb);
lb.setBounds(70,150,200,20);
mainFrame.add(tf);
tf.setBounds(270,150,50,20);
mainFrame.add(yes);
yes.setBounds(240,170,60,25);
mainFrame.add(ts);
ts.setBounds(70,130,200,20);
mainFrame.add(reTry);
reTry.setBounds(100,170,60,25);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==yes){
try{
t_num = Integer.parseInt(tf.getText());
}catch(NumberFormatException e){
Graphics g = mainFrame.getGraphics();
ts.setText("请输入正常的数字!");
}
if(t_num == num){
ts.setText("答对啦! 恭喜你~");
Graphics g = mainFrame.getGraphics();
}
if(t_num > num){
ts.setText("大了点哦! 继续努力~");
Graphics g = mainFrame.getGraphics();
}
if(t_num < num){
ts.setText("试试换个大点的数字~");
Graphics g = mainFrame.getGraphics();
}
}
if(event.getSource()==reTry){
Image img = mainFrame.getToolkit().getImage("welcome.jpg");
Graphics g = mainFrame.getGraphics();
g.drawImage(img,100,30,mainFrame);
r_num = new Double(Math.random()*100);
num = new Integer(r_num.intValue());
tf.setText("");
}
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询