I am trying to create a pythagorean calculator and my code looks like this.
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);
}
}
However, this will make two classes called TrigCalc.class and TrigCalc$1.class.
Question: 1. How can I fix the code to make only one class? 2. How can I fix the code to not use two action listeners?
btnCalc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e1)
{
buttonActionPerformed(e1);
}
});
youre passing in an anonymous inner class as an action listener. what you did just here is define a new class, that implements ActionListener (which is an interface). this is the class thats produced as TrigCalc$1.class.
since what youre doing there is redirecting to a method on the parent class anyway, you could replace it with
btnCalc.addActionListener(this);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.