2017-08-26 1 views
3

クリックしたときに背景の変更を停止するようJButtonを取得しようとしています。私はsuchのような他のquestionsからの回答を読んでテストしてきましたが、何も私を助けませんでした。私は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); 

が、それは偽のように設定するにはいずれかの変更:

this.setRolloverEnabled(false); 

かのJavaDocからデフォルト値にfalse

詳細をフォールバックする割り当てを削除setRolloverEnabled

+0

変更されましたが、問題は引き続き発生しています。 – tjkso

+1

javadocから:「一部のルックアンドフィールではロールオーバーエフェクトが実装されていない可能性がありますが、このプロパティは無視されます。 – c0der

1

RaisedButtonコンストラクタでこれを入れてみてください。

setUI(new BasicButtonUI()); 
+0

それを試してみてください。それはうまく動作します。 +1 – c0der

関連する問題