2014-03-03 4 views
0

피타고라스 계산기를 만들려고하는데 내 코드가 이렇게 보입니다.자바 피타고라스 계산기 수정

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

로를 대체 할 수있는 이유는 두 클래스 문제인가? 왜 두 작업 리스너가 문제가됩니까? – DNA

+0

저를위한 목적은 하나의 수업을 만드는 것이 었습니다 ... 그것은 문제가 아니며, 제 목적을위한 것이 아닙니다. –

답변

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

개봉 된 사용자는 익명 내부 클래스를 작업 수신기로 전달합니다. 여기에서 한 것은 ActionListener (인터페이스)를 구현하는 새로운 클래스를 정의하는 것입니다. 이것은 TrigCalc $ 1.class로 생성 된 클래스입니다.

어쨌든 상위 클래스의 메소드로 리디렉션 무엇 개봉 된이 일을하기 때문에, 당신은

btnCalc.addActionListener(this); 
+0

그럼 어떻게 클래스 이름을 TrigCalc $ 1이 아닌 멋진 것으로 변경할 수 있습니까? –

+0

P. 나는 그 코드를 시도했다 - 나는 버튼을 작동 시키면 라디오가 작동하지 않고 라디오가 작동된다면 버튼이 작동하지 않는다. –

+0

@jyoon - 그 때 내가 동일한 청취자 메소드를 작성했을 때 TrigCalc에서 하나)가 두 경우 모두 호출됩니다. ActionEvent의 소스를보고 이벤트가 어디에서 왔는지 (라디오 나 버튼)를보고 그에 따라 행동해야합니다. 클래스 이름을 변경하려면 _NAMED_ 내부 클래스를 정의하고 ad-hoc을 생성하는 대신 addActionListener()에 인스턴스를 전달하십시오 – radai