我收到错误“ java.lang.IllegalArgumentException:将容器的父级添加到自身”。在我的代码中,我显然看不到我在做什么。我看了另一个来自同一错误的人的stack.overflow线程,他们显然做得不好。在他们的案例中,有人回答说,是由于将一个容器添加到另一个容器,然后将其中一个容器添加到另一个容器(link)而引起的某种无限循环。我不确定为什么我的任何容器添加都会如果这是问题所在,则会导致无限循环。提供的解决方案是说使用JPanels。我已经尝试了所有“ this.getContentPane()”,所有JPanels和一个“ this.getContentPane()”的代码和其他Jpanels,但总是会收到相同的错误。我很好奇:
为什么会出现错误?
好像我正确地实现了CardLayout吗?
使用JPanel和“ this.getContentPane()”之间的功能区别是什么。
我希望再减少一些代码,如果需要的话,我会很乐意这样做。但是我拿出了我非常有信心不会影响代码的所有内容(其他按钮,标签,文本字段),但是我认为可能是所有问题,或者如果删除了某些内容可能会无意间破坏代码。
package comp1206.sushi.server;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.text.JTextComponent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.util.concurrent.Executors;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import comp1206.sushi.common.*;
import comp1206.sushi.server.ServerInterface.UnableToDeleteException;
import java.awt.Dimension;
/**
* Provides the Sushi Server user interface
*
*/
public class ServerWindowTest extends JFrame implements UpdateListener {
private static final long serialVersionUID = -4661566573959270000L;
private ServerInterface server;
/**
* Create a new server window
* @param server instance of server to interact with
*/
public ServerWindowTest(ServerInterface server) {
super("Restaurant Server");
this.server = server;
this.setTitle(server.getRestaurantName() + " Server");
server.addUpdateListener(this);
Container panel = this.getContentPane();
Container entryFields = new JPanel();
Container ordersPanel = new JPanel();
Container dishesPanel = new JPanel();
JButton ordersButton = new JButton("Orders");
JButton dishesButton = new JButton("Dishes");
CardLayout c1 = new CardLayout();
panel.setLayout(new GridBagLayout());
entryFields.setLayout(c1);
ordersPanel.setLayout(new GridBagLayout());
dishesPanel.setLayout(new GridBagLayout());
GridBagConstraints ordersPanelConstraints = new GridBagConstraints();
GridBagConstraints dishesPanelConstraints = new GridBagConstraints();
GridBagConstraints buttonConstraints = new GridBagConstraints();
GridBagConstraints viewScreenConstraints = new GridBagConstraints();
GridBagConstraints label1Constraints = new GridBagConstraints();
GridBagConstraints textField1Constraints = new GridBagConstraints();
buttonConstraints.gridwidth = 1;
buttonConstraints.gridy = 0;
viewScreenConstraints.gridwidth = 9;
viewScreenConstraints.gridheight = 6;
viewScreenConstraints.gridy = 1;
label1Constraints.gridy = 0;
label1Constraints.gridx = 0;
textField1Constraints.gridy = 0;
textField1Constraints.gridx = 0;
entryFields.add(ordersPanel, "ordersPanel");
entryFields.add(dishesPanel, "dishesPanel");
c1.show(entryFields, "ordersPanel");
JTextField viewScreen = new JTextField();
viewScreen.setPreferredSize(new Dimension(600, 200));
JLabel orders1Label = new JLabel("order 1");
JLabel dishes1Label = new JLabel("dish 1");
JTextField orders1TextField = new JTextField();
JTextField dishes1TextField = new JTextField();
ordersPanel.add(orders1Label, label1Constraints);
ordersPanel.add(orders1TextField, textField1Constraints);
dishesPanel.add(dishes1Label, label1Constraints);
dishesPanel.add(dishes1TextField, textField1Constraints);
panel.add(ordersButton, buttonConstraints);
panel.add(dishesButton, buttonConstraints);
panel.add(viewScreen, viewScreenConstraints);
panel.add(entryFields);
ordersButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
c1.show(entryFields, "ordersPanel");
}
});
dishesButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
c1.show(entryFields, "dishesPanel");
}
});
//Display window
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//Start timed updates
startTimer();
}
/**
* Start the timer which updates the user interface based on the given interval to update all panels
*/
public void startTimer() {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
int timeInterval = 5;
scheduler.scheduleAtFixedRate(() -> refreshAll(), 0, timeInterval, TimeUnit.SECONDS);
}
/**
* Refresh all parts of the server application based on receiving new data, calling the server afresh
*/
public void refreshAll() {
}
@Override
/**
* Respond to the model being updated by refreshing all data displays
*/
public void updated(UpdateEvent updateEvent) {
refreshAll();
}
}