当jscrollpane opaque设置为false时,无法看到内容

时间:2013-09-01 08:52:30

标签: java swing jscrollpane opacity jsplitpane

嗨,我正在创建一个JSplitPane的程序,其背景是透明的,所以我在JScrollPane上添加了JSplitPane的两个面板...当我添加内容时面板它没有出现。

这是我正在使用的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JViewport;
import javax.swing.UIManager;
public class sample {
JFrame mainFrame;
JPanel mainPanel;
JPanel splitPaneLeftPanel;
JPanel splitPaneRightPanel;
JSplitPane splitPane;
JScrollPane rightPanelScroll;
JScrollPane leftPanelScroll;
JViewport viewport;
sample(){
    Frame();
    SplitPaneAndContent();
    mainFrame.setVisible(true);
     }
 public void Frame(){
        mainFrame = new JFrame();
        mainFrame.setTitle("Sample");
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setSize(1024,720);
        mainFrame.setMaximumSize(new Dimension(1366,768));
        mainFrame.setMinimumSize(new Dimension(800,600));
        mainFrame.setResizable(true);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

 public void SplitPaneAndContent(){
     mainPanel = new JPanel(new BorderLayout());
     splitPaneLeftPanel = new JPanel(new BorderLayout());
     splitPaneRightPanel = new JPanel(new BorderLayout());
     mainPanel.setBackground(Color.blue);
     JScrollPane rightPanelScroll = new JScrollPane(splitPaneRightPanel);
     JScrollPane LeftPanelScroll = new JScrollPane(splitPaneLeftPanel);
    viewport = new JViewport();
    rightPanelScroll.setViewport(viewport);
    rightPanelScroll.getViewport().setOpaque(false);
    JLabel Text = new JLabel("SAMPLE");
    Text.setBackground(Color.red);

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,LeftPanelScroll,rightPanelScroll);
    splitPane.setDividerLocation(300);

    splitPaneLeftPanel.setOpaque(false);
    splitPaneRightPanel.setOpaque(false);
    rightPanelScroll.setOpaque(false);
    splitPane.setOpaque(false);

     splitPaneRightPanel.add(Text, BorderLayout.CENTER);
     mainPanel.add(splitPane, BorderLayout.CENTER);
     mainFrame.add(mainPanel, BorderLayout.CENTER);
 }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new sample();
        } 
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

0 个答案:

没有答案