2010-12-18 5 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

확실하지 않은 Swing 튜토리얼에서는 계층화 된 창을 "위치 지정을위한 세 번째 차원을 제공하는 스윙 컨테이너"로 설명합니다. 따라서 왜 2 차원 위치 지정을 수행하는 JPanel과 다른 점이 있을지 잘 모르겠습니다. – camickr