【问题标题】:how to create internal frame in netbeans platform?如何在netbeans平台中创建内部框架?
【发布时间】:2010-03-13 21:56:42
【问题描述】:

我创建了类 NewProject 扩展了 JInternalFrame。然后我创建了名为“NEW”的 New...Action,在 File 菜单中进行了本地化。我把代码NewProject p = new NewProject(); p.setVisible(true); 放到了动作的ActionPerformed 方法中。

但是当我运行模块并在文件菜单中单击“新建”时,什么都没有出现。 哪里有问题?

编辑: 我通过代码部分解决了它:

public void actionPerformed(ActionEvent e) {

    JInternalFrame f = new JInternalFrame();
    f.setSize(500, 500);
    f.setVisible(true);
    JDesktopPane p = new JDesktopPane();
    p.add(f);

   //WindowManager.getDefault().getMainWindow().setTitle("fFF");
   WindowManager.getDefault().getMainWindow().add(p)

}

但是 GUI 坏了。当我创建新的内部框架时,黑色背景会在我移动该框架时出现。 知道如何解决吗?

【问题讨论】:

  • 您能否再解释一下您想要实现的目标是什么?
  • 我想在 netbeans 平台上创建内部框架。当我运行模块并选择创建该内部框架的操作时,我希望该框架出现
  • 您应该首先创建一个像 JFrame 这样的顶级容器,然后在 JFrame 中添加 JInternalFrame。
  • 但是顶级容器是由 netbeans 创建的,我找不到如何访问它。

标签: java swing netbeans


【解决方案1】:

JInternalFrame 的惯用 Container 是 JDesktopPane。文章How to Use Internal Frames 概述了要点,您可能会喜欢在此上下文中使用Action 和JMenu 的简短example。

虽然 NetBean 的 GUI 编辑器很吸引人,但您可能希望先更轻松地使用 Swing 组件。

附录:您不能将一个Top-Level Container(如JFrame)添加到另一个类似JDesktopPane 的实例中,但您可以将任意数量的JInternalFrame 实例添加到一个JDesktopPane。试试demo 看看它是如何工作的。

附录:啊,你的意思是NetBeans Platform。抱歉,我没用过。

【讨论】:

  • 但问题是我不知道如何获取主窗口的桌面窗格或任何可以附加 JFrame 的位置。我说的是 Netbeans 平台。
  • 对损坏的教程链接感到抱歉。有关更多信息,请参见上文。
  • 我不确定你是否理解我的意思(我不会说英语)。当我创建自己的主窗口时,我知道如何创建内部框架。但我不知道如何让我的内部框架进入 Netbeans 平台的预先创建的主窗口
【解决方案2】:

我想你正在寻找的答案在这里:https://blogs.oracle.com/geertjan/jdesktoppane,-jinternalframe,-and-topcomponent

Geertjan Wielenga 展示了一个使用TopComponent 和内部JDesktopPane 的示例,您可以在其中附加一些JInternalFrame。

...
...
...
private JDesktopPane jdpDesktop;
private int openFrameCount = 0;
public DemoTopComponent() {
    initComponents();
    setName(NbBundle.getMessage(DemoTopComponent.class, "CTL_DemoTopComponent"));
    setToolTipText(NbBundle.getMessage(DemoTopComponent.class, "HINT_DemoTopComponent"));
    setLayout(new BorderLayout());
    jdpDesktop = new JDesktopPane();
    createFrame(); // Create first window
    createFrame(); // Create second window
    createFrame(); // Create third window
    //Add the JDesktop to the TopComponent
    add(jdpDesktop);
}
protected void createFrame() {
    MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true);
    jdpDesktop.add(frame);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {
        }
 }

class MyInternalFrame extends JInternalFrame {
    int xPosition = 30, yPosition = 30;
    public MyInternalFrame() {
        super("IFrame #" + (++openFrameCount), true, // resizable
            true, // closable
            true, // maximizable
            true);// iconifiable
        setSize(300, 300);
        setLocation(xPosition / openFrameCount, yPosition /   openFrameCount);
        // Add some content:
        add(new JLabel("hello IFrame #" + (openFrameCount)));
       }
}
...
...
...

【讨论】:

  • 你能至少提供一个例子来说明这是如何做到的吗?如果该链接在 X 年后消失,并且有人遇到这个问题并希望找到解决方案,您的帖子会为他们提供解决方案吗?通过添加相关示例,您的答案质量从 0(应该是评论)到 10(实际答案/解决方案)
  • 啊,太完美了——你在我发表评论时完成了编辑。我要收回我的旗帜,然后+1。这就是答案的样子。干得好。
  • @user8356314 同样在这里,我收回了我的标志并删除了我以前的 cmets(现在无关紧要)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
相关资源
最近更新 更多