Eclipse WindowBuilder,重叠JPanels [英] Eclipse WindowBuilder, overlapping JPanels

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

问题描述

我正在尝试重叠 JPanel 实例。将面板直接放在另一个面板上,位置和尺寸完全相同。每次我这样做,它将另一个面板移动到另一侧或下面,前一个面板位于另一个更大的面板内,并且其中有按钮。

I'm attempting to overlap JPanel instances. Put a panel directly on another, in the exact same position and exact size. Every time I do this it moves the other panel to the other side or underneath, the previous panel is inside another much larger one and has buttons in it.

我该怎么做?请记住它正在使用Window Builder工具。

How would I do this? Keep in mind it's using the Window Builder tool.

推荐答案

您可能还想查看 OverlayLayout ,见过此处。它不包含在传统的图库中,但它可能是感兴趣。

You might also want to look at OverlayLayout, seen here. It's not included in the conventional gallery, but it may be of interest.

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.OverlayLayout;

/** @see http://stackoverflow.com/a/13437388/230513 */
public class OverlaySample {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Overlay Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new OverlayLayout(panel));
        panel.add(create(1, "One", Color.gray.brighter()));
        panel.add(create(2, "Two", Color.gray));
        panel.add(create(3, "Three", Color.gray.darker()));
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private static JLabel create(final int index, String name, Color color) {
        JLabel label = new JLabel(name) {
            private static final int N = 64;

            @Override
            public boolean isOpaque() {
                return true;
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(index * N, index * N);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(index * N, index * N);
            }
        };
        label.setHorizontalAlignment(JLabel.RIGHT);
        label.setVerticalAlignment(JLabel.BOTTOM);
        label.setBackground(color);
        label.setAlignmentX(0.0f);
        label.setAlignmentY(0.0f);
        return label;
    }
}

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

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