线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:向容器添加窗口

时间:2016-09-28 09:46:51

标签: java jframe windowbuilder

我们一直在为我们的学校项目做这个计划,最后得到了:

  

线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:向容器添加窗口

和ff错误:

  

at java.awt.Container.checkNotAWindow(Unknown Source)at   java.awt.Container.addImpl(未知来源)at   java.awt.Container.add(未知来源)at   finalProject.TupanessGenre.initialize(TupanessGenre.java:88)at at   finalProject.TupanessGenre。(TupanessGenre.java:37)at   finalProject.TupanessHome $ 2.actionPerformed(TupanessHome.java:55)at   javax.swing.AbstractButton.fireActionPerformed(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 500(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)at at   java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)at   java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)at   java.awt.EventDispatchThread.pumpEvents(未知来源)at   java.awt.EventDispatchThread.pumpEvents(未知来源)at   java.awt.EventDispatchThread.run(未知来源)

受影响的是:游戏类型选择菜单

主页:

public class TupanessHome {

private JFrame home;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TupanessHome window = new TupanessHome();
                window.home.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TupanessHome() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
public void initialize() {
    home = new JFrame();
    home.setTitle("Tupaness - Home");
    home.setBounds(100, 100, 338, 322);
    home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    home.setVisible(true);
    home.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Buy Games");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TupanessGenre gen = new TupanessGenre();
            gen.initialize();
        }
    });
    btnNewButton.setBounds(29, 134, 111, 51);
    home.getContentPane().add(btnNewButton);

    JButton btnBuyConsoles = new JButton("Buy Consoles");
    btnBuyConsoles.setBounds(172, 134, 111, 51);
    home.getContentPane().add(btnBuyConsoles);

    JLabel lblMainMenu = new JLabel("Main Menu");
    lblMainMenu.setHorizontalAlignment(SwingConstants.CENTER);
    lblMainMenu.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
    lblMainMenu.setBounds(117, 30, 87, 33);
    home.getContentPane().add(lblMainMenu);

    JButton btnNewButton_1 = new JButton("Back");
    btnNewButton_1.setBounds(103, 250, 89, 23);
    home.getContentPane().add(btnNewButton_1);
}
}

对于GENRE MENU:

public class TupanessGenre {

private JFrame genre;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TupanessGenre window = new TupanessGenre();
                window.genre.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TupanessGenre() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
public void initialize() {
    genre = new JFrame();
    genre.setTitle("Select Genre");
    genre.setBounds(100, 100, 252, 300);
    genre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    genre.setVisible(true);
    genre.getContentPane().setLayout(null);

    JFrame frame_1 = new JFrame();
    frame_1.setBounds(-10008, -10030, 283, 300);
    frame_1.getContentPane().setLayout(null);

    JLabel label = new JLabel("Select a Genre");
    label.setBounds(24, 23, 80, 14);
    frame_1.getContentPane().add(label);

    JRadioButton radioButton = new JRadioButton("Racing");
    radioButton.setBounds(6, 61, 109, 23);
    frame_1.getContentPane().add(radioButton);

    JRadioButton radioButton_1 = new JRadioButton("First Person Shooter");
    radioButton_1.setBounds(6, 87, 163, 23);
    frame_1.getContentPane().add(radioButton_1);

    JRadioButton radioButton_2 = new JRadioButton("RTS");
    radioButton_2.setBounds(6, 139, 109, 23);
    frame_1.getContentPane().add(radioButton_2);

    JRadioButton radioButton_3 = new JRadioButton("Sports");
    radioButton_3.setBounds(6, 113, 109, 23);
    frame_1.getContentPane().add(radioButton_3);

    JRadioButton radioButton_4 = new JRadioButton("Fighting");
    radioButton_4.setBounds(6, 165, 109, 23);
    frame_1.getContentPane().add(radioButton_4);

    JRadioButton radioButton_5 = new JRadioButton("Online");
    radioButton_5.setBounds(6, 191, 109, 23);
    frame_1.getContentPane().add(radioButton_5);

    JButton button = new JButton("Proceed");
    button.setBounds(80, 228, 89, 23);
    frame_1.getContentPane().add(button);
    frame_1.setTitle("Select A Genre - USER");
    frame_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    genre.getContentPane().add(frame_1);

    JRadioButton rdbtnNewRadioButton = new JRadioButton("Racing");
    rdbtnNewRadioButton.setBounds(41, 57, 109, 23);
    genre.getContentPane().add(rdbtnNewRadioButton);

    JRadioButton rdbtnSimulation = new JRadioButton("Simulation");
    rdbtnSimulation.setBounds(41, 83, 109, 23);
    genre.getContentPane().add(rdbtnSimulation);

    JRadioButton rdbtnFirstpersonShooter = new JRadioButton("FirstPerson Shooter");
    rdbtnFirstpersonShooter.setBounds(41, 109, 144, 23);
    genre.getContentPane().add(rdbtnFirstpersonShooter);

    JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Sports");
    rdbtnNewRadioButton_1.setBounds(41, 135, 109, 23);
    genre.getContentPane().add(rdbtnNewRadioButton_1);

    JLabel lblSelectGameGenre = new JLabel("Select Game Genre");
    lblSelectGameGenre.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
    lblSelectGameGenre.setBounds(54, 27, 129, 23);
    genre.getContentPane().add(lblSelectGameGenre);

    JButton btnOkay = new JButton("Okay");
    btnOkay.setBounds(61, 228, 89, 23);
    genre.getContentPane().add(btnOkay);

    JRadioButton rdbtnOpenworld = new JRadioButton("OpenWorld");
    rdbtnOpenworld.setBounds(41, 187, 109, 23);
    genre.getContentPane().add(rdbtnOpenworld);

    JRadioButton rdbtnRealtimeStrategy = new JRadioButton("Realtime Strategy");
    rdbtnRealtimeStrategy.setBounds(41, 161, 144, 23);
    genre.getContentPane().add(rdbtnRealtimeStrategy);
}
}

我们可能仍会提出更多问题,因为时间压力并没有真正帮助,我们有很多项目需要解决。

1 个答案:

答案 0 :(得分:0)

您无法将JFrame添加到其他JFrame内容窗格(容器)中,这就是您正在进行的内容

 genre.getContentPane().add(frame_1);

其中genreframe_1都是JFrame s

如果您需要某种弹出窗口,请使用JDialog或切换为JPanel frame_1