?? 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 + -