JInternalFrame如何隐藏标题栏?-Java

10

我在网上找到了一些代码,稍作修改。我想隐藏一个JInternalFrame的标题栏。

  JInternalFrame frame = new JInternalFrame();
  // Get the title bar and set it to null
  setRootPaneCheckingEnabled(false);
  javax.swing.plaf.InternalFrameUI ifu= frame.getUI();
  ((javax.swing.plaf.basic.BasicInternalFrameUI)ifu).setNorthPane(null);      

  frame.setLocation(i*50+10, i*50+10);
  frame.setSize(200, 150);
  //frame.setBackground(Color.white);      

  frame.setVisible(true);
  desktop.add(frame);

问题在于标题栏由于某种原因没有被隐藏。谢谢。

4个回答

16
我这样解决了这个问题:我继承了 JInternalFrame 并将以下代码添加到它的构造函数中。(由于使用了NetBeans的GUI Builder,因此我免费获得了子类化)
((javax.swing.plaf.basic.BasicInternalFrameUI)this.getUI()).setNorthPane(null);

在你的情况下,我认为


不错,这是正确的答案,不像 .setUI(null)! 我还在某个地方读到过,在某些事件(比如最小化窗口)之后可能需要再次执行此操作。 - Dmitry Avtonomov
这是正确的答案。当与 frame.setBorder(null); 结合使用时,将 JInternalFrame 作为顶级 JFrame 中的单个组件使用,就像它是一个 JPanel 一样有用。 - Parker
对我有用。我甚至本来都不想尝试,但它像魔法一样奏效。谢谢。 - George

15

首先将 internalframe 转换为 basicinternalframe。

操作如下:

BasicInternalFrameUI bi = (BasicInternalFrameUI)your_internalframe_object.getUI();
bi.setNorthPane(null);

接下来,您的标题栏将变为不可见状态。


2

其他人的说法。根据框架,UI 可能会得到更新,这可能会使其重新出现。所以对我来说,像这样初始化 JInternalFrame 是有效的:

        JInternalFrame internalFrame = new JInternalFrame() {
           @Override
           public void setUI(InternalFrameUI ui) {
               super.setUI(ui); // this gets called internally when updating the ui and makes the northPane reappear
               BasicInternalFrameUI frameUI = (BasicInternalFrameUI) getUI(); // so...
               if (frameUI != null) frameUI.setNorthPane(null); // lets get rid of it
           }
        };

0

对我来说,这个非常有效:

    putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
    getRootPane().setWindowDecorationStyle(JRootPane.NONE);
    ((BasicInternalFrameUI) this.getUI()).setNorthPane(null);
    this.setBorder(null);

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接