У меня следующий код:
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);
У меня есть шесть панелей (panel1,2,3,4,5,6), которые находятся на другой панели (global2 [у меня больше этих глобальных панелей]), и я хочу добавить их в макет карты как разные карты.В частности, я хочу иметь panel1,2 и 3 в global1, panel4,5 и 6 в global2 И глобальные переменные должны быть в cardLayout.Я надеюсь, вы понимаете это