2011-09-10 16 views
5

私はJFrameを完全に透明にすることができます。ボタン上でマウスを動かし(クリックしない)、ボタンからマウスを移動する(MouseListener経由でMouseExitedを呼び出す)まで、JButtonは部分的に透明です。何が起こるのは、JButtonのバックグラウンドが再び描画されるということです。そのため、ボタンの上または下でマウスを2回動かすと、ボタンは完全に不透明になります。完全に透明なJFrame上で部分的に透明なJButtonを作成するにはどうすればよいですか?

public class ButtonExample extends JWindow 
{ 
    public ButtonExample() 
    { 
     JButton But = new JButton("Testing"); 
     But.setBackground(new Color(0, 0, 0, 200)); 
     But.setForeground(new Color(70, 155, 255)); 
     this.add(But); 
     this.setBackground(new Color(0, 0, 0, 0)); 
     this.setMinimumSize(new Dimension(200,100)); 
     this.setVisible(true); 
    } 

    public static void main(String[ ] Args) 
    { 
     new ButtonExample(); 
    } 
} 
+0

すぐに役立つように、[SSCCE](http://pscode.org/sscce.html)を投稿してください。 –

+3

Haase and Guyの[Filthy Rich Clients](http://filthyrichclients.org/)を購入してください。それは透明なコンポーネントを実行するのに必要なすべての詳細に入ります。 Java Swingプログラマーのためのすべての**素晴らしい**本で。 –

答えて

8

問題は、実際にそれがところで

but.setOpaque(false); 

(起因する部分的に透明色に)いないとき、ボタンレポートは完全に不透明であることということです

編集

arggghh :-)規則が...申し訳ありません、ということ逃しました。

 /** 
     * @inherited <p> 
     */ 
     @Override 
     protected void paintComponent(Graphics g) { 
      if (!isOpaque() && getBackground().getAlpha() < 255) { 
       g.setColor(getBackground()); 
       g.fillRect(0, 0, getWidth(), getHeight()); 
      } 
      super.paintComponent(g); 
     } 

のように、多分、「詳細を取得し、しかし、しようとしなかった、私の頭の上から、私はあなたがpaintComponentをオーバーライドして、自分で絵の背景を処理するために必要だと思います、私たちはSwingXに何をすべきかをチェックする必要があります「不透明やって再び戻ってきたので... 2

は大丈夫、確認明日

編集戻ってくる - 編集したコードが正しく動作します。だから、要約すると:半透明の背景

  • とコンポーネントは、デフォルトの絵のメカニズムを混同しないように、彼らは不透明ではないことを報告しなければならない
  • 背景の絵を引き継ぐ必要があり、背景色で自分自身をそれを埋める(SwingX JXPanel fiはありませんalphaプロパティを明示的にサポート)あなたの便宜のために

によって、ここで間違った/正しい背景サイド・バイ・サイドの小さなRunnableの

public class TransparentButton { 

    public TransparentButton() { 
     JWindow incorrectOpaque = createWindow("incorrect opaque", true); 
     incorrectOpaque.setLocation(600, 600); 
     incorrectOpaque.setVisible(true); 
     JWindow correctOpaque = createWindow("correct opaque", false); 
     correctOpaque.setLocation(800, 600); 
     correctOpaque.setVisible(true); 
    } 


    private JButton createButton(final boolean opaque) { 
     JButton but = new JButton("Testing") { 

      /** 
      * @inherited <p> 
      * Overridden to take over background painting with 
      * transparent color. 
      */ 
      @Override 
      protected void paintComponent(Graphics g) { 
       if (!isOpaque() && getBackground().getAlpha() < 255) { 
        g.setColor(getBackground()); 
        g.fillRect(0, 0, getWidth(), getHeight()); 
       } 
       super.paintComponent(g); 
      } 

     }; 
     but.setBackground(new Color(0, 0, 0, 100)); 
     but.setForeground(new Color(70, 155, 255)); 
     but.setOpaque(opaque); 
     return but; 
    } 

    private JWindow createWindow(String text, boolean opaque) { 
     JWindow window = new JWindow(); 
     JButton but = createButton(opaque); 
     window.add(but); 
     window.add(new JLabel(""), BorderLayout.SOUTH); 
     window.setOpacity(0.5f); 
     window.setBackground(new Color(0, 0, 0, 0)); 
     window.setSize(new Dimension(200, 100)); 
     return window; 
    } 

    public static void main(String[] Args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 

       new TransparentButton(); 
      } 
     }); 
    } 

    @SuppressWarnings("unused") 
    private static final Logger LOG = Logger.getLogger(TransparentButton.class 
      .getName()); 
} 
+0

不透明をfalseに設定すると、ボタンの背景が完全に透明(塗りつぶされない)になります。これは私が望むものではありません。不透明が間違っているときに背景を表示するために私をさらに助けてくれますか? – Pete

+0

完璧な解決策、多くのありがとう。 – Pete

0

translucencyを試しましたか?

translucent and shaped window APIが最初にプライベートAPIとしてJava SE 6 Update 10リリースに追加されました。この機能は、JDK 7リリースのパブリックAWTパッケージに移動されました。

私は上記のリンクが役立つと思います。あなたが見るように私はJavaの命名に適合するように、フィールド名を変更:

+0

ウィンドウを透明にする問題はありません。この修正では、コンポーネント(この場合はJButton)のペイントを変更することが含まれている可能性があり、ウィンドウには直接関係しません。 – Pete

+0

hehehe ...あ、そうだよ! – gumuruh

関連する問題