2015-04-21 30 views
0

我正在研究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 
+4

您是否申報過課程? – Laerte

+4

請問您可以顯示代碼嗎? – abarisone

+1

看起來你錯過了在你的類聲明中匹配的''''。 – Sridhar

回答

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

0

public void actionPerformed(...)方法調用之前,您實際上有一個額外的關閉}。該方法基本上不在Frame4a類的範圍之內,因此您將看到類似於您所展示的錯誤的編譯錯誤。

+0

@ gman我做到了,現在我有更多的錯誤。 – BlueDwarf