2010-12-18 9 views
2

JLayeredPaneでsetBackground(Color)を呼び出すと、実際に背景色が設定されていないように見えます。何らかの理由で透明な背景を持つ必要があるJLayeredPaneと何か関係があると思いますか?とにかく、ここに問題を示すコードがあります。これはMac上にあるので、OSX LAFを使用していると思います。この結果はthis imageで示されます。JLayeredPaneに背景色を設定するにはどうすればよいですか?

import java.awt.Color; 
import java.awt.Dimension; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 

public class Main { 
    public static void main(String[] args) { 

     // This should be done with Swing Utilities, but I wanted to keep this 
     // simple... 
     JFrame foo = new JFrame(); 
     foo.setSize(new Dimension(500, 500)); 

     JLayeredPane bar = new JLayeredPane(); 
     bar.setPreferredSize(new Dimension(200, 200)); 
     bar.setBackground(Color.red); 

     // If you comment out the following line, you don't see any indication 
     // the JLayeredPane is actually being drawn to screen 
     bar.setBorder(BorderFactory.createLineBorder(Color.green)); 


     JPanel content = new JPanel(); 
     content.add(bar); 

     foo.setContentPane(content); 
     foo.setVisible(true); 
    } 
} 

答えて

8

あなたが階層化区画を不透明に作ってみることができます。

bar.setOpaque(true); 
+0

それは、デフォルトではfalseとして設定されている理由の任意のアイデア? – Hamy

+0

スイングのチュートリアルでは、階層化されたペイントを「配置のための3次元を提供するSwingコンテナ」として説明しています。だから私は2次元の位置を決めるJPanelとは違う理由は分かりません。 – camickr

関連する問題