JButton(Java Swing)选择了背景颜色禁用更改

时间:2015-04-07 21:29:28

标签: java swing click jbutton

我想在点击它时禁用JButton上的颜色更改为GRAY。

我尝试过:

  1. UIManager.put( “Button.select”,NULL);
  2. 但是我发现对于Windows L& F来说,Button.select是不可用的。

    1. 我尝试创建JButton的子项并将其放在paint方法中:
    2. ...

      public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (this.isSelected()) {
                   int w = getWidth();
                   int h = getHeight();
                   g.setColor(backgroundColor); 
                   g.fillRect(0, 0, w, h);
                   g.setColor(Color.white); 
                  g.drawString(...);
               }   
          }
      

      但isSelected永远不会是真的

      1. 我尝试添加监听器并在点击时更改背景,但点击后仍然显示灰色背景。
      2. 点击后,我该怎么做才能禁用此灰色背景?

        这就是现在点击的方式:

        enter image description here

        这就是我想要的:

        enter image description here

0 个答案:

没有答案