Java毕达哥拉斯计算器修复

时间:2014-03-03 12:27:02

标签: java swing actionlistener

我正在尝试创建一个毕达哥拉斯计算器,我的代码看起来像这样。

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

public class TrigCalc extends JPanel implements ActionListener {
    private JRadioButton radioHyp;
    private JRadioButton radioOpp;
    private JRadioButton radioAdj;
    private JLabel labelAdj;
    private JLabel labelOpp;
    private JLabel labelHyp;
    private JTextField fieldAdj;
    private JTextField fieldOpp;
    private JTextField fieldHyp;
    private JButton btnCalc;
    private ButtonGroup calcMode;

    public TrigCalc() {
        //construct components

        //label
        labelAdj = new JLabel ("Adj A =");
        labelOpp = new JLabel ("Opp B =");
        labelHyp = new JLabel ("Hyp C =");

        //textfield
        fieldAdj = new JTextField (5);
        fieldOpp = new JTextField (5);
        fieldHyp = new JTextField (5);

        //button
        btnCalc = new JButton ("Calculate");
        btnCalc.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e1)
        {
            buttonActionPerformed(e1);
        }
    });

        //radio action
        calcMode = new ButtonGroup();

        radioHyp = new JRadioButton ("Hyp", false);
        radioHyp.addActionListener(this);

        radioOpp = new JRadioButton ("Opp", false);
        radioOpp.addActionListener(this);

        radioAdj = new JRadioButton ("Adj", false);
        radioAdj.addActionListener(this);

        calcMode.add(radioHyp);
        calcMode.add(radioOpp);
        calcMode.add(radioAdj);

        //set components properties
        fieldAdj.setEnabled (false);
        fieldOpp.setEnabled (false);
        fieldHyp.setEnabled (false);
        btnCalc.setEnabled (false);

        //adjust size and set layout
        setPreferredSize (new Dimension (269, 129));
        setLayout (null);

        //enabled false default
        fieldAdj.setEnabled (false);
        fieldOpp.setEnabled (false);
        fieldHyp.setEnabled (false);
        btnCalc.setEnabled (false);

        //add components
        add (radioHyp);
        add (radioOpp);
        add (radioAdj);
        add (labelAdj);
        add (labelOpp);
        add (labelHyp);
        add (fieldAdj);
        add (fieldOpp);
        add (fieldHyp);
        add (btnCalc);

        //set component bounds (only needed by Absolute Positioning)
        radioHyp.setBounds (200, 10, 60, 25);
        radioOpp.setBounds (105, 10, 60, 25);
        radioAdj.setBounds (10, 10, 55, 25);
        labelAdj.setBounds (15, 40, 55, 25);
        labelOpp.setBounds (15, 65, 55, 25);
        labelHyp.setBounds (15, 90, 55, 25);
        fieldAdj.setBounds (80, 40, 100, 25);
        fieldOpp.setBounds (80, 65, 100, 25);
        fieldHyp.setBounds (80, 90, 100, 25);
        btnCalc.setBounds (180, 40, 75, 75);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (radioHyp.isSelected()) {
            fieldAdj.setEnabled (true);
            fieldOpp.setEnabled (true);
            fieldHyp.setEnabled (false);
            btnCalc.setEnabled (true);
            //calculation
                double aValue = Double.parseDouble(fieldAdj.getText());
                double bValue = Double.parseDouble(fieldOpp.getText());
                double cValue = Math.sqrt(Math.pow(aValue, 2) + Math.pow(bValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldHyp.setText(nf.format(cValue));
        }
        else if (radioOpp.isSelected()) {
            fieldAdj.setEnabled (true);
            fieldOpp.setEnabled (false);
            fieldHyp.setEnabled (true);
            btnCalc.setEnabled (true);
            //calculation
                double aValue = Double.parseDouble(fieldAdj.getText());
                double bValue = Double.parseDouble(fieldHyp.getText());
                double cValue = Math.sqrt(Math.pow(bValue, 2) - Math.pow(aValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldOpp.setText(nf.format(cValue));
        }
        else if (radioAdj.isSelected()) {
            fieldAdj.setEnabled (false);
            fieldOpp.setEnabled (true);
            fieldHyp.setEnabled (true);
            btnCalc.setEnabled (true);
            //calculation
                double aValue = Double.parseDouble(fieldOpp.getText());
                double bValue = Double.parseDouble(fieldHyp.getText());
                double cValue = Math.sqrt(Math.pow(bValue, 2) - Math.pow(aValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldAdj.setText(nf.format(cValue));
        }
    }
    public void buttonActionPerformed(ActionEvent e1) {
        Object source1 = e1.getSource();

        if (radioHyp.isSelected()) {
            //calculation
                double aValue = Double.parseDouble(fieldAdj.getText());
                double bValue = Double.parseDouble(fieldOpp.getText());
                double cValue = Math.sqrt(Math.pow(aValue, 2) + Math.pow(bValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldHyp.setText(nf.format(cValue));
        }
        else if (radioOpp.isSelected()) {
            //calculation
                double aValue = Double.parseDouble(fieldAdj.getText());
                double bValue = Double.parseDouble(fieldHyp.getText());
                double cValue = Math.sqrt(Math.pow(bValue, 2) - Math.pow(aValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldOpp.setText(nf.format(cValue));
        }
        else if (radioAdj.isSelected()) {
            //calculation
                double aValue = Double.parseDouble(fieldOpp.getText());
                double bValue = Double.parseDouble(fieldHyp.getText());
                double cValue = Math.sqrt(Math.pow(bValue, 2) - Math.pow(aValue, 2));
                NumberFormat nf = NumberFormat.getNumberInstance();
                fieldAdj.setText(nf.format(cValue));
        }
    }
    public static void main (String[] args) {
        JFrame frame = new JFrame ("TrigCalc");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new TrigCalc());
        frame.pack();
        frame.setVisible (true);
    }
}

但是,这将产生两个名为TrigCalc.class和TrigCalc $ 1.class的类。

问题: 1.如何修复代码只能创建一个类? 2.如何修复代码以不使用两个动作侦听器?

1 个答案:

答案 0 :(得分:0)

btnCalc.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent e1)
      {
         buttonActionPerformed(e1);
      }
});

你传入一个匿名内部类作为动作监听器。你在这里做的是定义一个实现ActionListener(它是一个接口)的新类。这是作为TrigCalc $ 1.class生成的类。

因为你正在做什么,无论如何重定向到父类的方法,你可以用

替换它
btnCalc.addActionListener(this);