gpt4 book ai didi

java - 为什么 JInternalFrame 删除所有其他组件

转载 作者:行者123 更新时间:2023-12-02 05:53:03 24 4
gpt4 key购买 nike

我完成了在 JFrame 中添加 JInternalFrame 的工作,添加框架的方式是单击 JMenuItem 图标,然后就可以了很好,但是当添加框架时,旧组件将被删除并显示白色。我想做的是设置修复 JInternalFrame 位置与其他组件,而不影响其他组件

JInternalFrame 的代码

    package animeaidvlcj;

import javax.swing.JInternalFrame;


/* Used by InternalFrameDemo.java. */
public class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 0, yOffset = 25;

public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable

//...Create the GUI and put it in the window...

//...Then set the window size or call pack...
setSize(300,300);

//Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
}

操作代码

 Action newAction = new AbstractAction("New", newIcon) {
@Override
public void actionPerformed(ActionEvent e) {
desktop = new JDesktopPane();
createFrame();
setContentPane(desktop);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
};

创建方法

protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}

最佳答案

"why JInternalFrame remove all of the other component"

首先看看您的Action中的这段代码

desktop = new JDesktopPane();
createFrame();
setContentPane(desktop); <== this line in particular

您正在使用 desktop 设置框架的内容 Pane (假设 setContenPane() 正在调用类框架),这将删除以前的内容 Pane ,从而容纳您的所有组件。因此,唯一出现的就是桌面以及新的JInternalFrame

除非您正在创建 Multiple Document Interface (MDI) ,桌面 Pane 和内部框架主要用于哪些,我建议您只使用 JDialog 并忘记 DesktopPane。您可以看到How to use Dialogs 。它与创建 JFrame 几乎相同,只是您可以选择 modality

关于java - 为什么 JInternalFrame 删除所有其他组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com