为什么我在这段 Java 代码中不断收到错误?

发布于 2024-10-21 19:58:31 字数 2059 浏览 1 评论 0原文

我正在编写一个程序,让用户选择一种货币类型,然后当他们输入数字并单击“转换按钮”时,转换结果将显示在文本框中。但我在第 36 行不断收到错误,提示“类或接口预期 public void init ()”

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));

}

I'm writing a program that lets the user select a type of currency, then when they enter in the number and click the 'convert button' the conversion is displayed in a text box. But I keep getting an error on line 36 that says 'class or interface expected public void init ()'

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));

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谢绝鈎搭 2024-10-28 19:58:31

您在 CurrencyConversionApplet 类外部定义了 init() 方法。这就是你想要的吗?

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

You have the init() method defined outside of class CurrencyConversionApplet. Is that what you want?

The error 'class or interface expected public void init ()' says it all: The compiler expects there either a class or an interface. And init() is none of these.

弄潮 2024-10-28 19:58:31

你不应该删除那个'}'吗?

Image dollarSign;
}

Shouldn't you delete that '}' ?

Image dollarSign;
}
将军与妓 2024-10-28 19:58:31

如果我没看错的话,在 Init 声明行之前有一个额外的 },用于关闭类声明。

     Image dollarSign;
} /* <-- */

     public void init()

If I'm reading it right, you have an extra } just before your declaration line for Init, closing the class declaration.

     Image dollarSign;
} /* <-- */

     public void init()
酒浓于脸红 2024-10-28 19:58:31

您收到该错误是因为方法 public void init()
不在类 CurrencyConversionApplet 内。

You're getting that error because the method public void init()
is not inside the class CurrencyConversionApplet.

与风相奔跑 2024-10-28 19:58:31

因为方法 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 方法的上述所有语句都应以 ; 结束

Because the method init() needs to be part of CurrencyConversionApplet . So, do this instead -

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

With that corrected, there are other mistakes too -

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

All the above statements of itemStateChanged method should be ended by a ;

北渚 2024-10-28 19:58:31

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

I don't see that you extend the Applet Class. If this is an applet then you should extend the Applet class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文