package test;
import java.awt.*;
import java.awt.event.*;
class CaiShu extends Frame implements ActionListener
{
private static final long serialVersionUID = 1L;
int number;
Label 提示条;
TextField 输入框;
Button buttonGetNumber,buttonEnter;
CaiShu(String s)
{ super(s);
setLayout(new FlowLayout());
buttonGetNumber=new Button("得到一个随机数");
add(buttonGetNumber);
提示条=new Label("输入你的猜测:",Label.CENTER);
提示条.setBackground(Color.cyan);
输入框=new TextField("0",10);
add(提示条);
add(输入框);
buttonEnter=new Button("确定");
add(buttonEnter);
buttonEnter.addActionListener(this);
buttonGetNumber.addActionListener(this);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==buttonGetNumber)
{ number=(int)(Math.random()*100)+1;
提示条.setText("输入你的猜测:");
}
else if(e.getSource()==buttonEnter)
{ int guess=0;
try { guess=Integer.parseInt(输入框.getText());
if(guess==number)
{ 提示条.setText("猜对了!");
}
else if(guess>number)
{ 提示条.setText("猜大了!");
输入框.setText(null);
}
else if(guess
输入框.setText(null);
}
}
catch(NumberFormatException event)
{ 提示条.setText("请输入数字字符");
}
}
}
public static void main(String args[])
{ CaiShu win=new CaiShu("窗口");
}
}
修改一下类名,把主函数放到类中就好了。
class Jiao extends Frame implements ActionListener
//
public class Jiao
这里的两个类怎么能同名呢/???//
public 类的名称应该和 此 .java文件名一致