线程 "AWT-EventQueue-0" java.lang.IllegalArgumentException 中的异常:将 window 添加到容器

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

我们一直在为我们的学校项目做这个程序,最终得到:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

还有 ff 错误:

at java.awt.Container.checkNotAWindow(Unknown Source) at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at finalProject.TupanessGenre.initialize(TupanessGenre.java:88) at finalProject.TupanessGenre.(TupanessGenre.java:37) at finalProject.TupanessHome.actionPerformed(TupanessHome.java:55) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access0(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.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 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);
}
}

流派菜单:

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

我们可能还会问更多问题,因为时间压力并没有真正帮助我们,而且我们还有很多项目要处理。

您不能将 JFrame 添加到其他 JFrame 内容窗格(容器),这就是您在行

中所做的
 genre.getContentPane().add(frame_1);

其中 genreframe_1 都是 JFrames

如果您需要某种弹出窗口 window 使用 JDialog 或切换到 JPanel 而不是 frame_1