2013-09-25 78 views
0

我目前正在嘗試使放置在JScrollPane中的JList不透明。我試圖將所有內容設置爲setOpaque(false),但它仍然無效。JScrollPane中的透明JList

下面是代碼:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.DefaultListCellRenderer; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

public class NavigationView extends JPanel{ 

    private static final long serialVersionUID = 8033665282200374807L; 

    private JScrollPane pane; 
    private JList list; 

    public NavigationView() { 
     this.initialize(); 
     this.build(); 
     this.configure(); 
    } 

    public void initialize() { 
     this.pane = new JScrollPane(); 
     this.list = new JList(); 
    } 

    public void build() { 
     this.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill = GridBagConstraints.BOTH; 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 1; 
     gbc.weighty = 1; 
     gbc.gridwidth = 1; 
     gbc.insets = new Insets(5, 5, 5, 5); 
     this.add(this.pane, gbc); 
    } 


    public void configure() { 
     this.pane.setOpaque(false); 
     this.pane.setViewportView(this.list); 
     this.pane.getViewport().setOpaque(false); 
     this.list.setOpaque(false); 
     ((DefaultListCellRenderer) this.list.getCellRenderer()).setOpaque(false); 
    } 
} 

如果我這個面板添加到JFrame上用彩色背景我還是看到了NavigationView - 面板的邊界用白色背景。 我在做什麼錯?

回答

2

你不要使面板(NavigationView)本身透明

public void initialize() { 
    setOpaque(false); 
    this.pane = new JScrollPane(); 
    this.list = new JList(); 
} 
+0

+1你更快:) – StanislavL

2

我想你也應該調用this.setOpaque(false);

但是也檢查L & F.透明度可能被「抑制」L & F.我記得在Mac上與QuaQua L & F類似的錯誤。