2017-11-28 23 views
0

나는이 세금 신고 프로그램을 작동 시키려고했지만 약간의 어려움을 겪고 있습니다. 확실히 실행되지만 "계산 단추"는 작동하지 않습니다. 세금 반환 오류 GUI

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


public class Tax extends JFrame 
{ 

private int NewNetPay = 0; 

private JPanel panelN; 
private JPanel panelS; 
private JPanel panelE; 
private JPanel panelW; 
private JPanel panelC; 

private JButton calcButton; 
private JButton resetButton; 
private JButton exitButton; 

private JRadioButton hohRB; 
private JRadioButton marriedJRB; 
private JRadioButton marriedSRB; 
private JRadioButton singleRB; 

private JCheckBox mortgageCB; 
private JCheckBox charitableCB; 
private JCheckBox childCB; 
private JCheckBox educationCB; 

private JTextField childTF; 
private JTextField mortgageTF; 
private JTextField charitableTF; 
private JTextField educationTF; 

private JLabel firstName; 
private JTextField name1TF; 
private JLabel lastName; 
private JTextField name2TF; 
private JLabel gross; 
private JTextField grossTF; 

private JLabel netPay; 
private JTextField netPayTF; 


final int WIN_WIDTH = 500; 
final int WIN_HEIGHT = 300; 

public static void main (String[] args) 
{ 
    new Tax(); 
} 


public Tax() 
{ 
    setTitle("CSC142 Tax Calculator"); 
    setSize(WIN_WIDTH,WIN_HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new BorderLayout(20, 20)); 
    buildPanel(); 
    //add(panel); 
    setVisible(true); 

} 
    private void buildPanel() 
    { 
     JFrame frame = new JFrame(); 

     hohRB = new JRadioButton("Head of Household"); 
     marriedJRB = new JRadioButton("Married, Jointly"); 
     marriedSRB = new JRadioButton("Married, Seperately"); 
     singleRB = new JRadioButton("Single"); 

     ButtonGroup radioGroup = new ButtonGroup(); 

     radioGroup.add(hohRB); 
     radioGroup.add(marriedJRB); 
     radioGroup.add(marriedSRB); 
     radioGroup.add(singleRB); 

     mortgageCB = new JCheckBox("Mortgage"); 
     charitableCB = new JCheckBox("Charitable Donation"); 
     childCB = new JCheckBox("Child Deduction"); 
     educationCB = new JCheckBox("Education Expenses"); 

     ButtonGroup checkGroup = new ButtonGroup(); 

     checkGroup.add(mortgageCB); 
     checkGroup.add(charitableCB); 
     checkGroup.add(childCB); 
     checkGroup.add(educationCB); 

     calcButton = new JButton(" Calculate Taxes "); 
     resetButton = new JButton(" Reset Values "); 
     exitButton = new JButton (" Exit Application "); 



     panelN = new JPanel(); 
     panelS = new JPanel(); 
     panelE = new JPanel(); 
     panelW = new JPanel(); 
     panelC = new JPanel(); 

     add(panelN, BorderLayout.NORTH); 
     add(panelS, BorderLayout.SOUTH); 
     add(panelE, BorderLayout.EAST); 
     add(panelW, BorderLayout.WEST); 
     add(panelC, BorderLayout.CENTER); 

     panelN.setLayout(new BoxLayout(panelN, BoxLayout.X_AXIS)); 
     panelS.setLayout(new BoxLayout(panelS, BoxLayout.X_AXIS)); 
     panelE.setLayout(new BoxLayout(panelE, BoxLayout.Y_AXIS)); 
     panelW.setLayout(new BoxLayout(panelW, BoxLayout.Y_AXIS)); 
     panelC.setLayout(new BoxLayout(panelC, BoxLayout.Y_AXIS)); 

     panelW.add(hohRB); 
     panelW.add(marriedJRB); 
     panelW.add(marriedSRB); 
     panelW.add(singleRB); 

     JTextField childTF = new JTextField(); 
     childTF.setPreferredSize(new Dimension (10,10)); 
     JTextField mortgageTF = new JTextField(); 
     mortgageTF.setPreferredSize(new Dimension (10,10)); 
     JTextField charitableTF = new JTextField(); 
     charitableTF.setPreferredSize(new Dimension (10,10)); 
     JTextField educationTF = new JTextField(); 
     educationTF.setPreferredSize(new Dimension (10,10)); 

     childTF.setEnabled(false); 
     mortgageTF.setEnabled(false); 
     charitableTF.setEnabled(false); 
     educationTF.setEnabled(false); 



     panelC.add(mortgageCB); 
     panelC.add(mortgageTF); 
     panelC.add(charitableCB); 
     panelC.add(charitableTF); 
     panelC.add(childCB); 
     panelC.add(childTF); 
     panelC.add(educationCB); 
     panelC.add(educationTF); 

     panelS.add(Box.createRigidArea(new Dimension(35,0))); 
     panelS.add(calcButton); 
     panelS.add(Box.createRigidArea(new Dimension(20,0))); 
     panelS.add(resetButton); 
     panelS.add(Box.createRigidArea(new Dimension(20,0))); 
     panelS.add(exitButton); 

     panelW.setBorder(BorderFactory.createTitledBorder("Filing Status")); 
     panelC.setBorder(BorderFactory.createTitledBorder("Deductions")); 




     JTextField name1TF = new JTextField(); 
     JTextField name2TF = new JTextField(); 
     JTextField grossTF = new JTextField(); 

     JLabel firstName = new JLabel(" First Name "); 
     JLabel lastName = new JLabel(" Last Name "); 
     JLabel gross = new JLabel(" Gross Income ");  


     panelN.add(firstName); 
     panelN.add(name1TF); 
     panelN.add(lastName); 
     panelN.add(name2TF); 
     panelN.add(gross); 
     panelN.add(grossTF); 

     JLabel netPay = new JLabel("Net Pay"); 
     JTextField netPayTF = new JTextField(1); 

     panelE.add(netPay); 
     panelE.add(netPayTF); 

     calcButton.addActionListener(new CalcButtonListener()); 


     } 



