📄 lucky.java
字号:
package lucky;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class lucky
extends Applet
implements ActionListener {
Panel pNorth = new Panel();
PicPanel pCenter = new PicPanel();
Panel pBottom = new Panel();
TextField txField = new TextField(10);
Label label = new Label("请输入你对商品价格的估计:");
private int nPrice = 1100;
public lucky() {
super();
this.setLayout(new BorderLayout());
pNorth.add(label);
pNorth.add(txField);
add(pNorth, BorderLayout.NORTH);
add(pCenter, BorderLayout.CENTER);
Button btStart = new Button("开始游戏");
Button btOk = new Button("确定");
Button btCancel = new Button("取消");
btStart.setActionCommand("start");
btStart.addActionListener(this);
btOk.setActionCommand("ok");
btOk.addActionListener(this);
btCancel.setActionCommand("cancel");
btCancel.addActionListener(this);
pBottom.add(btStart);
pBottom.add(btOk);
pBottom.add(btCancel);
add(pBottom, BorderLayout.SOUTH);
setBackground(Color.white);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("start")) {
pCenter.initImg();
label.setText("请输入你对商品价格的估计:");
pCenter.repaint();
}
else if (evt.getActionCommand().equals("ok")) {
int guessPrice = 0;
try {
guessPrice = Integer.parseInt(txField.getText().trim());
String guess = comparePrice(guessPrice);
new MsgDlg(guess);
}
catch (Exception e) {
e.printStackTrace();
}
}
else if (evt.getActionCommand().equals("cancel")) {
txField.setText("");
}
}
public String comparePrice(int guessPrice) {
if (guessPrice == nPrice) {
return "你猜对了,恭喜你!";
}
else if (guessPrice > nPrice) {
return "你猜的价格过高,请重新猜!";
}
else if (guessPrice < nPrice) {
return "你猜的价格过低,请再加价!";
}
return "出错了";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -