Java - How to fill a Windows L&F button with color instead of just the border?

元气小坏坏 提交于 2020-01-05 08:40:41

问题


I'm doing a few GUI tests with Java on Windows 7. I would like to use the Windows Look & Feel because Java's default "Metal" UI is really ugly IMO. When I set the background color of a button, it just colors the border around the button rather than fill its whole background.

public class GUITest {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {} // I know, not a good idea, but it's just a test

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JButton button = new JButton("Windows L&F");
        button.setFocusable(false);
        button.setBackground(Color.GREEN.darker());

        frame.add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

The result is this:

Whereas I'm aiming for something like this (but without using the Metal L&F):

Is there a way to do that with the Windows L&F, or is it simply not built in?


回答1:


Some PLAFs support (custom) colored buttons. Others don't.

One way to achieve what you seem to want is to write the button text in an image with green BG and use the image for an icon of a button without text.




回答2:


If you want green filled in the button simply use button.setBorderPainted(false);

which will fill the whole button with your color as shown in your second picture.



来源:https://stackoverflow.com/questions/22109821/java-how-to-fill-a-windows-lf-button-with-color-instead-of-just-the-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!