?? lucky.java
字號:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
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 "出錯了";
}
}
class PicPanel extends Panel
{
String fileName="N3310.gif";
Image m_img;
public void initImg()
{
URL url=null;
try
{
url=Class.forName("Lucky").getResource("N3310.gif");
}
catch(Exception e){}
m_img=getToolkit().getImage(url);
MediaTracker mt=new MediaTracker(this);
mt.addImage(m_img,1);
try{
mt.wait();
mt.checkAll();
}
catch(Exception e){}
}
public void paint(Graphics g)
{
g.drawImage(m_img,125,0,50,160,this);
}
}
class MsgDlg extends Frame implements ActionListener
{
Label label=new Label();
public MsgDlg(String strMsg)
{
super();
setTitle("猜的結果");
Panel p=new Panel();
add(p);
p.add(label);
label.setText(strMsg);
setSize(150,100);
setLocation(300,200);
Button btOk=new Button("確定");
btOk.addActionListener(this);
p.add(btOk);
show();
}
public void actionPerformed(ActionEvent evt)
{
this.dispose();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -