我正在研究Weight Conversion GUI程序。我收到了幾個編譯器錯誤。不知道我做錯了什麼。java重量轉換程序中的編譯器錯誤
下面是錯誤日誌:
Frame4a.java:32: error: class, interface, or enum expected
public void actionPerformed(ActionEvent e){
^
Frame4a.java:36: error: class, interface, or enum expected
double kp= Double.parseDouble(strkilo);
^
Frame4a.java:38: error: class, interface, or enum expected
if(k.isSelected()){ ans=(kp/2.24);
^
Frame4a.java:40: error: class, interface, or enum expected
area.settext(kp + "kilograms = " + ans + " pounds");
^
Frame4a.java:41: error: class, interface, or enum expected
}
^
Frame4a.java:47: error: class, interface, or enum expected
area.settext(kp + "pounds =" + ans +" kilograms");
^
Frame4a.java:48: error: class, interface, or enum expected
}
^
7 errors
我能得到一些光就這些嗎?
我更新的代碼,現在我得到這些:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class Frame4a implements ActionListener
{
public static void main(String[] args)
{
JFrame f = new JFrame("Weight converter");
JPanel p = new JPanel(); // Make a JPanel;
JTextArea area = new JTextArea() ;
JRadioButton k,Po;
JTextField field = new JTextField();
k=new JRadioButton("kilograms to pounds");
Po=new JRadioButton("pounds to kilograms");
k.addActionListener(this);
Po.addActionListener(this);
p.add(area);
p.add(k);
p.add(Po);
p.add(field);
f.getContentPane().add(p); // Add panel P to JFrame f
f.setSize(400,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String strkilo = field.getText();
double kp= Double.parseDouble(strkilo);
if(k.isSelected())
{ ans =(kp/2.24);
area.setText(kp + "kilograms = " + ans + " pounds");
}
else
{
double ans=(kp*2.24);
area.setText(kp + "pounds =" + ans +" kilograms");
}
}
}
===================
Now the error log:
===================
Frame4a.java:19: error: non-static variable this cannot be referenced from a static context
k.addActionListener(this);
^
Frame4a.java:20: error: non-static variable this cannot be referenced from a static context
Po.addActionListener(this);
^
Frame4a.java:36: error: cannot find symbol
String strkilo = field.getText();
^
symbol: variable field
location: class Frame4a
Frame4a.java:40: error: cannot find symbol
if(k.isSelected())
^
symbol: variable k
location: class Frame4a
Frame4a.java:42: error: cannot find symbol
{ ans =(kp/2.24);
^
symbol: variable ans
location: class Frame4a
Frame4a.java:44: error: cannot find symbol
area.setText(kp + "kilograms = " + ans + " pounds");
^
symbol: variable ans
location: class Frame4a
Frame4a.java:44: error: cannot find symbol
area.setText(kp + "kilograms = " + ans + " pounds");
^
symbol: variable area
location: class Frame4a
Frame4a.java:49: error: cannot find symbol
area.setText(kp + "pounds =" + ans +" kilograms");
^
symbol: variable area
location: class Frame4a
8 errors
您是否申報過課程? – Laerte
請問您可以顯示代碼嗎? – abarisone
看起來你錯過了在你的類聲明中匹配的''''。 – Sridhar