Java 如何更改 JFrame 中标题栏的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2482971/
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
how can i change the color of titlebar in JFrame?
提问by Venkat
I am using the following code,
我正在使用以下代码,
UIManager.put("JFrame.activeTitleBackground", Color.red);
for change the toolbar color in JFrame. But it didn't work.
用于更改 JFrame 中的工具栏颜色。但它没有用。
Is it possible to change the color of titlebar in JFrame?
是否可以更改 JFrame 中标题栏的颜色?
采纳答案by Andrew Dyster
Its not possible. The top level JFrame is controlled by the look & feel of the underlying OS.
这是不可能的。顶层 JFrame 由底层操作系统的外观控制。
You CAN change the color of an InternalFrame
.
您可以更改InternalFrame
.
回答by yassine
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(Color.black ));
UIManager.put("InternalFrame.activeTitleForeground", new ColorUIResource(Color.WHITE));
UIManager.put("InternalFrame.titleFont", new Font("Dialog", Font.PLAIN, 11));
回答by guest123
//source : javax/swing/plaf/metal/MetalTitlePane.java
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
public class TitleColor
{
public static void main_helper (String args[])
{
JFrame f = new JFrame ();
f.setDefaultCloseOperation
(
JFrame.DISPOSE_ON_CLOSE
);
f.setSize (300, 300);
f.setLocationRelativeTo (null);
f.setUndecorated ( true );
f.getRootPane ().setWindowDecorationStyle
(
JRootPane.FRAME
);
JPanel panel = new JPanel ();
panel.setBackground ( java.awt.Color.white );
f.setContentPane ( panel );
DefaultMetalTheme z =
new DefaultMetalTheme ()
{
//inactive title color
public ColorUIResource
getWindowTitleInactiveBackground()
{
return new ColorUIResource
(java.awt.Color.orange);
}
//active title color
public ColorUIResource
getWindowTitleBackground()
{
return new ColorUIResource
(java.awt.Color.orange);
}
//start ActiveBumps
public ColorUIResource
getPrimaryControlHighlight()
{
return new ColorUIResource
(java.awt.Color.orange);
}
public ColorUIResource
getPrimaryControlDarkShadow()
{
return new ColorUIResource
(java.awt.Color.orange);
}
public ColorUIResource
getPrimaryControl()
{
return new ColorUIResource
(java.awt.Color.orange);
}
//end ActiveBumps
//start inActiveBumps
public ColorUIResource
getControlHighlight ()
{
return new ColorUIResource
(java.awt.Color.orange);
}
public ColorUIResource
getControlDarkShadow ()
{
return new ColorUIResource
(java.awt.Color.orange);
}
public ColorUIResource
getControl ()
{
return new ColorUIResource
(java.awt.Color.orange);
}
//end inActiveBumps
};
MetalLookAndFeel.setCurrentTheme
(
z
);
try
{
UIManager.setLookAndFeel
(
new MetalLookAndFeel ()
);
}
catch (Exception e)
{
e.printStackTrace ();
}
SwingUtilities.updateComponentTreeUI (f);
f.setVisible (true);
}
public static void main (final String args[])
{
SwingUtilities.invokeLater
(
new Runnable ()
{
public void run ()
{
main_helper ( args );
}
}
);
}
}
回答by guest123
For Windows10, in your main method, you can use:
对于 Windows10,在您的主要方法中,您可以使用:
UIDefaults uiDefaults = UIManager.getDefaults();
uiDefaults.put("activeCaption", new javax.swing.plaf.ColorUIResource(Color.gray));
uiDefaults.put("activeCaptionText", new javax.swing.plaf.ColorUIResource(Color.white));
JFrame.setDefaultLookAndFeelDecorated(true);
回答by Gee Bee
I think the goal is to achive a real application look on Win10. As it is not possible to change the window title color for real, the only way is to customize the window.
我认为目标是在 Win10 上实现真正的应用程序外观。由于无法真正更改窗口标题颜色,唯一的方法是自定义窗口。
Although examples installing a variant of the Metal LAF gives a good example, I found that this problem is more complicated. A real Win10 window has to have win10 border, transparent border (shadow) where the user can drag for resize. The title has to use Win10 icons, and hovers for the window buttons.
尽管安装 Metal LAF 变体的示例给出了一个很好的示例,但我发现这个问题更复杂。一个真正的 Win10 窗口必须有 Win10 边框、透明边框(阴影),用户可以在其中拖动以调整大小。标题必须使用 Win10 图标,并悬停在窗口按钮上。
I'd f.setUndecorated(true); and draw it on my own, and set the insets of the window so that the content will work as normal.
我会 f.setUndecorated(true); 并自行绘制,并设置窗口的插图,以便内容正常工作。
(Small print: although we all "know" that one can customize Swing with LAFs, in the real life writing a LAF is always a lot more complicated than just subclassing and drawing your own decoration. As an extra hassle, the LAF architecture does not express all the component properties, and the "native" LAF is quite far from the native look (e.g. check the win7 dropdown) or the feel (dropdowns don't slide out, win7 dropdowns have no hover, but buttons do). Oh, not to mention the lack of Windows-like components, such as a decent Office 2016 ribbon, or even a simple "toggle" of Win10. Really, you cannot do too much without brewing your own components.)
(小字:虽然我们都“知道”可以用 LAF 自定义 Swing,但在现实生活中,编写 LAF 总是比子类化和绘制自己的装饰要复杂得多。作为额外的麻烦,LAF 架构没有表达了所有的组件属性,“原生”LAF与原生的外观(例如查看win7下拉菜单)或感觉(下拉菜单不会滑出,win7下拉菜单没有悬停,但按钮可以)相去甚远。哦,更不用说缺少类似Windows的组件,比如像样的Office 2016功能区,甚至是Win10的一个简单“切换”。真的,不酝酿自己的组件,你做不了太多。)