0

I'm working on a Weight Conversion GUI program. I'm getting few compiler errors. Don't know what I'm doing wrong.

Below are the error log:

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

Can I get some light on these please?

I updated code and now I'm getting these:

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
5
  • 4
    Did you declare a class?
    – Laerte
    Apr 21, 2015 at 13:04
  • 4
    Could you show the code please?
    – abarisone
    Apr 21, 2015 at 13:04
  • 1
    It seems you missed a matching { in your class declaration.
    – Sridhar
    Apr 21, 2015 at 13:07
  • @Sridhar it seems more like the whole class declaration is missing, otherwise the compiler would complain about a missing {
    – Turing85
    Apr 21, 2015 at 13:10
  • I added a pic of my code (unfortunately couldn't add the code in the main question, so put a screenshot instead) @abarison
    – BlueDwarf
    Apr 21, 2015 at 14:06

2 Answers 2

0

I'm guessing you didn't put these methods in a class. Looking at the filename, it needs to be something like this:

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

This doesn't account for other errors in your code, e.g. settext() is probably setText().

0

You actually have an extra closing } just before your public void actionPerformed(...) method call. The method is basically outside the scope of the Frame4a class, so you will get weird compilation errors like the ones you showed.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.