如何改变JFrame中标题栏的颜色?

发布于 2024-08-25 16:46:28 字数 165 浏览 7 评论 0 原文

我正在使用以下代码

UIManager.put("JFrame.activeTitleBackground", Color.red);

来更改 JFrame 中的工具栏颜色。但这没有用。

是否可以更改 JFrame 中标题栏的颜色?

I am using the following code,

UIManager.put("JFrame.activeTitleBackground", Color.red);

for change the toolbar color in JFrame. But it didn't work.

Is it possible to change the color of titlebar in JFrame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泛泛之交 2024-09-01 16:46:28

这是不可能的。顶层 JFrame 由外观和样式控制。底层操作系统的感觉。

您可以更改 InternalFrame 的颜色。

Its not possible. The top level JFrame is controlled by the look & feel of the underlying OS.

You CAN change the color of an InternalFrame.

画骨成沙 2024-09-01 16:46:28
//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 );
                    }
                }
            );
        }
    }
//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 );
                    }
                }
            );
        }
    }
风透绣罗衣 2024-09-01 16:46:28

对于Windows10,在你的main方法中,你可以使用:

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);

For Windows10, in your main method, you can use:

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);
拔了角的鹿 2024-09-01 16:46:28

是的,我找到了解决方案。我认为这对于现在看到这个的人可能会有所帮助。

使用 FlatLaf

因为有很多外观和感觉。您可以在此处查看它们。

要更改标题栏背景颜色和前景色,请使用以下命令:-

FlatLightLaf.setup(); //setting the look and feel
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getRootPane().putClientProperty("JRootPane.titleBarBackground", new Color(23,180,252));
       frame.getRootPane().putClientProperty("JRootPane.titleBarForeground", Color.white);

现在我可以实现此目的:-

在此处输入图像描述

Yes I found the solution. I think it may be helpful for people who are seeing this now.

Use FlatLaf

In that there are many look and feels. You can see them here.

To change the title bar background color and foreground color use this:-

FlatLightLaf.setup(); //setting the look and feel
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getRootPane().putClientProperty("JRootPane.titleBarBackground", new Color(23,180,252));
       frame.getRootPane().putClientProperty("JRootPane.titleBarForeground", Color.white);

Now I can achieve this:-

enter image description here

遗心遗梦遗幸福 2024-09-01 16:46:28

我知道我有点晚了,但这可能对其他人有帮助:

我搜索了整个互联网,找到了更改我的基于摇摆的应用程序的整个主题的方法。然后我找到了一篇关于这样做的文章:

有人可能会说:

这是不可能的,并且由操作系统控制。

如果您想访问标题栏,首先您应该有权访问本机 java 引擎库。幸运的是,有一种方法可以做到这一点:

第一步:JNA 使 Java 程序可以轻松访问本机共享库,而无需编写 Java 代码之外的任何内容。
因此,您可以使用此存储库来访问它们:JNA

Scroll down to readme and find the downloadable library.

根据您使用的平台,某些元素可能会有所延迟。因此,请确保使用 jna-platform-5.8.0.jar 使您的应用与任何平台兼容。

第二步:如果您不知道如何使用 JNA 库,那么您可以以此为例:示例

无论如何,这可以解决您有关标题栏颜色的问题;)

主要文章:外部链接

告诉我我的错误,帮助我提高英语写作水平:D

I know I'm a bit late, but this may help others:

I searched the whole internet to find the way to change the entire theme of my swing-based app. then I found an article about doing so:

Somebody may say:

Its not possible and controlled by OS.

If you want to access the title bar, first, you should have the access to the native java engine libraries. Fortunately, there is a way to it:

First Step: JNA provides Java programs easy access to native shared libraries without writing anything but Java code.
So you can use this repository to access to them: JNA.

Scroll down to readme and find the downloadable library.

Some elements may defer based on your platform you are using. So make sure you use jna-platform-5.8.0.jar to make your app compatible to any platforms.

Second Step: If you don't know how to use JNA libraries, then you could use this as an example: example

anyway, this could solve your problem about title bar color ;)

Main Article: External Link

Help me improve my writing in English by telling me my mistakes :D

假情假意假温柔 2024-09-01 16:46:28

我认为目标是在Win10上实现真正的应用程序外观。由于无法真正更改窗口标题颜色,唯一的方法是自定义窗口。

虽然安装 Metal LAF 变体的示例提供了一个很好的例子,但我发现这个问题更加复杂。真正的Win10窗口必须有win10边框,透明边框(阴影),用户可以拖动以调整大小。标题必须使用Win10图标,并悬停为窗口按钮。

我会 f.setUndecorated(true);并自己绘制它,并设置窗口的插图,以便内容正常工作。

(小字:虽然我们都“知道”可以使用 LAF 自定义 Swing,但在现实生活中编写 LAF 总是比仅仅子类化和绘制自己的装饰要复杂得多。作为一种额外的麻烦,LAF 架构并不表达了所有的组件属性,而“原生”的LAF与原生的外观(例如查看win7下拉菜单)或感觉(下拉菜单不会滑出,win7下拉菜单没有悬停,但按钮有)相差甚远哦,更不用说缺乏类似Windows的组件,例如像样的Office 2016功能区,甚至是Win10的简单“切换”,真的,如果不酿造自己的组件,你就无法做太多事情。)

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.

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.

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.

(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.)

鹿港小镇 2024-09-01 16:46:28
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));
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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文