Eclipse中的WindowBuilder,重叠JPanels(Eclipse WindowBui

2019-06-21 04:45发布

我试图重叠JPanel实例。 直接把面板上的另一个,在完全相同的位置和精确的尺寸。 每次我做到这一点的其他面板移动到另一侧或下面,前面面板内的另一个更大的之一,并有按钮它。

我会怎么做呢? 请记住它的使用Window Builder工具。

Answer 1:

你可能也想看看OverlayLayout ,看到这里 。 它不包括在传统的画廊 ,但它可能会感兴趣。

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;
    }
}


Answer 2:

我试图重叠JPanels

使用JLayeredPane (从链接的教程如下图所示)。

直接在另一个把一个JPanel,

..或者一个CardLayout如图所示这里 ..

..depending上那您的意思是两个人,因为我理解他们是完全不同的效果。



Answer 3:

使用JDesktopPane (或者其超JLayeredPane )作为其内容,增加了窗格。

请参阅如何使用内部框架的例子 。


在这里你可以看到让组件覆盖的一个很好的方式,并弹出当光标停留在它:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class ShiftedStackPanel extends JPanel implements MouseListener,
                                                            ActionListener {

    private static final long serialVersionUID = 1988454751139668485L;

    private int layer;
    private JDesktopPane desktopPane;
    private Timer timer;
    private Component currentComponent;
    private int layerOfCurrent;
    private int shiftDivision;

    public ShiftedStackPanel() {
        this(4);
    }

    public ShiftedStackPanel(int shift) {
        shiftDivision = shift;

        setLayout(new BorderLayout(0, 0));

        desktopPane = new JDesktopPane();
        desktopPane.setBackground(SystemColor.window);
        super.add(desktopPane);
        timer = new Timer(1000, this);
        timer.setRepeats(false);

    }

    public Component add(Component c) {
        Dimension dim = c.getPreferredSize();
        c.setBounds(
                (desktopPane.getComponentCount() * (dim.width / shiftDivision)),
                0, dim.width, dim.height);
        desktopPane.add(c, new Integer(++layer));
        c.addMouseListener(this);
        return c;
    }

    public void remove(Component c) {
        throw new IllegalArgumentException(
                "Removal of component, not yet supported.");
        // FIXME: allow removal, and shift all latter comps, to left
    }

    public void removeAll() {
        desktopPane.removeAll();
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("JFrame Wrapper");
        ShiftedStackPanel p;
        f.setContentPane(p = new ShiftedStackPanel(4));
        p.add(new JTextField("ABCDEFGHI"));
        p.add(new JTextField("DEFGHIJKL"));
        p.add(new JTextField("GHIJKLMNO"));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setMinimumSize(new Dimension(400, 200));
        f.setLocationRelativeTo(null);
    }

    @Override
    public void mouseClicked(MouseEvent evt) {
        if (currentComponent != null) {
            Component c = (Component) evt.getSource();
            currentComponent = c;
            layerOfCurrent = desktopPane.getLayer(c);
            desktopPane.remove(c);
            desktopPane.add(c, new Integer(100));
        }
    }

    @Override
    public void mouseEntered(MouseEvent evt) {
        timer.start();
        Component c = (Component) evt.getSource();
        currentComponent = c;
        layerOfCurrent = desktopPane.getLayer(c);
    }

    @Override
    public void mouseExited(MouseEvent evt) {
        if ((currentComponent != null) && currentComponent == evt.getSource()) {
            desktopPane.remove(currentComponent);
            desktopPane.add(currentComponent, new Integer(layerOfCurrent));
            currentComponent = null;
            timer.stop();
        }
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        desktopPane.remove(currentComponent);
        desktopPane.add(currentComponent, new Integer(100));
    }
}

仍然有一些问题,使用需要重点部件时,但应与正常工作JLabelJPanel



文章来源: Eclipse WindowBuilder, overlapping JPanels