着色JButton

时间:2013-09-09 13:07:56

标签: java swing colors jbutton

我在JAVA编程,我的操作系统是MAC。我正在尝试setForeground甚至是我的JButton的setBackground,但没有进展。我添加了:

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

但是,仍然没有变化!如何更改JButtons的颜色?

这是我的代码:

    public class New_button extends JButton{
    String up = new String("<html><font color=BLACK>^<size=50></font></html>");
    String down = new String("<html><font color=BLACK>v<size=50></font></html>");
    double value;
    int foldChange;

    public New_button(double val, int foldChan) {
        value = val;
        foldChange = foldChan;
        this.setSize(30, 30);
        this.setFont(this.getFont().deriveFont(50.0f));
        this.setBorder(null);
    }

    public void changeColor(double value){
         try {
                UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
             } catch (Exception e) {
                        e.printStackTrace();
             }
        if(value > 0.05){
            this.setForeground(Color.YELLOW);
            this.setOpaque(true);
            this.setBorderPainted(false);

        }
        else{
            this.setForeground(Color.RED);
            this.setOpaque(true);
            this.setBorderPainted(false);
        }
    }

    public void changeLable(int foldChange){
        if(foldChange == 1)
            this.setText(up);
        else
            this.setText(down);
    }    
}

由于

1 个答案:

答案 0 :(得分:0)

检查方式:

JButton button = new JButton("test");
button.setBackground(Color.RED);
button.setOpaque(true);

source