代码:
//MsgDlg.java
package java_class.Guess;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MsgDlg extends Frame implements ActionListener {
Label label = new Label();
public MsgDlg(String strMsg) {
super();
setResizable(false);
setTitle("猜的结果");
Panel p = new Panel();
add(p);
p.add(label);
label.setText(strMsg);
setSize(200, 60);
setLocation(
(int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width/2-100,
(int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height/2-50);
Button btOk = new Button("确定");
btOk.addActionListener(this);
p.add(btOk);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
this.dispose();
}
}
//lucky.java
package java_class.Guess;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Guess extends Applet implements ActionListener {
Panel pNorth = new Panel();
Panel pNum = new Panel();
Panel pBottom = new Panel();
TextField txField = new TextField(10);
Label label = new Label("请输入0到1000的整数:");
Label label1 = new Label("次数:0");
int num;
private int nPrice;
void reinit(){
num=0;
label1.setText("次数:0");
nPrice=(int)(1000*Math.random());
}
public Guess() {
super();
reinit();
this.setLayout(new BorderLayout());
/*输入*/
pNorth.add(label);
pNorth.add(txField);
txField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg) {
press_ok();
}
});
add(pNorth, BorderLayout.NORTH);
/*次数统计*/
pNum.add(label1);
add(pNum, BorderLayout.CENTER);
/*按钮*/
Button btStart = new Button("重新开始");
btStart.setActionCommand("start");
btStart.addActionListener(this);
Button btOk = new Button("确定");
btOk.setActionCommand("ok");
btOk.addActionListener(this);
Button btCancel = new Button("取消");
btCancel.setActionCommand("cancel");
btCancel.addActionListener(this);
pBottom.add(btStart);
pBottom.add(btOk);
pBottom.add(btCancel);
add(pBottom, BorderLayout.SOUTH);
setBackground(Color.white);
}
void press_ok(){
int guessPrice = 0;
try {
guessPrice = Integer.parseInt(txField.getText().trim());
String guess = comparePrice(guessPrice);
label1.setText("次数:"+num);
new MsgDlg(guess);
}
catch (Exception e) {
new MsgDlg("出错了,请重新猜!");
// e.printStackTrace();
}
}
String comparePrice(int guessPrice) {
if (guessPrice == nPrice) {
return "你猜对了,恭喜你!";
}
else if (guessPrice > nPrice) {
num++;
return "太大了,请重新猜!";
}
else if (guessPrice < nPrice) {
num++;
return "太小了,请重新猜!";
}
return "出错了";
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("start")) {
txField.setText("");
reinit();
}
if (evt.getActionCommand().equals("ok")) {
press_ok();
}
else if (evt.getActionCommand().equals("cancel")) {
txField.setText("");
}
}
}
//index.html <html> <body> <applet code=java_class.Guess.Guess.class width="600" height="120" > </applet> </body> </html>
文件目录结构
.\java_class\Guess\Guess.java .\java_class\Guess\MsgDlg.java .\index.html
编译语句
javac java_class\Guess\*.java
效果预览
0 条评论。