使用ActionListener输出几何计算器的计算结果

时间:2014-05-19 13:55:19

标签: java swing awt

我正在构建几何计算器,但很难实现ActionListener。我在Oracle网站上找到了一些示例代码,并进行了修改,以适应我正在尝试的视觉概念。

我梳理了我的代码,寻找拼写错误和错误的标点符号,并对其进行了纠正,或者没有找到任何可以解决的问题。我在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();
            }
        });
    }
}

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分:1)

编译错误只是说那时编译器不知道CalcButtonListenerA的含义。您在方法CalcButtonListenerA中定义了类addComponentToPane,但是在使用之后定义了,所以此时该类尚未定义,这有点等同于如果发生变量,则无法执行以下操作:

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

如果它是一个非常重要的类,您也可以将它放在自己的文件中并在此处导入。