2012-07-30 118 views
5

這個例子編譯得很好。你可以看到運行它,我面臨的問題。我想讓JPanel直接在桌面上繪製,而沒有可見的JWindow內容窗格。我還需要一個JScrollpane才能看到,以便水平移動JPanel數組!Java:如何讓容器的Jscrollpane背景不透明? (即透明)

感謝任何幫助。

/* 
* SSCE.java 
* Short Self Contained Example 
* 
* Problem: Cant make the containers scrollpane non-opaque! (ie. transparent) 
*/ 
package fb; 

import com.sun.awt.AWTUtilities; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Toolkit; 
import javax.swing.BorderFactory; 
import javax.swing.JEditorPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JWindow; 
import javax.swing.ScrollPaneConstants; 

/** 
* 
* @author Aubrey 
*/ 
public class SSCE { 
    JWindow w = new JWindow(); 
    Container c = w.getContentPane(); 
    JPanel[] ps; 
    Toolkit toolkit = Toolkit.getDefaultToolkit(); 
    Dimension dim = toolkit.getScreenSize(); 
    int width = dim.width; 
    int height= dim.height; 

    public SSCE(){ 
    c.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5)); 

    JScrollPane scrollPane = new JScrollPane(c); 
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 
    scrollPane.setOpaque(false); 
    ps = new JPanel[19]; 

    for(int i=0; i<19; i++){ 
    ps[i]=new JPanel(); 
    ps[i].setLocation(0, 0); 
    ps[i].setSize(400, 400); 
    ps[i].setVisible(true); 
    JEditorPane area = new JEditorPane(); 
    area.setEditable(false); 
    area.setOpaque(false); 
    area.setSize(400, 400); 
    area.setForeground(Color.WHITE); 

    area.setText("Date: \nFrom: \n\nMessage: "+i); 

    ps[i].add(area); 
    ps[i].setBorder(BorderFactory.createLineBorder(Color.GRAY)); 
    ps[i].setBackground(Color.darkGray); 
    c.add(ps[i]); 

    } 
    if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { 
System.out.println("TranslucencySupported !!!!"); 
AWTUtilities.setWindowOpaque(w, false); 

}else{System.out.println("Translucency NOT Supported !!!!");} 

    //Problem seems to be here --> either the scrollPane or the Container is not non-opaque (ie. transparent)! HOW TO FIX THIS?? 
    w.setContentPane(scrollPane); 
    w.setLocation(0,height-490); 
    w.setSize(width, 450); 

    w.setVisible(true);     
} 
    public static void main(String[] args){ 

     new SSCE(); 
    }} 
+0

+1 [SSCCE(http://sscce.org/)。 – trashgod 2012-07-30 15:54:29

回答

13

試試看:

scrollPane.getViewport().setOpaque(false); 
    scrollPane.setBorder(null); 
+0

Ahhhhhh ----->不錯的一個!現在像魔術一樣工作:))))))))) – aubreybourke 2012-07-30 09:56:22

+0

@aubreybourke真棒,也許你想通過標記問題來回答你的應用程序來顯示你的應用程序,以便Xon可以重新獲得他應得的認可;) – MadProgrammer 2012-07-30 10:16:14

+0

@aubreybourke:有一個相關的例子[這裏](http://stackoverflow.com/a/3518047/230513)。您可以點擊左側的[空格勾號](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)接受此答案。 – trashgod 2012-07-30 15:53:14