我对编程很新,我试图将一个不同类的GUI方法调用到我的主类但是遇到了麻烦,当从GUI类运行程序时一切正常但是当我运行主程序时class我在控制台中收到错误消息。
这些是我得到的错误
Exception in thread "AWT-EventQueue-0" 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 mathsQuiz3_1_1.MathsQuiz_3_1_1_Main$1.run(MathsQuiz_3_1_1_Main.java:16)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是主要的课程
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Maths_Quiz_3_1_1_GUI GUIObject = new Maths_Quiz_3_1_1_GUI();
JFrame frame = new JFrame();
GUIObject.setDefaultCloseOperation(Maths_Quiz_3_1_1_GUI.EXIT_ON_CLOSE);
GUIObject.getContentPane().add(GUIObject);
GUIObject.pack();
GUIObject.setVisible(true);
}
});
}
继承GUI类
package mathsQuiz3_1_1;
public class Maths_Quiz_3_1_1_GUI extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Maths_Quiz_3_1_1_GUI frame = new Maths_Quiz_3_1_1_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Maths_Quiz_3_1_1_GUI() {
JPanel contentPane;
JPanel pnlStartScreen = new JPanel();
JPanel pnlAnswerScreen = new JPanel();
JPanel pnlQuestionResult = new JPanel();
JPanel pnlEndScreen = new JPanel();
ButtonGroup btngrpQuestionType = new ButtonGroup();
/**
* JFrame
*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1100, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
我删除了其他代码,只是在jframe中添加了更多元素。
我查看了this thread,其中用户遇到了类似的问题并尝试实施解决方案,但仍然没有运气。任何人都可以提供的任何帮助都会有所帮助,因为我是编程新手,并且不确定如何使用GUI和调用方法。
答案 0 :(得分:1)
嗯...
是
的组合线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:将容器的父项添加到自身
和
GUIObject.getContentPane().add(GUIObject);
打铃?