java.lang.IllegalArgumentException:将容器的父级添加到自身 [英] java.lang.IllegalArgumentException: adding container's parent to itself

查看:109
本文介绍了java.lang.IllegalArgumentException:将容器的父级添加到自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

public class GUI extends JFrame {
private JLabel text, result;

private JTextField volt, amper, ohm, capacity, time, tensionChange, stream;

private JButton calculate, next, previous;

private JPanel header, panel1, panel2, panel3, panel4, panel5, panel6, 
panel7, panel8, panel9, card, pane;

private CardLayout cards;

private double voltNumber = 0;
private double amperNumber = 0;
private double ohmNumber = 0;
private double timeNumber = 0;
private double streamNumber = 0;
private double tensionChangeNumber = 0;
private double capacityNumber = 0;

public GUI() {
Container pane = this.getContentPane();
pane.setLayout(new BorderLayout());

this.header = new JPanel();
this.next = new JButton(">>");
this.previous = new JButton("<<");
this.header.add(this.previous);
this.header.add(this.next);
pane.add(this.header, BorderLayout.PAGE_START);

cards = new CardLayout();
card = new JPanel();
card.setLayout(cards);
cards.show(card, "Vybíráme");

Container global1 = this.getContentPane();

panel1 = new JPanel();
panel1.setLayout(new FlowLayout());

panel1.add(new JLabel("Výpočet Ohmova zákona"));
panel1.add(new JLabel("Zadej dvě hodnoty a třetí hodnota bude dopočítána."));
panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

text = new JLabel("Zadej napětí: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.1;
panel2.add(text, c);

volt = new JTextField("0");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.8;
panel2.add(volt, c);

text = new JLabel("[V]");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.weightx = 0.1;
panel2.add(text, c);

text = new JLabel("Zadej proud: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
panel2.add(text, c);

amper = new JTextField("0");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
panel2.add(amper, c);

text = new JLabel("[A]");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
panel2.add(text, c);

text = new JLabel("Zadej odpor: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
panel2.add(text, c);

ohm = new JTextField("0");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
panel2.add(ohm, c);

text = new JLabel("[Ohm]");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 3;
panel2.add(text, c);

panel3 = new JPanel();
calculate = new JButton("Vypočti");
panel3.add(calculate);
calculate.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                ohmCalculation(voltNumber, amperNumber, ohmNumber);
            };
        });

panel3.setLayout(new FlowLayout());

global1.add(panel1);
global1.add(panel2);
global1.add(panel3);
global1.setLayout(new BoxLayout(global1, BoxLayout.PAGE_AXIS));

//////////////////////////////////////////////////////////////////////////////

Container global2 = this.getContentPane();

panel4 = new JPanel();
panel4.setLayout(new FlowLayout());

panel4.add(new JLabel("Výpočet Ohmova zákona"));
panel4.add(new JLabel("Zadej dvě hodnoty a třetí hodnota bude dopočítána."));
panel4.setLayout(new BoxLayout(panel4, BoxLayout.PAGE_AXIS));
panel5 = new JPanel();
panel5.setLayout(new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();

text = new JLabel("Zadej přepětí: ");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 0;
d.gridy = 0;
d.weightx = 0.1;
panel5.add(text, c);

volt = new JTextField("0");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 1;
d.gridy = 0;
d.weightx = 0.8;
panel5.add(volt, c);

text = new JLabel("[V]");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 2;
d.gridy = 0;
d.weightx = 0.1;
panel5.add(text, c);

text = new JLabel("Zadej proud: ");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 0;
d.gridy = 1;
panel5.add(text, c);

amper = new JTextField("0");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 1;
d.gridy = 1;
panel5.add(amper, c);

text = new JLabel("[A]");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 2;
d.gridy = 1;
panel5.add(text, c);

text = new JLabel("Zadej odpor: ");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 0;
d.gridy = 3;
panel5.add(text, c);

ohm = new JTextField("0");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 1;
d.gridy = 3;
panel5.add(ohm, c);

text = new JLabel("[Ohm]");
d.fill = GridBagConstraints.HORIZONTAL;
d.gridx = 2;
d.gridy = 3;
panel5.add(text, c);

panel6 = new JPanel();
calculate = new JButton("Vypočti");
panel6.add(calculate);
calculate.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                condenserCalculation(capacityNumber, timeNumber, tensionChangeNumber, streamNumber);
            };
        });

panel6.setLayout(new FlowLayout());

//boxLayout = new BoxLayout()

global2.add(panel4);
global2.add(panel5);
global2.add(panel6);
global2.setLayout(new BoxLayout(global2, BoxLayout.PAGE_AXIS));

card.add(global1, "first");
card.add(global2, "second");

pane.add(card, BorderLayout.CENTER);

}

public void setListeners(){
  this.next.addActionListener(
  new ActionListener() {
      public void actionPerformed(ActionEvent e){
        cards.next(card);
      }
  });
  this.previous.addActionListener(
  new ActionListener(){
      public void actionPerformed(ActionEvent e){
       cards.previous(card);
    }
  });
}

在编译时会抛出此错误:

And it throws me this error while compiling:

Exception in thread "main" java.lang.IllegalArgumentException: adding 
container's parent to itself
at java.awt.Container.checkAddToSelf(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at GUI.<init>(GUI.java:219)
at ELK_MyName.main(ELK_MyName.java:6)

第n219行是带有pane.add(card, BorderLayout.CENTER);

我有六个面板(面板1,2,3,4,5,6),它们位于另一个面板(global2 [我有更多这些全局面板])中,我想将它们作为不同的方式添加到卡布局中牌.具体来说,我想在global1中有panel1,2和3,在global2中有panel4,5和6,而全局变量应该在cardLayout中.希望你能理解

I've got six panels (panel1,2,3,4,5,6), which are in another panel (global2 [I have more these global panels]), and I want add them into card layout as diferrent cards. Specifically I want to have panel1,2, and 3 in global1, panel4,5 and 6 in global2 AND the globals should be in cardLayout. I hope you understand it

推荐答案

窗格,global1和global2都是同一对象.这导致卡和窗格之间的子关系无限.

pane, global1, and global2 are all the same object. This results in an infinite child relationship between card and pane.

Container pane = this.getContentPane();

...

Container global1 = this.getContentPane();

...

card.add(global1, "first");

...

pane.add(card, BorderLayout.CENTER);

这将创建一个无限循环.

This creates an infinite loop.

pane -> card -> global1 (also pane) -> card -> ...

您可能正打算为布局目的创建新的JPanel,而不是多次重复使用同一UI元素.

You likely are looking to instead create new JPanels for layout purposes, rather than reusing the same UI element multiple times.

Container global1 = new JPanel();

这篇关于java.lang.IllegalArgumentException:将容器的父级添加到自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