这是我的代码。我想使用菜单栏打开另一个Java表单,但是当我单击菜单项时,它会显示一个错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
adding a window to a container
我现在该怎么办?
package Driver;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class frmTestMenu extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frmTestMenu frame = new frmTestMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frmTestMenu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100,653, 425);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 434, 31);
contentPane.add(menuBar);
JMenu mnFile = new JMenu("FIle");
menuBar.add(mnFile);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
frmTestMenu.this.dispose();
}
});
mnFile.add(mntmExit);
JMenu mnItem = new JMenu("Item1");
menuBar.add(mnItem);
JMenuItem mntmMenuItem = new JMenuItem("open Test Internal Form");
mnItem.add(mntmMenuItem);
JMenuItem mntmMenuItem_3 = new JMenuItem("open Test Internal Form 2");
mnItem.add(mntmMenuItem_3);
JMenu mnItem_1 = new JMenu("item 2");
menuBar.add(mnItem_1);
JMenuItem mntmMenuItem_1 = new JMenuItem("Menu 2 item 1");
mnItem_1.add(mntmMenuItem_1);
JMenu mnItem_2 = new JMenu("item 3");
menuBar.add(mnItem_2);
JMenuItem mntmMenuItem_2 = new JMenuItem("Menu 3 item 1");
mnItem_2.add(mntmMenuItem_2);
final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBounds(0, 0, 633, 366);
mntmMenuItem.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
frm3_0 test1 = new frm3_0();
test1.setBounds(10, 10, 426, 229);
desktopPane.add(test1);
test1.setVisible(true);
}
});
mntmMenuItem_3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
frmcity_A test2 = new frmcity_A();
test2.setBounds(10, 10, 426, 229);
desktopPane.add(test2);
test2.setVisible(true);
}
});
JScrollPane scrollPane = new JScrollPane(desktopPane);
JDesktopPane desktopPane_1 = new JDesktopPane();
desktopPane_1.setBounds(145, 88, 1, 1);
desktopPane.add(desktopPane_1);
scrollPane.setBounds(0, 30, 633, 336);
contentPane.add(scrollPane);
}
}