2017-11-28 11 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( "First Name"); \t \t lastName = new JLabel( "Last Name"); \t \t gross = new JLabel( "総収入"); ' グローバル変数を使用する – Saran

+0

「うまくいきません」と言えば、やはり* how *とする必要があります。私たちはテレパスではありません。 –

答えて

0

あなたはgrossTFがフレームにローカルな変数であるので、あなたがNPEを取得したい、データを取得するときはnullに初期化されるだろうクラスのメンバであるinputo = grossTF.getText();どこthis.grossTFからデータを取得しようとJTextField grossTF = new JTextField();を追加しています。

また、あなたがnetPayTFのために同じことをやった、にそれらを変更。..

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

+0

ああ、私は事を明確にしてくれてありがとう、私はかなり混乱していた。 – Tobi

関連する問題