java.lang.IllegalArgumentException:将容器的父级添加到自身

时间:2018-12-03 14:32:36

标签: java swing jframe awt

我有以下代码:

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

编译时会抛出此错误:

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中。希望你能理解

1 个答案:

答案 0 :(得分:1)

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

Container pane = this.getContentPane();

...

Container global1 = this.getContentPane();

...

card.add(global1, "first");

...

pane.add(card, BorderLayout.CENTER);

这将创建一个无限循环。

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

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

Container global1 = new JPanel();