2016-09-27 3 views
-1

확인 그래서 내가 여기에 사용되는 내 프로그램을 완료하고 잘 작동하지만 그것은 내 제목의 JTextField를 식별하는 코드의 마지막 줄에 오류가 말했다파싱하는 동안 파일 끝까지 도달 했습니까?

package trigonomityhelper; 

/*This program was mae by andrew cameron 
*on september 25th 2016 
*to help people with trigonometric functions 
*/ 
public class TrigonomityHelperUI extends javax.swing.JFrame { 

/** 
* Creates new form TrigonomityHelperUI 
*/ 
public TrigonomityHelperUI() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    title = new javax.swing.JLabel(); 
    sin = new javax.swing.JButton(); 
    cos = new javax.swing.JButton(); 
    tan = new javax.swing.JButton(); 
    csc = new javax.swing.JButton(); 
    sec = new javax.swing.JButton(); 
    cot = new javax.swing.JButton(); 
    asin = new javax.swing.JButton(); 
    acos = new javax.swing.JButton(); 
    atan = new javax.swing.JButton(); 
    acsc = new javax.swing.JButton(); 
    asec = new javax.swing.JButton(); 
    acot = new javax.swing.JButton(); 
    calculate = new javax.swing.JButton(); 
    in = new javax.swing.JTextField(); 
    out = new javax.swing.JLabel(); 
    background = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setMaximumSize(new java.awt.Dimension(400, 330)); 
    setMinimumSize(new java.awt.Dimension(400, 330)); 
    setPreferredSize(new java.awt.Dimension(400, 330)); 
    setResizable(false); 
    getContentPane().setLayout(null); 

    title.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N 
    title.setForeground(new java.awt.Color(0, 0, 255)); 
    title.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 
    title.setText("<html>Trigonometry <br>           Helper</html>"); 
    getContentPane().add(title); 
    title.setBounds(120, 10, 100, 50); 

    sin.setText("sin"); 
    sin.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      sinActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(sin); 
    sin.setBounds(240, 23, 70, 20); 

    cos.setText("cos"); 
    cos.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      cosActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(cos); 
    cos.setBounds(240, 43, 70, 20); 

    tan.setText("tan"); 
    tan.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      tanActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(tan); 
    tan.setBounds(240, 63, 70, 20); 

    csc.setText("csc"); 
    csc.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      cscActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(csc); 
    csc.setBounds(240, 83, 70, 20); 

    sec.setText("sec"); 
    sec.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      secActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(sec); 
    sec.setBounds(240, 103, 70, 20); 

    cot.setText("cot"); 
    cot.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      cotActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(cot); 
    cot.setBounds(240, 123, 70, 20); 

    asin.setText("asin"); 
    asin.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      asinActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(asin); 
    asin.setBounds(310, 23, 70, 20); 

    acos.setText("acos"); 
    acos.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      acosActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(acos); 
    acos.setBounds(310, 43, 70, 20); 

    atan.setText("atan"); 
    atan.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      atanActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(atan); 
    atan.setBounds(310, 63, 70, 20); 

    acsc.setText("acsc"); 
    acsc.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      acscActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(acsc); 
    acsc.setBounds(310, 83, 70, 20); 

    asec.setText("asec"); 
    asec.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      asecActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(asec); 
    asec.setBounds(310, 103, 70, 20); 

    acot.setText("acot"); 
    acot.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      acotActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(acot); 
    acot.setBounds(310, 123, 70, 20); 

    calculate.setText("calculate"); 
    calculate.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      calculateActionPerformed(evt); 
     } 
    }); 
    getContentPane().add(calculate); 
    calculate.setBounds(300, 150, 80, 23); 

    in.setHorizontalAlignment(javax.swing.JTextField.CENTER); 
    getContentPane().add(in); 
    in.setBounds(40, 240, 300, 30); 

    out.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    getContentPane().add(out); 
    out.setBounds(10, 270, 360, 20); 

    background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trigonomityhelper/TrigAngles.gif"))); // NOI18N 
    background.setToolTipText("Type the number which you need to calculate, select the calculation type, and then click calculate to get your answer."); 
    getContentPane().add(background); 
    background.setBounds(0, -40, 400, 340); 

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

// define global variables 
String sInput; 
int type = 0; 
double input, output; 

// make custom selection/recoloration button method 
private void select (int a, int b, int c) { 
    //recolor selected button 
    switch (a) { 
     case 1: sin.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 2: cos.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 3: tan.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 4: csc.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 5: sec.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 6: cot.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 7: asin.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 8: acos.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 9: atan.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 10:acsc.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 11:asec.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     case 12:acot.setForeground(new java.awt.Color(0, 0, 255)); 
       break; 
     default:break; 
    } 
    //set type equal to b 
    type = b; 
    //recolor last button black 
    switch (c) { 
     case 1: sin.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 2: cos.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 3: tan.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 4: csc.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 5: sec.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 6: cot.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 7: asin.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 8: acos.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 9: atan.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 10:acsc.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 11:asec.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     case 12:acot.setForeground(new java.awt.Color(0, 0, 0)); 
       break; 
     default:break; 
    } 
} 

private void sinActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select sine 
    select(1, 1, type); 
}         

private void cosActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select cosine 
    select(2, 2, type); 
}         

private void tanActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select tangent 
    select(3, 3, type); 
}         

private void cscActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select cosecant 
    select(4, 4, type); 
}         

private void secActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select secant 
    select(5, 5, type); 
}         

private void cotActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select cotangent 
    select(6, 6, type); 
}         

private void asinActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arcsine 
    select(7, 7, type); 
}          

private void acosActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arccosine 
    select(8, 8, type); 
}          

