java重量转换程序中的编译器错误

时间:2015-04-21 13:03:08

标签: java compiler-errors

我正在进行重量转换GUI程序。我收到的编译错误很少。不知道我做错了什么。

以下是错误日志:

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

1 个答案:

答案 0 :(得分:0)

我猜你没有把这些方法放在课堂上。查看文件名,它需要是这样的:

public class Frame4a implements ActionListener {
    public void actionPerformed(ActionEvent e){
        double kp= Double.parseDouble(strkilo);
        if(k.isSelected()){ 
            ans=(kp/2.24);
            area.settext( kp + "kilograms = " + ans + " pounds");
        } else {
            area.settext( kp + "pounds =" + ans +" kilograms");
        }
    }
}

这不会解释代码中的其他错误,例如: settext()可能是setText()