这个java小程序怎么运行不了,该怎么改?

importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassLucky52extends... import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Lucky52 extends Frame
implements ActionListener
{ Panel pnlNorth=new Panel();
MyImgPanel pnlCenter=new MyImgPanel();
Panel pnlButtom=new Panel();
TextField textfield=new TextField(10);
Label label=new Label("请输入您对商品的估价");
int truePrice=678;
public Lucky52()
{ super();
this.setLayout(new BorderLayout());
pnlNorth.add(label);
pnlNorth.add(textfield);
add(pnlNorth,BorderLayout.NORTH);
add(pnlCenter,BorderLayout.CENTER);
Button btnStart=new Button("开始");
Button btnOk=new Button("确定");
Button btnCancel=new Button("取消");
btnStart.setActionCommand("start");
btnStart.setActionListener(this);
btnOk.setActionCommand("Ok");
btnOk.addActionListener(this);
btnCancle.setActionCommand("cancel");
btnCancel.addActionListener(this);
pnlButtom.add(btnStart);
pnlButtom.add(btnOk);
pnlButtom.add(btnCancel);
add(pnlButtom,BorderLayout.SOUTH);
setBackground(Color.white);
}
public void actionPerformed(ActionEvent evt)
{ if(evt.getActionCommand().equals("start"))
{ label.setText("请输入您对商品的估价");
pnlCenter.repaint();
}
else if(evt.getActionCommand().equals("Ok"))
{ int guessPrince=0;
try { guessPrice=Integer.parseInt(textfield.getText().trim());
String guess=comparePrice(guessPrice);
new MsgDlg(guess);
}
catch (Exception e)
{e.PrintStackTrace();
}
}
else if(evt.getActionCommand().equals("cancel"))
{ textfield.setText("");
}
}
public String comparePrice(int guessPrice)
{ if(guessPrice==truePrice)
{return"恭喜你,猜对了!";}
else if(guessPrice>truePrice)
{return"过高了";}
else if(guessPrice<truePrice)
{return"过低了";}
return "出错了!";
}
}
class MyImgPanel extends Panel
{ String fileName="lucky52.gif";
Image myImage;
public void initImg()
{ URL url=null;
try{url=Class.forName("Lucky52").getResource("lucky52.gif");}
catch(Exception e){}
myImage=getToolkit().getImage(url);
MediaTracker myTracker=new MediaTracker(this);
myTracker.addImage(myImage,1);
try{myTracker.wait();
myTracker.checkAll();
}
catch(Exception e){}
}
}
public void paint(Graphics g)
{g.drawImage(myImage,135,30,55,75,this);}
}
class MsgDlg extends Frame implements ActionListener
{ Label label=new Label();
public MsgDlg(String strMsg)
{ super();
setTitle("猜测结果显示");
Panel myPanel=new Panel();
add(myPanel);
myPanel.add(label);
label.setText(strMsg);
setSize(150,100);
setLocation(300,200);
Button btnOk=new Button("确定");
btnOk.addActionListener(this);
myPanel.add(btnOk);
show();
}
public void actionPerformed(ActionEvent evt)
{ this.dispose();
}
}
展开
 我来答
