Java:如何在未修饰的 JFrame 周围绘制边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20165698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to draw a border around an undecorated JFrame?
提问by Tim Visée
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
. I would like to draw this border around my undecorated JFrame
. I'm currently using the following method inside my JFrame
to set the border:
我目前拥有库中的DropShadowBorder
类(它扩展了javax.swing.border.Border
类)SwingX
,因此这是常规Border
. 我想在我未装饰的JFrame
. 我目前正在使用以下方法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 notwhat I would like though. I want to draw a border outside the frame. These kind of solutions usually cause other problems.
可以用来解决这个问题的一种方法是创建一个不同的未修饰的全屏窗口,它是透明的,普通窗口放在这个窗口的顶部。这个全屏窗口用于在其上绘制阴影,因此不需要在框架本身中绘制阴影。这是获得类似结果的解决方案,但这不是我想要的。我想在框架外画一个边框。这些类型的解决方案通常会导致其他问题。
回答by Robin Green
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. If a look and feel supports this feature, its getSupportsWindowDecorations( ) method will return true.
某些 Java 外观可以提供自己的标题栏、图标和窗口边框。如果外观支持此功能,则其 getSupportsWindowDecorations() 方法将返回 true。
So you need to implement your own Java Look & Feel.
所以你需要实现你自己的 Java Look & Feel。
回答by tucuxi
JFrames and JDialogs are the only (swing) windows that need to interact with the external windowing system. To get external shadows, you need external context. See this answer on how to get it:
JFrames 和 JDialogs 是唯一需要与外部窗口系统交互的(swing)窗口。要获得外部阴影,您需要外部上下文。有关如何获取它的信息,请参阅此答案:
回答by Hanzallah Afgan
Yes you can draw the borders around the undecorated JFrame. Just simply get the root pane of the JFrame and set its borders by setBorder(Border border) method.
是的,您可以在未修饰的 JFrame 周围绘制边框。只需简单地获取 JFrame 的根窗格并通过 setBorder(Border border) 方法设置其边框。
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();
}
}