这个java小程序怎么运行不了,该怎么改?
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();
}
}
答案:你的这个代码只是自定义了一个类,由于你这个代码里面没有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();
}
}
上一个:关于一个JAVA开发的学员管理系统的毕业论文
下一个:用Java的画图实现 价格-日期 的走势图