mzocean
推荐于2016-08-28 · TA获得超过1083个赞
知道小有建树答主
回答量:492
采纳率:33%
帮助的人:470万
展开全部
你的这个代码只是自定义了一个类,由于你这个代码里面没有main方法,不能直接运行,你可以在其他的地方写一个有main方法的类来调用这个Lucky52类。或者在这个源代码中加磨衡庆入main方法,但是需要改动。除此之外,代码还有几处问题我已经改好了,我用拦判的是第二种调用方法在本程序中调用Lucky52类。还有你的类里还有包含一个图片,可是你的代码好像没有显示出来图片,我也没有看懂怎么回事。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;//
public class TestLucky52{ //加入的main方法
public static void main(String[] args){//
new Lucky52();} //
} //
class Lucky52 extends Frame
implements ActionListener
{ Panel pnlNorth=new Panel();
MyImgPanel pnlCenter=new MyImgPanel();
Panel pnlButtom=new Panel();
TextField textfield=new TextField(10);
Label label=new Label("请输入您对商品的估价");
int truePrice=678;
public Lucky52()
{ super();
this.setLayout(new BorderLayout());
pnlNorth.add(label);
pnlNorth.add(textfield);
add(pnlNorth,BorderLayout.NORTH);
add(pnlCenter,BorderLayout.CENTER);
Button btnStart=new Button("开始");
Button btnOk=new Button("确定");
Button btnCancel=new Button("取消");
btnStart.setActionCommand("start");
btnStart.addActionListener(this);// btnStart.setActionListener(this);
btnOk.setActionCommand("Ok");
btnOk.addActionListener(this);
btnCancel.setActionCommand("cancel");
btnCancel.addActionListener(this);
pnlButtom.add(btnStart);
pnlButtom.add(btnOk);
pnlButtom.add(btnCancel);
add(pnlButtom,BorderLayout.SOUTH);
setBackground(Color.white);
addWindowListener(new WindowAdapter(){//加入关闭瞎握窗口
public void windowClosing(WindowEvent ee){
System.exit(0);
}
} );
setSize(400,400);//改变框架大小
setVisible(true);//可见
}
public void actionPerformed(ActionEvent evt)
{ if(evt.getActionCommand().equals("start"))
{ label.setText("请输入您对商品的估价");
pnlCenter.repaint();
}
else if(evt.getActionCommand().equals("Ok"))
{ int guessPrice=0;
try { guessPrice=Integer.parseInt(textfield.getText().trim());
String guess=comparePrice(guessPrice);
new MsgDlg(guess);
}
catch (Exception e)
{//e.PrintStackTrace();
}
}
else if(evt.getActionCommand().equals("cancel"))
{ textfield.setText("");
}
}
public String comparePrice(int guessPrice)
{ if(guessPrice==truePrice)
{return"恭喜你,猜对了!";}
else if(guessPrice>truePrice)
{return"过高了";}
else if(guessPrice<truePrice)
{return"过低了";}
return "出错了!";
}
}
class MyImgPanel extends Panel
{ String fileName="lucky52.gif";//图片,放在同一个文件夹里
Image myImage;
public void initImg()
{ URL url=null;
try{url=Class.forName("Lucky52").getResource("lucky52.gif");}
catch(Exception e){}
myImage=getToolkit().getImage(url);
MediaTracker myTracker=new MediaTracker(this);
myTracker.addImage(myImage,1);
try{myTracker.wait();
myTracker.checkAll();
}
catch(Exception e){}
}
public void paint(Graphics g)
{ super.paint(g);
g.drawImage(myImage,135,30,55,75,this);}
}
class MsgDlg extends Frame implements ActionListener
{ Label label=new Label();
public MsgDlg(String strMsg)
{ super();
setTitle("猜测结果显示");
Panel myPanel=new Panel();
add(myPanel);
myPanel.add(label);
label.setText(strMsg);
setSize(150,100);
setLocation(300,200);
Button btnOk=new Button("确定");
btnOk.addActionListener(this);
myPanel.add(btnOk);
show();
}
public void actionPerformed(ActionEvent evt)
{ this.dispose();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
梦云途
2010-07-02 · 超过16用户采纳过TA的回答
知道答主
回答量:41
采纳率:0%
帮助的人:29.8万
展开全部
我对代码调试了下,应该可以了
package com.java;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
public class Lucky52 extends Frame implements ActionListener
{ Panel pnlNorth=new Panel();
MyImgPanel pnlCenter=new MyImgPanel();
Panel pnlButtom=new Panel();
TextField textfield=new TextField(10);
Label label=new Label("请输入您坦纯对商品的裂信洞估价");
int truePrice=678;
public Lucky52()
{ super();
this.setLayout(new BorderLayout());
pnlNorth.add(label);
pnlNorth.add(textfield);
add(pnlNorth,BorderLayout.NORTH);
add(pnlCenter,BorderLayout.CENTER);
Button btnStart=new Button("开始");
Button btnOk=new Button("确定");
Button btnCancel=new Button("取消");
btnStart.setActionCommand("start");
btnStart.addActionListener(this);
btnOk.setActionCommand("Ok");
btnOk.addActionListener(this);
btnCancel.setActionCommand("肆枯cancel");
btnCancel.addActionListener(this);
pnlButtom.add(btnStart);
pnlButtom.add(btnOk);
pnlButtom.add(btnCancel);
add(pnlButtom,BorderLayout.SOUTH);
setBackground(Color.white);
}
public void actionPerformed(ActionEvent evt)
{ if(evt.getActionCommand().equals("start"))
{ label.setText("请输入您对商品的估价");
pnlCenter.repaint();
}
else if(evt.getActionCommand().equals("Ok"))
{ int guessPrince=0;
try {
guessPrince =Integer.parseInt(textfield.getText().trim());
String guess=comparePrice(guessPrince);
new MsgDlg(guess);
}
catch (Exception e)
{e.printStackTrace();
}
}
else if(evt.getActionCommand().equals("cancel"))
{ textfield.setText("");
}
}
public String comparePrice(int guessPrice)
{ if(guessPrice==truePrice)
{return"恭喜你,猜对了!";}
else if(guessPrice>truePrice)
{return"过高了";}
else if(guessPrice<truePrice)
{return"过低了";}
return "出错了!";
}

public static void main(String args[]){
new Lucky52();
}
}
class MyImgPanel extends Panel
{ String fileName="lucky52.gif";
Image myImage;
public void initImg()
{ URL url=null;
try{url=Class.forName("Lucky52").getResource("lucky52.gif");}
catch(Exception e){}
myImage=getToolkit().getImage(url);
MediaTracker myTracker=new MediaTracker(this);
myTracker.addImage(myImage,1);
try{myTracker.wait();
myTracker.checkAll();
}
catch(Exception e){}
}

public void paint(Graphics g)
{g.drawImage(myImage,135,30,55,75,this);}

}
class MsgDlg extends Frame implements ActionListener
{ Label label=new Label();
public MsgDlg(String strMsg)
{ super();
setTitle("猜测结果显示");
Panel myPanel=new Panel();
add(myPanel);
myPanel.add(label);
label.setText(strMsg);
setSize(150,100);
setLocation(300,200);
Button btnOk=new Button("确定");
btnOk.addActionListener(this);
myPanel.add(btnOk);
show();
}
public void actionPerformed(ActionEvent evt)
{ this.dispose();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式