⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lucky.java

📁 java游戏编程导学系列一 使用说明 所有范例
💻 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 + -