简体   繁体   English

Java:如何在未修饰的JFrame周围绘制边框?

[英]Java: How to draw a border around an undecorated JFrame?

I currently have the DropShadowBorder class (which extends the javax.swing.border.Border class) from the SwingX library, so this is an instance of a regular Border . 我目前有来自SwingX库的DropShadowBorder类(它扩展了javax.swing.border.Border类),因此这是一个常规Border的实例。 I would like to draw this border around my undecorated JFrame . 我想在我未修饰的JFrame周围绘制这个边框。 I'm currently using the following method inside my JFrame to set the border: 我目前正在我的JFrame使用以下方法来设置边框:

DropShadowBorder b = new DropShadowBorder(Color.BLACK, 0, 10, 0.2f, 10, true, true, true, true);
this.getRootPane().setBorder(b);

Note: I'm using the root pane of the frame to draw the border on, because the frame doesn't support borders itself. 注意:我正在使用框架的根窗格来绘制边框,因为框架本身不支持边框。

The problem is that the borders are drawn inside the component itself, as you can see in the picture bellow, the shadowed border is drawn inside, against the bounds of the frame itself: 问题是边框是在组件内部绘制的,如下图所示,阴影边框是在框架内部的边界内绘制的:

在此输入图像描述

Note: The (shadowed) border is drawn inside the frame against it's bounds, instead of outside the frame. 注意:(阴影)边框是在框架内绘制的,而不是在框架外部。

It doesn't matter what kind of border is used, all of them are drawn inside the JFrame itself. 使用什么样的边框并不重要,它们都是在JFrame内部绘制的。

My question is: Is it possible to draw any border around the frame, not just inside against the bounds of the frame? 我的问题是:是否有可能在框架周围绘制任何边框,而不仅仅是在框架的边界内?


One way that could be used to solve this problem, is to create a different undecorated full screen window which is transparent, the normal window is put on top of this one. 可以用来解决这个问题的一种方法是创建一个透明的不同的未修饰的全屏窗口,将普通窗口放在这个窗口的顶部。 This full screen window is used to draw the shadow on, so the shadow doesn't need to be drawn in the frame itself. 此全屏窗口用于绘制阴影,因此不需要在帧本身中绘制阴影。 This is a solution to get a similar result, this is not what I would like though. 这是一个获得类似结果的解决方案,但这不是我想要的。 I want to draw a border outside the frame. 我想画框外的边框。 These kind of solutions usually cause other problems. 这些解决方案通常会导致其他问题。

Yes you can draw the borders around the undecorated JFrame. 是的,您可以在未修饰的JFrame周围绘制边框。 Just simply get the root pane of the JFrame and set its borders by setBorder(Border border) method. 只需简单地获取JFrame的根窗格并通过setBorder(边框边框)方法设置其边框。

getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

For example: 例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Borders2UndecoFrame extends JFrame{
    JLabel label = new JLabel("Welcome!", JLabel.CENTER);
    public Borders2UndecoFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(200, 200));
        add(label, BorderLayout.CENTER);
        setUndecorated(true);
        getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
        setVisible(true);

    }
    public static void main(String[] args) {
        new Borders2UndecoFrame();
    }

} 

在此输入图像描述

JFrames and JDialogs are the only (swing) windows that need to interact with the external windowing system. JFrames和JDialogs是唯一需要与外部窗口系统交互的(swing)窗口。 To get external shadows, you need external context . 要获得外部阴影,您需要外部上下文 See this answer on how to get it: 看到这个答案如何获得它:

Undecorated JFrame shadow 未修饰的JFrame阴影

From http://nadeausoftware.com/articles/2009/02/windows_java_tip_how_control_window_decorations 来自http://nadeausoftware.com/articles/2009/02/windows_java_tip_how_control_window_decorations

Some Java look and feels can provide their own title bar, icons, and window border. 一些Java外观可以提供自己的标题栏,图标和窗口边框。 If a look and feel supports this feature, its getSupportsWindowDecorations( ) method will return true. 如果外观支持此功能,则其getSupportsWindowDecorations()方法将返回true。

So you need to implement your own Java Look & Feel. 所以你需要实现自己的Java外观。

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

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