2014-12-11 3 views
0

예, 이전에이 질문에 대해 알고 있습니다. 그러나 기능을 수행하는 우리의 방법은 다르므로 이러한 이유로 중복으로 표시해서는 안됩니다.자바 - 계산기 GUI

저는 계산기 GUI 기능을 만들려고합니다. 그러나 현재 사용자가 숫자를 클릭 한 다음 그 번호를 화면에 출력하게해야합니다.

분할 기능 (현재 설정 한 유일한 기능)을 클릭하면 "1"이 화면에 인쇄됩니다. 분명히 사용자의 입력 (클릭 수)이 변경되지 않고 두 번 표시되기 때문에 이것은 오류가 아닙니다. (내가 지금 뭐하는 거지 본질적으로) 그 자체로 나누어 그 숫자는이 내 현재 코드 1.

입니다 :

package my.calculator; 

public class CalculatorUI extends javax.swing.JFrame 
{ 
    public CalculatorUI() 
    { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     Output = new javax.swing.JTextField(); 
     One = new javax.swing.JButton(); 
     Two = new javax.swing.JButton(); 
     Three = new javax.swing.JButton(); 
     Four = new javax.swing.JButton(); 
     Five = new javax.swing.JButton(); 
     Six = new javax.swing.JButton(); 
     Seven = new javax.swing.JButton(); 
     Eight = new javax.swing.JButton(); 
     Nine = new javax.swing.JButton(); 
     Zero = new javax.swing.JButton(); 
     Divide = new javax.swing.JButton(); 
     Multiply = new javax.swing.JButton(); 
     Subtract = new javax.swing.JButton(); 
     Add = new javax.swing.JButton(); 
     Clear = new javax.swing.JButton(); 
     Calc = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     Output.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 
      public void propertyChange(java.beans.PropertyChangeEvent evt) { 
       OutputPropertyChange(evt); 
      } 
     }); 

     One.setText("1"); 
     One.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       OneActionPerformed(evt); 
      } 
     }); 

     Two.setText("2"); 
     Two.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       TwoActionPerformed(evt); 
      } 
     }); 

     Three.setText("3"); 
     Three.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       ThreeActionPerformed(evt); 
      } 
     }); 

     Four.setText("4"); 
     Four.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       FourActionPerformed(evt); 
      } 
     }); 

     Five.setText("5"); 
     Five.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       FiveActionPerformed(evt); 
      } 
     }); 

     Six.setText("6"); 
     Six.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       SixActionPerformed(evt); 
      } 
     }); 

     Seven.setText("7"); 
     Seven.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       SevenActionPerformed(evt); 
      } 
     }); 

     Eight.setText("8"); 
     Eight.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       EightActionPerformed(evt); 
      } 
     }); 

     Nine.setText("9"); 
     Nine.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       NineActionPerformed(evt); 
      } 
     }); 

     Zero.setText("0"); 
     Zero.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       ZeroActionPerformed(evt); 
      } 
     }); 

     Divide.setText("/"); 
     Divide.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       DivideActionPerformed(evt); 
      } 
     }); 

     Multiply.setText("X"); 

     Subtract.setText("-"); 

     Add.setText("+"); 

     Clear.setText("C"); 
     Clear.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       ClearActionPerformed(evt); 
      } 
     }); 

     Calc.setText("="); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(Output) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(One) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(Two) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(Three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(Four) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(Five) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(Six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
          .addComponent(Seven) 
          .addComponent(Clear)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addGroup(layout.createSequentialGroup() 
           .addComponent(Eight) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addComponent(Nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
          .addGroup(layout.createSequentialGroup() 
           .addComponent(Zero) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addComponent(Calc))))) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addComponent(Add) 
           .addGap(0, 0, Short.MAX_VALUE)) 
          .addComponent(Subtract, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(4, 4, 4) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(Multiply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
          .addComponent(Divide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(Output, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(Three, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
            .addComponent(One) 
            .addComponent(Two)) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
            .addComponent(Four) 
            .addComponent(Five) 
            .addComponent(Six) 
            .addComponent(Multiply)) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
            .addComponent(Seven) 
            .addComponent(Eight) 
            .addComponent(Nine) 
            .addComponent(Subtract)) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
            .addComponent(Zero) 
            .addComponent(Add) 
            .addComponent(Calc) 
            .addComponent(Clear))) 
          .addComponent(Divide, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(0, 0, Short.MAX_VALUE)))) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void OneActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(1)); 
    }         

    private void ClearActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(""));   
    }          

    private void TwoActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(2)); 
    }         

    private void ThreeActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(3));   
    }          

    private void FourActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(4));   
    }          

    private void FiveActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(5));   
    }          

    private void SixActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(6));   
    }         

    private void SevenActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(7));   
    }          

    private void EightActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(8));   
    }          

    private void NineActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(9));   
    }          

    private void ZeroActionPerformed(java.awt.event.ActionEvent evt) {          
     Output.setText(String.valueOf(0));   
    }          

    private void DivideActionPerformed(java.awt.event.ActionEvent evt) {          
     int num1 = Integer.parseInt(Output.getText()); 
     int number = Integer.parseInt(Output.getText()); 
     int calculate = num1/number; 
     Output.setText(Integer.toString(calculate)); 
    }          

    private void OutputPropertyChange(java.beans.PropertyChangeEvent evt) {          
     Output.setEditable(false);  // TODO add your handling code here: 
    }          

    public static void main(String args[]) 
    { 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(CalculatorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new CalculatorUI().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton Add; 
    private javax.swing.JButton Calc; 
    private javax.swing.JButton Clear; 
    private javax.swing.JButton Divide; 
    private javax.swing.JButton Eight; 
    private javax.swing.JButton Five; 
    private javax.swing.JButton Four; 
    private javax.swing.JButton Multiply; 
    private javax.swing.JButton Nine; 
    private javax.swing.JButton One; 
    private javax.swing.JTextField Output; 
    private javax.swing.JButton Seven; 
    private javax.swing.JButton Six; 
    private javax.swing.JButton Subtract; 
    private javax.swing.JButton Three; 
    private javax.swing.JButton Two; 
    private javax.swing.JButton Zero; 
    // End of variables declaration     
} 

가 나는 또한 (소수점 기능을 포함시킬 당신이 할 수 있도록 복식 등),하지만 그렇게하는 방법에 관해서는 길을 잃었다. 두 가지 질문에 대한 모든 도움이 크게 감사하겠습니다.

감사합니다.

+7

쉬운 권장 사항 : (1) import 문에 대해 배우고, (2) 루핑 및 배열에 대해 배웁니다. 너무 추악하고 반복적 인 코드는 좋은 것으로 간주됩니다. – duffymo

답변

0

나는 유혹을 느낀다. 그러나 초보자를 찾는 것처럼 사소한 것은 아니지만 계산을 고려한다. 예. 3 + 3 * 2 같은 식을 풀어 주시겠습니까? 우선 필요한 것은 파서 표기법과 같은 중간 형식 인 파서/토큰 네이저 (Parser/Tokenizer)입니다. 산마루 (shanting yard) 알고리즘