2017-08-26 121 views
3

點擊我試圖點擊時讓我的JButton停止其背景變化時被突出顯示。我一直在閱讀和測試其他questions的答案,如such,但沒有人幫助過我。我對Java非常陌生,所以具體的細節會很有幫助,下面是我的代碼和演示的內容。另外,如果這很重要,我正在運行Ubuntu。停止按鈕在擺動

注意:如果我將鼠標懸停並懸停在上面,它也會觸發背景更改。

import javax.swing.*; 
import java.awt.*; 

public class RaisedButton extends JButton{ 

    private String  text  =  ""; 
    private String  type  =  "default"; 


    public RaisedButton(String text, String type) { 
     this.text = text; 
     this.type = type; 
     __init__(); 
    } 

    private void foundations() { 
     NotoFont noto = new NotoFont(true); 
     this.setText(this.text.toUpperCase()); 
     this.setRolloverEnabled(false); 
     this.setFocusPainted(false); 
     this.setBorderPainted(false); 
     this.setFont(noto.get()); 
     this.setPreferredSize(new Dimension(108 + this.text.length() * 4, 37)); 
    } 

    private void default_type() { 
     this.foundations(); 
     this.setBackground(Color.decode("#FFFFFFF")); 
     this.setForeground(Color.decode("#000000")); 
    } 

    private void primary_type() { 
     this.foundations(); 
     this.setBackground(Color.decode("#00BCD4")); 
     this.setForeground(Color.decode("#FFFFFF")); 
    } 

    private void secondary_type() { 
     this.foundations(); 
     this.setBackground(Color.decode("#FF4081")); 
     this.setForeground(Color.decode("#FFFFFF")); 
    } 

    private void disabled_type() { 
     this.foundations(); 
     this.setBackground(Color.decode("#E5E5E5")); 
     this.setForeground(Color.decode("#B2A4A4")); 
    } 

    private void __init__() { 
     switch(this.type.toLowerCase()) { 
      case "default": 
       default_type(); 
       break; 
      case "primary": 
       primary_type(); 
       break; 
      case "secondary": 
       secondary_type(); 
       break; 
      case "disabled": 
       disabled_type(); 
       break; 
     } 
    } 
} 

示範什麼發生的......

Demonstration

謝謝!

+1

你嘗試設置自定義[按鈕 - 模型(https://stackoverflow.com/a/22547218/3992939)? – c0der

+0

謝謝!我試過了,這次它似乎有效。我以前的嘗試一定是做錯了。 – tjkso

回答

0

在你foundations()方法,要麼改變

this.setRolloverEnabled(true); 

將其設置爲false爲:

this.setRolloverEnabled(false); 

或刪除分配給從JavaDoc,以退回到默認值false

詳細setRolloverEnabled

+0

更改了它,但問題仍在發生。 – tjkso

+1

根據JavaDoc:「一些外觀也許不實現翻轉效果;它們將忽略此屬性。」 – c0der

1

儘量把這個在RaisedButton構造:通過添加 setModel(new FixedStateButtonModel());foundations()

setUI(new BasicButtonUI()); 
+0

試過了。它工作正常。 +1 – c0der