【问题标题】:Java: how to make container's Jscrollpane background not opaque? (ie. transparent)Java:如何使容器的 Jscrollpane 背景不透明? (即透明)
【发布时间】:2012-07-27 21:20:51
【问题描述】:

这个例子编译得很好。你可以通过运行它看到我面临的问题。我希望 JPanel 直接在桌面上绘制,而没有可见的 JWindow 内容窗格。我还需要一个可见的 JScrollpane 以便水平移动 JPanel 数组!

感谢任何帮助。

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

【问题讨论】:

标签: java swing transparent


【解决方案1】:

试一试:

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

【讨论】:

  • @aubreybourke 太棒了,也许你想通过将问题标记为已回答来展示你的应用程序,这样 Xon 就可以恢复他应得的认可 ;)
  • @aubreybourke:有一个相关的例子here。您可以通过点击左侧的empty check mark 来接受这个答案。
  • 还有scrollPane.setOpaque(false); 我花了三个。
猜你喜欢
  • 1970-01-01
  • 2013-03-03
  • 2011-12-15
  • 2011-03-31
  • 1970-01-01
  • 2021-10-17
  • 2013-05-01
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多