2014-05-19 57 views
-1

我正在構建幾何計算器的過程中,但我很難實現ActionListener。我在Oracle網站上找到了一些示例代碼,並對其進行了修改以適應我正在嘗試執行的視覺概念。使用ActionListener輸出幾何計算器的計算結果

我梳理了我的代碼尋找錯別字和不正確的標點符號,並糾正它或沒有發現任何東西卡住了我。我查看了Stack Overflow和課本中的類似問題,並且我的代碼在結構上看起來與示例中正在做的類似。我粘貼了以下代碼的相關部分。

Eclipse給我這個錯誤信息:線程「AWT-EventQueue-0」中的異常java.lang.Error:未解決的編譯問題: CalcButtonListenerA無法解析爲類型我不明白爲什麼會發生這種情況。我想這些線路將採取解決類型的護理:

  `calcButton1 = new JButton("Calculate"); 
    calcButton1.addActionListener(new CalcButtonListenerA());` 

其他相關的代碼如下...

package layout; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GeometryCalculator implements ItemListener { 
    JPanel calcTools; 
    final static String CIRCLEPANEL = "Circle Calculator"; 
    final static String RECTANGLEPANEL = "Rectangle Calculator"; 
    final static String TRIANGLEPANEL = "Triangle Calculator"; 


    private JLabel messageLabel1; 
    private JLabel messageLabel2; 
    private JLabel messageLabel3; 
    private JLabel radiusLabel; 
    private JLabel baseLabel; 
    private JLabel heightLabel; 
    private JLabel lengthLabel; 
    private JLabel widthLabel; 
    private JLabel circleAreaLabel; 
    private JLabel circumferenceLabel; 
    private JLabel rectanglePerimeterLabel; 
    private JLabel rectangleAreaLabel; 
    private JLabel triangleAreaLabel; 
    private JTextField choiceTextField; 
    private JTextField radiusTextField; 
    private JTextField baseTextField; 
    private JTextField heightTextField; 
    private JTextField lengthTextField; 
    private JTextField widthTextField; 
    private JButton calcButton1; 
    private JButton calcButton2; 
    private JButton calcButton3; 

    JTextField rectanglePerimeterField = new JTextField(15); 
    JTextField rectangleAreaField = new JTextField(15); 
    JTextField triangleAreaField = new JTextField(15); 



    public void addComponentToPane(Container pane) { 

     JPanel comboBoxPane = new JPanel(); 
     String comboBoxItems[] = { CIRCLEPANEL, RECTANGLEPANEL, TRIANGLEPANEL }; 
     JComboBox cb = new JComboBox(comboBoxItems); 
     cb.setEditable(false); 
     cb.addItemListener(this); 
     comboBoxPane.add(cb); 

     //Create the "calcTools". 
     JPanel calcTool1 = new JPanel(); 
     radiusLabel = new JLabel("Radius"); 
     circumferenceLabel = new JLabel("Circumference"); 
     circleAreaLabel = new JLabel("Area"); 
     radiusTextField= new JTextField(10); 
     messageLabel1 = new JLabel("Let's make some circle calculations."); 
     final JTextField circumferenceField = new JTextField(15); 
     circumferenceField.setEditable(false); 
     final JTextField circleAreaField = new JTextField(15); 
     circleAreaField.setEditable(false); 
     calcButton1 = new JButton("Calculate"); 
     calcButton1.addActionListener(new CalcButtonListenerA()); 

     calcTool1.add(messageLabel1); 
     calcTool1.add(radiusLabel); 
     calcTool1.add(radiusTextField); 
     calcTool1.add(circumferenceLabel); 
     calcTool1.add(circumferenceField); 
     calcTool1.add(circleAreaLabel); 
     calcTool1.add(circleAreaField); 
     calcTool1.add(calcButton1); 

     class CalcButtonListenerA implements ActionListener 
     { 

      public void actionPerformed(ActionEvent e) 
      { 
       String radius; 
       double circumference; 
       double circleArea; 

       radius = radiusTextField.getText(); 
       circumference = 2*Double.parseDouble(radius)*Math.PI; 
       String circ = String.valueOf(circumference); 
       circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI; 
       String area = String.valueOf(circleArea); 

       circumferenceField.setText(circ); 
       circleAreaField.setText(area);   

      } 
     } 

     JPanel calcTool2 = new JPanel(); 
     messageLabel2 = new JLabel("Let's make some rectangle calculations."); 
     lengthLabel = new JLabel("Length"); 
     widthLabel = new JLabel("Width"); 
     lengthTextField = new JTextField(10); 
     widthTextField = new JTextField(10); 
     rectanglePerimeterLabel = new JLabel("Perimeter"); 
     rectangleAreaLabel = new JLabel("Area"); 
     JTextField rectanglePerimeterField = new JTextField(15); 
     rectanglePerimeterField.setEditable(false); 
     JTextField rectangleAreaField = new JTextField(15); 
     rectangleAreaField.setEditable(false); 
     JButton calcButton2 = new JButton("Calculate"); 

     calcTool2.add(messageLabel2); 
     calcTool2.add(lengthLabel); 
     calcTool2.add(lengthTextField); 
     calcTool2.add(widthLabel); 
     calcTool2.add(widthTextField); 
     calcTool2.add(rectanglePerimeterLabel); 
     calcTool2.add(rectanglePerimeterField); 
     calcTool2.add(rectangleAreaLabel); 
     calcTool2.add(rectangleAreaField); 
     calcTool2.add(calcButton2); 


     JPanel calcTool3 = new JPanel(); 
     messageLabel3 = new JLabel("Let's make some triangle calculations"); 
     baseLabel = new JLabel("Base"); 
     heightLabel = new JLabel("Height"); 
     baseTextField = new JTextField(10); 
     heightTextField = new JTextField(10); 
     triangleAreaLabel = new JLabel("Area"); 
     triangleAreaField = new JTextField(15); 
     triangleAreaField.setEditable(false); 
     JButton calcButton3 = new JButton("calculate"); 

     calcTool3.add(messageLabel3); 
     calcTool3.add(baseLabel); 
     calcTool3.add(baseTextField); 
     calcTool3.add(heightLabel); 
     calcTool3.add(heightTextField); 
     calcTool3.add(triangleAreaLabel); 
     calcTool3.add(triangleAreaField); 
     calcTool3.add(calcButton3); 



     calcTools = new JPanel(new CardLayout()); 
     calcTools.add(calcTool1, CIRCLEPANEL); 
     calcTools.add(calcTool2, RECTANGLEPANEL); 
     calcTools.add(calcTool3, TRIANGLEPANEL); 

     pane.add(comboBoxPane, BorderLayout.PAGE_START); 
     pane.add(calcTools, BorderLayout.CENTER); 
    } 

    public void itemStateChanged(ItemEvent evt) { 
     CardLayout cl = (CardLayout)(calcTools.getLayout()); 
     cl.show(calcTools, (String)evt.getItem()); 
    } 


    private static void createAndShowGUI() { 

     JFrame frame = new JFrame("Geometry Calculator"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     GeometryCalculator demo = new GeometryCalculator(); 
     demo.addComponentToPane(frame.getContentPane()); 


     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     try { 

      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 

     UIManager.put("swing.boldMetal", Boolean.FALSE); 


     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

問題在哪裏? –

+0

@Sergiy我很抱歉不清楚。 Eclipse給我這個錯誤信息:線程「AWT-EventQueue-0」中的異常java.lang.Error:未解決的編譯問題: \t CalcButtonListenerA無法解析爲類型 – kikiwelsh

回答

1

編譯錯誤簡單地說,在這一點上,編譯器不知道什麼CalcButtonListenerA手段。您可以在方法addComponentToPane中定義類CalcButtonListenerA,但是在之後的定義被放置在之後,因此此時該類尚未定義,這與變量會發生的變化有些相同,您無法執行以下操作:

int y = x + 5; //what is x? 
int x = 10; //even if it's defined below, compiler error 

爲此,您可以適當在幾個方面:

  • 定義它的方法,爲前的「局部類」,但用法:

    public void addComponentToPane(Container pane) { 
        class CalcButtonListenerA implements ActionListener 
        { 
         //... 
        } 
        //... 
        calcButton1.addActionListener(new CalcButtonListenerA()); 
    } 
    
  • 將其定義在類GeometryCalculator不在方法:

    public class GeometryCalculator implements ItemListener { 
    
        public void addComponentToPane(Container pane) { 
    
         //... 
         calcButton1.addActionListener(new CalcButtonListenerA()); 
        } 
    
        private class CalcButtonListenerA implements ActionListener 
        { 
         //... 
        } 
    } 
    
  • 把它定義爲一個匿名類,這是一個緊湊的方式做到這一點,如果你不希望使用任何其他actionListener中的代碼。

    public void addComponentToPane(Container pane) { 
        //... 
        calcButton1.addActionListener(new ActionListener() 
        { 
         public void actionPerformed(ActionEvent e) 
         { 
          //the actionPerformed code of CalcButtonListenerA 
         } 
        }); 
    } 
    

如果這是一個非常重要的類,你也可以把它放在自己的文件,在這裏導入它。

+0

感謝您徹底打亂我的選項。問題解決了,但這對於我在未來的項目中考慮非常有用。 – kikiwelsh

+0

@kikiwelsh很高興我能幫忙! – DSquare

1

不要定義方法中的方法。

使用這樣的(更清潔)一個匿名類:

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class GeometryCalculator implements ItemListener { 

    JPanel calcTools; 
    final static String CIRCLEPANEL = "Circle Calculator"; 
    final static String RECTANGLEPANEL = "Rectangle Calculator"; 
    final static String TRIANGLEPANEL = "Triangle Calculator"; 


    private JLabel messageLabel1; 
    private JLabel messageLabel2; 
    private JLabel messageLabel3; 
    private JLabel radiusLabel; 
    private JLabel baseLabel; 
    private JLabel heightLabel; 
    private JLabel lengthLabel; 
    private JLabel widthLabel; 
    private JLabel circleAreaLabel; 
    private JLabel circumferenceLabel; 
    private JLabel rectanglePerimeterLabel; 
    private JLabel rectangleAreaLabel; 
    private JLabel triangleAreaLabel; 
    private JTextField choiceTextField; 
    private JTextField radiusTextField; 
    private JTextField baseTextField; 
    private JTextField heightTextField; 
    private JTextField lengthTextField; 
    private JTextField widthTextField; 
    private JButton calcButton1; 
    private JButton calcButton2; 
    private JButton calcButton3; 

    JTextField rectanglePerimeterField = new JTextField(15); 
    JTextField rectangleAreaField = new JTextField(15); 
    JTextField triangleAreaField = new JTextField(15); 



    public void addComponentToPane(Container pane) { 

     JPanel comboBoxPane = new JPanel(); 
     String comboBoxItems[] = { CIRCLEPANEL, RECTANGLEPANEL, TRIANGLEPANEL }; 
     JComboBox cb = new JComboBox(comboBoxItems); 
     cb.setEditable(false); 
     cb.addItemListener(this); 
     comboBoxPane.add(cb); 

     //Create the "calcTools". 
     JPanel calcTool1 = new JPanel(); 
     radiusLabel = new JLabel("Radius"); 
     circumferenceLabel = new JLabel("Circumference"); 
     circleAreaLabel = new JLabel("Area"); 
     radiusTextField= new JTextField(10); 
     messageLabel1 = new JLabel("Let's make some circle calculations."); 
     final JTextField circumferenceField = new JTextField(15); 
     circumferenceField.setEditable(false); 
     final JTextField circleAreaField = new JTextField(15); 
     circleAreaField.setEditable(false); 
     calcButton1 = new JButton("Calculate"); 

     calcButton1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       String radius; 
       double circumference; 
       double circleArea; 

       radius = radiusTextField.getText(); 
       circumference = 2*Double.parseDouble(radius)*Math.PI; 
       String circ = String.valueOf(circumference); 
       circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI; 
       String area = String.valueOf(circleArea); 

       circumferenceField.setText(circ); 
       circleAreaField.setText(area); 

      } 
     }); 

//  class CalcButtonListenerA implements ActionListener 
//  { 
// 
//   public void actionPerformed(ActionEvent e) 
//   { 
//    String radius; 
//    double circumference; 
//    double circleArea; 
// 
//    radius = radiusTextField.getText(); 
//    circumference = 2*Double.parseDouble(radius)*Math.PI; 
//    String circ = String.valueOf(circumference); 
//    circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI; 
//    String area = String.valueOf(circleArea); 
// 
//    circumferenceField.setText(circ); 
//    circleAreaField.setText(area); 
// 
//   } 
//  } 



     calcTool1.add(messageLabel1); 
     calcTool1.add(radiusLabel); 
     calcTool1.add(radiusTextField); 
     calcTool1.add(circumferenceLabel); 
     calcTool1.add(circumferenceField); 
     calcTool1.add(circleAreaLabel); 
     calcTool1.add(circleAreaField); 
     calcTool1.add(calcButton1); 



     JPanel calcTool2 = new JPanel(); 
     messageLabel2 = new JLabel("Let's make some rectangle calculations."); 
     lengthLabel = new JLabel("Length"); 
     widthLabel = new JLabel("Width"); 
     lengthTextField = new JTextField(10); 
     widthTextField = new JTextField(10); 
     rectanglePerimeterLabel = new JLabel("Perimeter"); 
     rectangleAreaLabel = new JLabel("Area"); 
     JTextField rectanglePerimeterField = new JTextField(15); 
     rectanglePerimeterField.setEditable(false); 
     JTextField rectangleAreaField = new JTextField(15); 
     rectangleAreaField.setEditable(false); 
     JButton calcButton2 = new JButton("Calculate"); 

     calcTool2.add(messageLabel2); 
     calcTool2.add(lengthLabel); 
     calcTool2.add(lengthTextField); 
     calcTool2.add(widthLabel); 
     calcTool2.add(widthTextField); 
     calcTool2.add(rectanglePerimeterLabel); 
     calcTool2.add(rectanglePerimeterField); 
     calcTool2.add(rectangleAreaLabel); 
     calcTool2.add(rectangleAreaField); 
     calcTool2.add(calcButton2); 


     JPanel calcTool3 = new JPanel(); 
     messageLabel3 = new JLabel("Let's make some triangle calculations"); 
     baseLabel = new JLabel("Base"); 
     heightLabel = new JLabel("Height"); 
     baseTextField = new JTextField(10); 
     heightTextField = new JTextField(10); 
     triangleAreaLabel = new JLabel("Area"); 
     triangleAreaField = new JTextField(15); 
     triangleAreaField.setEditable(false); 
     JButton calcButton3 = new JButton("calculate"); 

     calcTool3.add(messageLabel3); 
     calcTool3.add(baseLabel); 
     calcTool3.add(baseTextField); 
     calcTool3.add(heightLabel); 
     calcTool3.add(heightTextField); 
     calcTool3.add(triangleAreaLabel); 
     calcTool3.add(triangleAreaField); 
     calcTool3.add(calcButton3); 



     calcTools = new JPanel(new CardLayout()); 
     calcTools.add(calcTool1, CIRCLEPANEL); 
     calcTools.add(calcTool2, RECTANGLEPANEL); 
     calcTools.add(calcTool3, TRIANGLEPANEL); 

     pane.add(comboBoxPane, BorderLayout.PAGE_START); 
     pane.add(calcTools, BorderLayout.CENTER); 
    } 

// class CalcButtonListenerA implements ActionListener 
// { 
// 
//  public void actionPerformed(ActionEvent e) 
//  { 
//   String radius; 
//   double circumference; 
//   double circleArea; 
// 
//   radius = radiusTextField.getText(); 
//   circumference = 2*Double.parseDouble(radius)*Math.PI; 
//   String circ = String.valueOf(circumference); 
//   circleArea = Double.parseDouble(radius)* Double.parseDouble(radius)*Math.PI; 
//   String area = String.valueOf(circleArea); 
// 
//   circumferenceField.setText(circ); 
//   circleAreaField.setText(area); 
// 
//  } 
// } 



    public void itemStateChanged(ItemEvent evt) { 
     CardLayout cl = (CardLayout)(calcTools.getLayout()); 
     cl.show(calcTools, (String)evt.getItem()); 
    } 


    private static void createAndShowGUI() { 

     JFrame frame = new JFrame("Geometry Calculator"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     GeometryCalculator demo = new GeometryCalculator(); 
     demo.addComponentToPane(frame.getContentPane()); 


     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     try { 

      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 

     UIManager.put("swing.boldMetal", Boolean.FALSE); 


     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
}