     private class CalcButtonListener implements ActionListener 
     { 
     public void actionPerformed(ActionEvent e) 
     { 
      String inputo; 

      inputo = grossTF.getText(); 




      if (mortgageCB.isSelected()) 
       NewNetPay = Integer.parseInt(inputo) - 10000; 
      else if (charitableCB.isSelected()) 
       NewNetPay = Integer.parseInt(inputo) - 5000 ; 
      else if (childCB.isSelected()) 
       NewNetPay = Integer.parseInt(inputo) - 1000; 
      else if (educationCB.isSelected()) 
       NewNetPay = Integer.parseInt(inputo) - 20000; 







     netPayTF.setText(" " + NewNetPay); 


     } 
     } 

    } `enter code here` 

나는 사용자가 다음 그들이 공제를 얻을 것이다 클릭 무엇을 체크 박스에 따라 총소득 텍스트 필드에 급여를 입력 있도록 만들려고 노력하고 있습니다. 마지막으로 금액이 순 유료 텍스트 필드에 표시됩니다.

+0

' \t \t name1TF = 새로운 JTextField를(); \t \t name2TF = new JTextField(); \t \t grossTF = new JTextField(); \t \t firstName = new JLabel ("이름"); \t \t lastName = new JLabel ("성"); \t \t gross = new JLabel ("Gross Income"); ' 전역 변수를 사용하십시오 – Saran

+0

"작동하지 않습니다"라고 말하면 길 * *을 사용해야합니다. 우리는 텔레 패스가 아닙니다. –

답변

0

당신은 grossTF 프레임의 로컬 변수가 어디에 JTextField grossTF = new JTextField();을 추가하고 this.grossTF 데이터를 가져올 때 널 (null)을 초기화 할 것이다 클래스 멤버 인, 그래서 당신은 NPE를 얻을 할 위치 inputo = grossTF.getText();에서 데이터를 얻기 위해 노력하고 있습니다.

은 또한 당신은 ... 그들에게 변경 netPayTF에 대해 같은 짓을했는지

netPayTF = new JTextField(1); & grossTF = new JTextField();

+0

아아, 나에 대한 것들을 명확히 해줘서 고맙다. 나는 꽤 혼란 스러웠다. – Tobi