java可以面板重叠不_java – Eclipse WindowBuilder,重叠JPanels

  • Post author:
  • Post category:java


您可能还想查看OverlayLayout,见于here.它不包含在传统的gallery中,但它可能是有意义的.

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 https://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;

}

}



版权声明:本文为weixin_29437633原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。