private void atanActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arctangent 
    select(9, 9, type); 
}          

private void acscActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arcsecant 
    select(10, 10, type); 
}          

private void asecActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arccosecant 
    select(11, 11, type); 
}          

private void acotActionPerformed(java.awt.event.ActionEvent evt) {          
    //set color to blue and all other colors to balck, select arccotangent 
    select(12, 12, type); 
}          

private void calculateActionPerformed(java.awt.event.ActionEvent evt) {           
    //use selected method to run the apropriate calculation and output an answer 
    if (type == 0) { 
     out.setText("No conversion method was selected"); 
    } else { 
     sInput = in.getText(); 
     in.setText(""); 
     try { 
      input = Double.parseDouble(sInput); 
     } catch (NumberFormatException e) { 
      out.setText("The input was not a number"); 
      return; 
     } 

     switch (type) { 
      case 1: output = Math.sin(input); 
        out.setText("The sine of "+input+" is "+output); 
        break; 
      case 2: output = Math.cos(input); 
        out.setText("The cosine of "+input+" is "+output); 
        break; 
      case 3: output = Math.tan(input); 
        out.setText("The tangent of "+input+" is "+output); 
        break; 
      case 4: output = (1/Math.sin(input)); 
        out.setText("The secant of "+input+" is "+output); 
        break; 
      case 5: output = (1/Math.cos(input)); 
        out.setText("The cosecant of "+input+" is "+output); 
        break; 
      case 6: output = (1/Math.tan(input)); 
        out.setText("The cotangent of "+input+" is "+output); 
        break; 
      case 7: output = Math.asin(input); 
        out.setText("The arcsine of "+input+" is "+output+" Radians"); 
        break; 
      case 8: output = Math.acos(input); 
        out.setText("The arccosine of "+input+" is "+output+" Radians"); 
        break; 
      case 9: output = Math.atan(input); 
        out.setText("The arctangent of "+input+" is "+output+" Radians"); 
        break; 
      case 10:output = Math.pow((1/Math.asin(input)),-1); 
        out.setText("The arcsecant of "+input+" is "+output+" Radians"); 
        break; 
      case 11:output = Math.pow((1/Math.acos(input)),-1); 
        out.setText("The arccosecant of "+input+" is "+output+" Radians"); 
        break; 
      case 12:output = Math.pow((1/Math.atan(input)),-1); 
        out.setText("The arccotangent of "+input+" is "+output+" Radians"); 
        break; 
      default:out.setText("No calculation type has been chosen"); 
        break; 
     } 
    } 
}           


public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<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(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(() -> { 
     new TrigonomityHelperUI().setVisible(true); 
    }); 
} 


// Variables declaration - do not modify      
private javax.swing.JButton acos; 
private javax.swing.JButton acot; 
private javax.swing.JButton acsc; 
private javax.swing.JButton asec; 
private javax.swing.JButton asin; 
private javax.swing.JButton atan; 
private javax.swing.JLabel background; 
private javax.swing.JButton calculate; 
private javax.swing.JButton cos; 
private javax.swing.JButton cot; 
private javax.swing.JButton csc; 
private javax.swing.JTextField in; 
private javax.swing.JLabel out; 
private javax.swing.JButton sec; 
private javax.swing.JButton sin; 
private javax.swing.JButton tan; 
private javax.swing.JLabel title;//this is the faulty line of code 
// End of variables declaration     

그래서 제 질문은 지금, 어떻게 이 코드의 마지막 줄을 "오류"가 없도록 만드십시오. 프로그램이 정상적으로 작동하기 때문입니다.

+2

귀하의 질문에 세부 사항과 코드가 매우 부족하여 도움을 받으려면 먼저 질문을 개선하십시오. 세부 사항을 말하고 관련 코드를 표시하십시오. [mcve]가 가장 중요하며 청중과 함께 질문을 작성하십시오. "내 문제와 코드를 완전히 이해하고 나를 도울 수있는 방법을 생각하십시오. ". –

+0

또한 버튼에 ActionListeners를 추가 했습니까? 바로 그 곳에서 버튼 누르기에 대한 작업을 수행합니다. –

+0

''... 그래서 내가 찾고있는 것은 모든 jbutton에 영향을 줄 수있는 코드 라인과 그 private void 메소드를 사용하여 jbutton을 참조하는 방법이다. (코드를 만드는 방법 인 private void 일 필요는 없다. 그런 선은)''- 나에게 이해가되지 않는다. 추가로 개선하십시오. –

답변

2

NetBean의 "자동 생성"ActionListeners를 사용할 필요는 없습니다. 대신 ActionListener를 만들고 확인하려는 모든 JButton에 추가하고 수신기에서 ActionEvent 매개 변수, 특히 getSource() 메서드를 통해 푸시 된 버튼을 가져옵니다. 예를 들어

:

ActionListener buttonListener = new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     JButton source = (JButton) e.getSource(); // here's the button that was pressed 
     source.setForeground(new java.awt.Color(0, 0, 255)); 
    } 
} 

물론 다시이 리스너가 작동하려면, 당신은 JButton의에 추가해야 할 것입니다.

+0

네가 작동하지만 네가 시각적으로 선택을 취소하기 위해 클릭했을 때 검정으로 모든 jButton을 다시 칠하기 위해서 필요하다. –

+1

@AndrewCameron : 다른 모든 버튼을 의미한다면'ArrayList '를 위의 액션 리스너의 목록에있는 버튼은 for 루프를 사용하여 ** 버튼을 ** 파란색으로 변경하기 전에 ** 모든 버튼을 검정으로 전환합니다. –

+1

@AndrewCameron : 그렇지 않으면 JToggleButton과 ButtonGroup을 사용하십시오. –

관련 문제