开发者

How to change the color of titlebar in JFrame?

开发者 https://www.devze.com 2022-12-24 02:33 出处:网络
I am using the following code, UIManager.put(\"JFrame.activeTitleBackground\", Color.red); for change the toolbar color in JFrame. But it didn\'t work.

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 pos开发者_如何学Gosible to change the color of titlebar in JFrame?


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.


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


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


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


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:-

How to change the color of titlebar in JFrame?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消