简体   繁体   English

Java:如何使容器的Jscrollpane背景不透明? (即透明)

[英]Java: how to make container's Jscrollpane background not opaque? (ie. transparent)

This example compiles fine. 这个例子很好。 And you can see from running the it, the problem I'm facing. 你可以从运行它看到我面临的问题。 I want the JPanels to paint directly on the Desktop, without a visible JWindow content pane. 我希望JPanels直接在桌面上绘制,而没有可见的JWindow内容窗格。 I also need a JScrollpane to be visible in order to shift through the JPanel array horizontally! 我还需要一个JScrollpane才能看到,以便横向移动JPanel数组!

Appreciate any help. 感谢任何帮助。

/*
 * 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();
    }}

Give it a try: 试试看:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM