2011-03-14 52 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方法的上述聲明應該由結束;

相關問題