java - 线程中出现异常 "main"IllegalArgumentException : adding container's parent to itself

标签 java swing

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

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself

我不明白要改变什么。

*/

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("");
        }

    }

最佳答案

这里

amountTaxedJLabel.add(amountTaxedJLabel);

我想你的意思是

contentPane.add(amountTaxedJLabel);

在前面,您正在为其自身添加一个 View 。 稍后,您将其添加到您的 contentPane 中。

关于java - 线程中出现异常 "main"IllegalArgumentException : adding container's parent to itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939862/

相关文章:

java - 从java中的套接字读取多个xml文档

java - Java错误: Default constructor cannot handle exception type FileNotFound Exception

java - 如何使用java从xml中读取一些点

Java:按住鼠标时基于二维数组填充图形

java - 如何在不冒失去对称属性的风险的情况下使用 hibernate 实现相等?

java - 为什么我在 Ubuntu 20.04 上从 Aptana 收到运行错误

java - 根据屏幕分辨率设置JFrame和Componets?

java - 如何让JTable显示数据库中的所有记录

java - 添加面板到面板

java - 当我们选择“刷新”选项时,JFileChooser中的“修改日期”字段不会更新