线程“main”中的异常IllegalArgumentException:将容器的父级添加到自身

时间:2014-09-19 18:03:31

标签: java swing

我刚开始学习编程,我正在尝试创建一个计算所得税20%的计划。程序编译但不断出错。错误状态

  

线程“main”中的异常java.lang.IllegalArgumentException:将容器的父级添加到自身

我不明白要改变什么。

*/

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

public class IncomeTax extends JFrame
{
// declarations
    Color black = new Color(0, 0, 0);
    Color white = new Color(255, 255, 255);
    Color light_gray = new Color(192, 192, 192);

    DecimalFormat currency;

    JLabel grossIncomeJLabel;
    JTextField grossIncomeJTextField;

    JLabel amountTaxedJLabel;
    JTextField amountTaxedJTextField;
    JTextField currencyJTextField;

    JLabel amountDeductedJLabel;
    JTextField amountDeductedJTextField;


    JLabel netIncomeJLabel;
    JTextField netIncomeJTextField;


    JButton enterJButton;
    JButton clearJButton;

double grossIncome;
double amountTaxed;
double amountDeducted;
double netIncome;

public IncomeTax()
    {
            createUserInterface();
    }

    public void createUserInterface()
    {
    Container contentPane = getContentPane();
    contentPane.setBackground(white);
    contentPane.setLayout(null);

    // initialize components

    // input
    grossIncomeJLabel = new JLabel ();
    grossIncomeJLabel.setBounds (50, 20, 150, 20);
    grossIncomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    grossIncomeJLabel.setText ("Enter Gross Income:");
    grossIncomeJLabel.setForeground(black);
    grossIncomeJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(grossIncomeJLabel);

    grossIncomeJTextField = new JTextField();
    grossIncomeJTextField.setBounds(200, 20, 100, 20);
    grossIncomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    grossIncomeJTextField.setHorizontalAlignment(JTextField.CENTER);
    grossIncomeJTextField.setForeground(black);
    grossIncomeJTextField.setBackground(white);
    grossIncomeJTextField.setEditable(true);
    contentPane.add(grossIncomeJTextField);


    //outputs
    amountTaxedJLabel = new JLabel();
    amountTaxedJLabel.setBounds(50, 50, 150, 20);
    amountTaxedJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    amountTaxedJLabel.setText("Amount Taxed");
    amountTaxedJLabel.setForeground(black);
    amountTaxedJLabel.setHorizontalAlignment(JLabel.LEFT);
    amountTaxedJLabel.add(amountTaxedJLabel);

    amountTaxedJTextField = new JTextField();
    amountTaxedJTextField.setBounds(200, 50, 100, 20);
    amountTaxedJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    amountTaxedJTextField.setHorizontalAlignment(JTextField.CENTER);
    amountTaxedJTextField.setForeground(black);
    amountTaxedJTextField.setBackground(white);
    amountTaxedJTextField.setEditable(false);
    contentPane.add(amountTaxedJTextField);

    amountDeductedJLabel = new JLabel();
    amountDeductedJLabel.setBounds(50, 80, 150, 20);
    amountDeductedJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    amountDeductedJLabel.setText("Amount Deducted:");
    amountDeductedJLabel.setForeground(black);
    amountDeductedJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(amountDeductedJLabel);

    amountDeductedJTextField = new JTextField();
    amountDeductedJTextField.setBounds(200, 80, 100, 20);
    amountDeductedJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    amountDeductedJTextField.setHorizontalAlignment(JTextField.CENTER);
    amountDeductedJTextField.setForeground(black);
    amountDeductedJTextField.setBackground(white);
    amountDeductedJTextField.setEditable(false);
    contentPane.add(amountDeductedJTextField);

    netIncomeJLabel = new JLabel();
    netIncomeJLabel.setBounds(50, 110, 150, 20);
    netIncomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    netIncomeJLabel.setText("Net Income:");
    netIncomeJLabel.setForeground(black);
    netIncomeJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(netIncomeJLabel);

    netIncomeJTextField = new JTextField();
    netIncomeJTextField.setBounds(200, 110, 100, 20);
    netIncomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    netIncomeJTextField.setHorizontalAlignment(JTextField.CENTER);
    netIncomeJTextField.setForeground(black);
    netIncomeJTextField.setBackground(white);
    netIncomeJTextField.setEditable(false);
    contentPane.add(netIncomeJTextField);

    // control
    enterJButton = new JButton();
    enterJButton.setBounds(50, 210, 100, 20);
    enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    enterJButton.setText("Enter");
    enterJButton.setForeground(black);
    enterJButton.setBackground(white);
    contentPane.add(enterJButton);
    enterJButton.addActionListener(

        new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    enterJButtonActionPerformed(event);
                }
            }
        );

        clearJButton = new JButton();
        clearJButton.setBounds(200, 210, 100, 20);
        clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
        clearJButton.setText("Clear");
        clearJButton.setForeground(black);
        clearJButton.setBackground(white);
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    clearJButtonActionPerformed(event);
                }
            }
        );

        // set properties of application’s window
        setTitle("Income Tax");         // set title
        setSize( 400, 400 );        // set window size
        setVisible(true);           // display window
        }

        // main method
        public static void main(String[] args)
        {
              IncomeTax application = new IncomeTax();
              application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        public void enterJButtonActionPerformed(ActionEvent event)
    {

        // get input
        grossIncome = Double.parseDouble(grossIncomeJTextField.getText());

        // process data
        final double Tax_Rate = .20;
        amountTaxed = Tax_Rate;
        amountDeducted = grossIncome * Tax_Rate;
        netIncome = grossIncome - amountDeducted;

        //display results
        currency = new DecimalFormat("$0.00");

        amountTaxedJTextField.setText("" + currency.format(amountTaxed));
        amountDeductedJTextField.setText("" + currency.format(amountDeducted));
        netIncomeJTextField.setText("" + currency.format(netIncome));
}

    public void clearJButtonActionPerformed(ActionEvent event)
    {
        grossIncomeJTextField.setText("");
        grossIncomeJTextField.requestFocusInWindow();
        amountTaxedJTextField.setText("");
        amountDeductedJTextField.setText("");
        netIncomeJTextField.setText("");
        }

    }

1 个答案:

答案 0 :(得分:3)

下面

amountTaxedJLabel.add(amountTaxedJLabel);

我认为你的意思是

contentPane.add(amountTaxedJLabel);

在之前的版本中,您要为自己添加视图。 在后面,您将它添加到contentPane。