Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java勾股计算器修复程序_Java_Swing_Actionlistener - Fatal编程技术网

Java勾股计算器修复程序

Java勾股计算器修复程序,java,swing,actionlistener,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 radioHy

我正在尝试创建一个毕达哥拉斯计算器,我的代码如下所示

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.如何修复代码以不使用两个操作侦听器

btnCalc.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent e1)
      {
         buttonActionPerformed(e1);
      }
});
您正在以动作侦听器的身份传入一个匿名内部类。您刚才所做的是定义一个新类,它实现了ActionListener,它是一个接口。这是生成为TrigCalc$1.class的类

因为你在那里做的是重定向到父类上的方法,你可以用

btnCalc.addActionListener(this);

为什么两个班是个问题?为什么两个动作听众是个问题?我的目的是让一节课。。。这不是问题,只是不符合我的目的。那么我如何才能将类名更改为好的而不是丑陋的TrigCalc$1?P.s.我尝试过这个代码-如果我让按钮工作,收音机就不工作,如果我让收音机工作,这个按钮不响work@jyoon-这是因为当您执行我编写的相同侦听器方法时,在这两种情况下都会调用TrigCalc上的侦听器方法。您需要查看ActionEvent的源,查看事件来自何处—收音机或按钮—并相应地采取行动。要更改类名,请定义一个命名的内部类,并将该类的实例传递给addActionListener,而不是直接创建它。谢谢您,我查找并可以定义一个新的嵌套类。非常感谢。