java - 在 JDesktopPane Java 中添加外部框架

标签 java swing jframe grouplayout jdesktoppane

我有一个名为“Menu_Project”的框架。

我有另一个名为“Main_Menu”的框架,其中有一个 JButton,单击该按钮后,我想在 JDesktopPane 内调用框架“Menu_Project”。我尝试按照以下链接查看 YouTube 上的教程:https://www.youtube.com/watch?v=TUL3qEoZkR4

当我运行它时,我得到:

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

我的代码:

import java.awt.BorderLayout;


public class Menu extends JFrame {

private JPanel contentPane;
private JDesktopPane desktopPane;
private JButton btnNewButton;
private Menu_Modifications_Single m1; //The Frame to be added
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Menu frame = new Menu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Menu() {
    initComponents();
    createEvents();
}
private void initComponents(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1706, 995);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    btnNewButton = new JButton("Analyze Single");


    desktopPane = new JDesktopPane();
    desktopPane.setBackground(SystemColor.window);
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 1597, GroupLayout.PREFERRED_SIZE))
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNewButton)
                .addContainerGap(1496, Short.MAX_VALUE))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addComponent(btnNewButton)
                .addGap(18)
                .addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE))
    );
    GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
    gl_desktopPane.setHorizontalGroup(
        gl_desktopPane.createParallelGroup(Alignment.LEADING)
            .addGap(0, 1597, Short.MAX_VALUE)
    );
    gl_desktopPane.setVerticalGroup(
        gl_desktopPane.createParallelGroup(Alignment.LEADING)
            .addGap(0, 823, Short.MAX_VALUE)
    );
    desktopPane.setLayout(gl_desktopPane);
    contentPane.setLayout(gl_contentPane);
}
private void createEvents(){
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if(m1==null){
            m1 =  new Menu_Modifications_Single();
            desktopPane.add(m1);

            }
        }
    });

}
}

谁能指出哪里错了。我正在按照视频教程进行操作。

最佳答案

您应该在 JDesktopPane 中使用轻量级 JInternalFrame,而不是 JFrame

您可以轻松地 修改您的代码:

if(m1==null){
    m1 =  new Menu_Modifications_Single();
    desktopPane.add(m1);
}

致:

if(m1==null){
    m1 =  new Menu_Modifications_Single();
    JInternalFrame iFrame = 
            new JInternalFrame("Internal Frame", true, true, true, true);    
            /* give the same contents of m1 */
    iFrame.setContentPane(m1.getContentPane());    
            /* additional Integer argument, required by JDesktopPane
              (although not strictly required) */
    desktopPane.add(iFrame, new Integer(0));    
}

关于java - 在 JDesktopPane Java 中添加外部框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319883/

相关文章:

java - 如何在struts项目的服务器中进行更改

java.lang.IllegalStateException : This feature is only available to backend instances 错误

java - 当帧捕捉到屏幕左侧或右侧时具有事件的监听器

java - 将 JList 值从 JFrame 传递到 JDialog

java - 迭代访问所有二叉树节点?

java - 我的 Android Scrollview 在 tabhost 中消耗了太多内存

java - Swing JButton 中的默认属性(颜色)(默认)

java - 在 Swing 中使用线程的困难

java - NetBeans 文件选择器。如何使用它将文件从一个位置复制到字符串中定义的另一个位置?

java - JPanel 中央一列三行