GUI中重叠的JPanels [英] overlapping JPanels in the GUI

查看:149
本文介绍了GUI中重叠的JPanels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用BorderLayout.我有一个主面板,在中间添加了两个JPanels.我希望其中之一是透明的. 我的代码是:

I am using BorderLayout in my application. I have a main panel to which I add two JPanels at the center. I want one of them to be transparent. My code is :

 mainPanel = new JPanel();
 mainPanel.setLayout(new BorderLayout());
 mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
 mainPanel.add(getSituationPanel(), BorderLayout.CENTER);

这两个功能的代码是:

public JScrollPane getGraphPaneScrollPane() {
    if (graphPaneScrollPane == null) {
        graphPaneScrollPane = new JScrollPane();
        graphPaneScrollPane.setViewportView(getGraphEditorPane());
    }
    return graphPaneScrollPane;
}
private JScrollPane getSituationPanel(){
    if(situationPanel == null){
        logs.debug("Initializing Situation Panel");

        situationPanel = new JScrollPane();

        situationLabel = new JLabel("");
        situationLabel.setVerticalTextPosition(JLabel.BOTTOM);
        situationLabel.setHorizontalTextPosition(JLabel.CENTER);
        situationLabel.setVerticalAlignment(JLabel.TOP);
        situationLabel.setHorizontalAlignment(JLabel.CENTER);
        situationLabel.setBorder(BorderFactory.createTitledBorder(""));
        situationLabel.setBackground(Color.WHITE);
        situationLabel.setOpaque(true);
        situationLabel.setVerticalAlignment(SwingConstants.TOP);

        situationPanel.setViewportView(situationLabel); 

    } 

    return situationPanel;
}

现在,我要使contextPanel透明并且使getGraphPaneScrollPane在GUI中高于它,因为getGraphPaneScrollPane是画布,我用它来绘制节点.

Now I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI, because getGraphPaneScrollPane is the canvas, which I use to draw nodes.

推荐答案

我想使censingPanel透明并且使getGraphPaneScrollPane在GUI中高于它,

I want situationPanel to be transparent and getGraphPaneScrollPane to be above that in the GUI,

最上面的面板是需要透明的面板.如果顶部面板是不透明的,那么您将永远不会在顶部面板下方看到该面板.

The panel that is on top is the panel that needs to be transparent. If the panel on top is opaque then you will never see the panel under the top panel.

所以最后要更改布局是我想要的.

So making changes to the layout is the last thing I want.

这就是您需要做的.您不能只将两个面板添加到一个面板中,并期望它按照您希望的方式工作.大多数Swing布局管理器旨在将组件二维放置,而不是彼此重叠.

Well that is what you are going to need to do. You can't just add two panels to one panel and expect it to work the way you want it to. Most Swing layout managers are designed to lay out components in two dimensions, not on top of one another.

您当前的代码是:

mainPanel.setLayout(new BorderLayout());
mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.add(getSituationPanel(), BorderLayout.CENTER);

您可以尝试使用OverlayLayout,它旨在将面板布置在另一个之上.该代码应类似于:

You could try using the OverlayLayout, it is designed to lay out panels on top of on another. The code should be something like:

JPanel overlay = new JPanel();
overlay.setLayout( new OverlayLayout(overlay) );
overlay.add(getSituationPanel(), BorderLayout.CENTER); // add transparent panel first
overlay.add(getGraphPaneScrollPane(), BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout()); 
mainPanel.add(overlay);

这篇关于GUI中重叠的JPanels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