简体   繁体   English

从JFrame移除标题栏后保留边框

[英]Preserve border after removing title bar from JFrame

I want to remove title bar from JFrame, so I call setUndecorated(true) on that JFrame, but I would like to preserve border (nifty gradient) on that JFrame, which is present when decoration is on? 我想从JFrame删除标题栏,所以我在该JFrame上调用setUndecorated(true),但是我想在该JFrame上保留边框(漂亮的渐变),当装饰打开时会出现该边框? Can I do that? 我可以那样做吗? Something like getting border instance for LookAndFeel default or make gradient border myself? 像让LookAndFeel的边框实例默认还是自己制作渐变边框?

The default system LookAndFeel window borders are drawn by system, not Java, so there is no way to remove title bar from the window alone. 默认系统LookAndFeel窗口边框是由系统而不是Java绘制的,因此无法单独从窗口中删除标题栏。 The only thing you can do is undecorate your window and draw border by yourself (and yes, to fully copy system border you will have to put a lot of effort into it). 您唯一可以做的就是取消装饰窗口并自己绘制边框(是的,要完全复制系统边框,您将需要付出很多努力)。

Maybe something like that could be available in SWT , but to use it you will have to abandon standart Swing. 也许SWT中可以使用类似的功能,但是要使用它,您将不得不放弃标准的Swing。

You can accomplish this visually by creating a JPanel and giving it a border, then setting the panel as your frame's content. 您可以通过创建JPanel并为其指定边框,然后将面板设置为框架的内容来直观地完成此操作。

public class Undecorated {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel borderedPanel = new JPanel();

    //Use any border you want, eg a nice blue one
    borderedPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLUE));

    frame.setContentPane(borderedPanel);
    frame.setUndecorated(true);
    frame.setSize(new Dimension(200, 200));
    frame.setVisible(true);
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM