概述
多年不碰,一位老同学让帮忙,只好先蒙混过关了……
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class CaishuAwt extends WindowAdapter {
private Frame frame;
private TextField t1;
private TextArea t2;
private Button b1;
private Panel p1, p2;
String str = "";
static int shu;
static int times;
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
str = t1.getText();
shu = Integer.parseInt(str);
times+=1;
if(shu > num){
t2.setText(origin+=("输入的是"+shu+",输入的数大了,输小点的!rn"));}
else if (shu < num){
t2.setText(origin+=("输入的是"+shu+",输入的数小了,输大点的!rn"));}
else {
t2.setText(origin+=("输入的是"+shu+",恭喜你,猜对了!共用了" + times + "次rn"));
if (times <= 2)
t2.setText(origin+=("你真是个天才!共用了" + times + "次rn"));
else if (times <= 6)
t2.setText(origin+=("还将就,你过关了!共用了" + times + "次rn"));
else if (times <= 8)
t2.setText(origin+=("但是你还……真笨!共用了" + times + "次rn"));
else
t2.setText(origin+=("用了很多次,好慢啊!共用了" + times + "次rn"));
}
//b1.removeActionListener(al);
}
};
private static String origin = "请输入一个100以内的整数:rn";
private int num = new Random().nextInt(100);
public void lunchFrame() {
frame = new Frame("猜数游戏");
p1 = new Panel();
p2 = new Panel();
p1.setLayout(new GridLayout(1, 2));
p2.setLayout(new GridLayout(1, 1));
t1 = new TextField(30);
t1.setText("请在此输入 然后点击Go按钮");
t1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
t1.setText("");
}
});
t2 = new TextArea(origin, 20, 30);
b1 = new Button("Go");
b1.addActionListener(al);
p1.add(t1);
p1.add(b1);// b1.addActionListener(this);
p2.add(t2); //b2.addActionListener(this);
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setBounds(400, 200, 300, 200);
frame.setBackground(new Color(200, 168, 234));
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CaishuAwt ca = new CaishuAwt();
ca.lunchFrame();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
最后
以上就是害怕水池为你收集整理的java awt 垃圾代码一则的全部内容,希望文章能够帮你解决java awt 垃圾代码一则所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复