在百度上回答的问题,还不错啊, 我能做出来,别人也做出来了,不过还是学到点东西的,呵呵,下午应该继续学习GUI编程,老师让我看那个java jdk上的demo,我觉得好难啊, 看了一上午,还是没什么头绪,三维的东西很难搞明白,有空再弄吧,
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
public class bb extends Frame implements ActionListener ,WindowListener
{ TextField tf1,tf2;
Label lb1,lb3,lb4;
Button but1,but2;
Panel p1,p2,p3;
double a,w,cc;
String s ,d;
String qq =null;
bb ()
{
Frame ff=new Frame("这是一个程序");
ff.setLayout(new BorderLayout());
but1=new Button("获得1-100之间的随机数");
tf1=new TextField(15);
lb1=new Label("请输入你的猜测:");
tf2=new TextField(15);
//lb2=new Label("单击确定按钮");
but2=new Button("确定");
lb3=new Label("反馈信息:");
lb4=new Label("无反馈信息");
p1=new Panel();
p2=new Panel();
p3=new Panel();
but1.addActionListener(this);
but2.addActionListener(this);
p1.add(but1);
p1.add(tf1);
p2.add(lb1);
p2.add(tf2);
p2.add(but2);
p3.add(lb3);
p3.add(lb4);
ff.add("North",p1);
ff.add("Center",p2);
ff.add("South",p3);
ff.setSize(500,500);
ff.setVisible(true);
ff.addWindowListener(this);
ff.addWindowListener(this);
//ff.validate();
}
public void init()
{
this.add(but1);
this.add(tf1);
this.add(lb1);
this.add(tf2);
this.add(lb3);
this.add(but2);
this.add(lb4);
but1.addActionListener(this);
but2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//TextField tf=(TextField)(e.getSource());
if( e.getSource()==but1){
a=Math.random();
//System.out.println(a);
double b =a*100;
int c=(int)b;
cc=c;
d=String.valueOf(cc);
tf1.setText(d);
}
if (e.getSource().equals(but2))
{
s=null;
s=tf2.getText();
String q=s;
if(s.length()==0){
s=qq;
System.out.println("现在你的值是"+qq+"不要输入空值!");
}
tf2.setText("");
w=Integer.parseInt(s);
qq=s;
if (cc>w)
{
lb4.setText("笨蛋,猜小了你");
}
if (cc
{
lb4.setText("猜大了,笨的跟猪一样,!!嘎嘎");
}
if(cc==w)
{
if(q==null)
lb4.setText("The textfield is empty!");
lb4.setText("还好,你猜对了,要不要接着挨骂了!");
}
}
}
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public static void main(String[] args)
{
bb f = new bb ();
}
}
Tags -
百度 ,
java ,
gui
文章来源:
http://www.tt-shopping.com/kevinlau/read.php/117.htm