2011-03-14 72 views
0

我正在编写一个程序,让用户选择一种货币,然后当他们输入数字并点击“转换按钮”时,转换将显示在文本框中。不过,我不断收到在线36上,上面写着“类或接口预期公共无效的init()”为什么我在这个Java代码中不断收到错误?

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class CurrencyConversionApplet implements ActionListener, ItemListener 
{ 

// Variables 

    double dollars, pounds, euros, ruble, price; 

    Image dollarSign; 



    Label lblTitle = new Label ("Enter the dollar amount (do not use commas or dollar signs): "); 
    Label lblOutput = new Label (" "); 
    TextField txtDollar = new TextField(10); 
    Button convButton = new Button("Convert"); 

    CheckboxGroup chkGroup = new CheckboxGroup(); 
      Checkbox chkPounds = new Checkbox("British Pounds",false,chkGroup); 
      Checkbox chkEuro = new Checkbox("Euros",false,chkGroup); 
      Checkbox chkRuble = new Checkbox("Russian Ruble",false,chkGroup); 
      Checkbox hiddenBox = new Checkbox("",true,chkGroup); 
    Image dollarSign; 
} 

    public void init() 

     { 


     add(lblTitle); 
     add(txtDollar); 
     add(convButton); 
     add(chkPounds); 
     add(chkEuro); 
     add(chkRuble); 
      chkPounds.addItemListener(this); 
      chkEuro.addItemListener(this); 
      chkRuble.addItemListener(this); 

     dollarSign=getImage(getDocumentBase(), "dollar.jpg"); 

     setBackground(Color.blue); 
     setForeground(Color.yellow); 

     convButton.addActionListener(this); 

    } 

      public void paint(Graphics g) { 
      g.drawImage(dollarSign, 0, 28, this); 

    } 


public void itemStateChanged(ItemEvent choice) 
     { 

      dollars = Double.parseDouble(txtDollar.getText()); 
      pounds = dollars * .62 
      euros = dollars * .71 
      ruble = dollars * .03 


    if(chkPounds.getState()) 
     price = pounds; 

    if(chkEuro.getState()) 
     price = euros; 

    if(chkRuble.getState()) 
     price = ruble; 

    } 

     public void actionPerformed(ActionEvent e) 
        { 



lblOutput.setText(Double.toString(price)); 

} 

回答

0

我没有看到你扩展Applet类的错误。如果这是一个小程序,那么你应该扩展Applet类

+1

这不是这个错误信息的原因,但是当试图运行它作为一个小程序时会弹出。 – 2011-03-15 00:16:18

4

你有init()方法定义在类CurrencyConversionApplet之外。那是你要的吗?

错误'class or interface expected public void init()'说了一切:编译器期望有一个类或一个接口。而init()就不是这些。

2

如果我正确地阅读它,那么在Init的宣言行之前还有一个额外的},并关闭类声明。

 Image dollarSign; 
} /* <-- */ 

    public void init() 
2

你得到错误,因为该方法public void init() 不是类CurrencyConversionApplet内。

3

不应该删除'}'吗?

Image dollarSign; 
} 
2

由于init()需要该方法是的CurrencyConversionApplet一部分。那么,这样做,而不是 -

Image dollarSign; 
} // <- Remove that and place it at the very end of the program. 

随着该修正的,还有其他的失误太多 -

pounds = dollars * .62 
euros = dollars * .71 
ruble = dollars * .03 

所有itemStateChanged方法的上述声明应该由结束;

相关问题