简体   繁体   English

更改JButton的颜色而不更改其形状

[英]Change color of JButton without changing its shape

I want to change the color of JButton by: 我想通过以下方式更改JButton的颜色:

JButton button = new JButton();

button.setBackground(Color.decode("#00a5ff"));

In order for change to occur, I have to add: 为了进行更改,我必须添加:

button.setOpaque(true);
button.setBorderPainted(false);

However, this remove the curves around the edges and thus changes the shape of the button. 但是,这会删除边缘周围的曲线,从而更改按钮的形状。 Is there a way to just simply change to color and keep the other properties? 有没有一种方法可以简单地更改颜色并保留其他属性? Another example is the changing of color (getting darker) when you press a button without having changed its color. 另一个示例是在不更改按钮颜色的情况下按下按钮时更改颜色(变暗)。

Here is some code that illustrates the difference between the two buttons: 以下代码说明了两个按钮之间的区别:

    public static void main(String[] args) {
    JFrame frame = new JFrame("frame");
    frame.setSize(new Dimension(300, 300));
    JButton button1 = new JButton();
    JButton button2 = new JButton();

    button1.setPreferredSize(new Dimension(100,100));
    button2.setPreferredSize(new Dimension(100,100));
    button2.setBackground(Color.decode("#00a5ff"));
    button2.setBorderPainted(false);
    button2.setOpaque(true);

    JPanel pane1 = new JPanel(new FlowLayout());
    JPanel pane2 = new JPanel(new FlowLayout());

    pane1.add(button1);
    pane2.add(button2);
    frame.add(pane1, BorderLayout.WEST);
    frame.add(pane2, BorderLayout.EAST);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

Thanks 谢谢

I literally just tested the following: 我实际上只是测试了以下内容:

JFrame frame = new JFrame("frame");
frame.setSize(new Dimension(300, 300));
JButton button = new JButton();

button.setBackground(Color.decode("#00a5ff"));
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

It seemed to work, but I am working on Ubuntu Studio 16.04. 它似乎可以工作,但是我正在使用Ubuntu Studio 16.04。 If you notice that it does not work, then let me know. 如果您发现它不起作用,请告诉我。 Could you please show the result of your white button or your failed button (if it still doesn't work)? 您能否显示白色按钮或失败按钮的结果(如果仍然不起作用)?

Perhaps you have something else going on in your code. 也许您的代码中还有其他内容。 I altered a small example I have and the color changes with only setBackground(Color) using regular Button and using JButton. 我更改了一个小示例,使用常规Button和JButton仅使用setBackground(Color)更改了颜色。 See the following... 请参阅以下内容...

public static void main(String[] args) {
    Frame frame = new Frame();
    Panel panel = new Panel();
    Button closeButton = new Button("Close");
    closeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            frame.dispose();
        }           
    });
    closeButton.setBackground(Color.decode("#00a5ff"));
    panel.add(closeButton);
    frame.add(panel);
    frame.setSize(200, 100);
    frame.setLocation(200, 50);
    frame.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM