我已经阅读了有关此问题的所有主题,但我无法弄清楚问题是什么。
以下代码位于按钮单击时调用的方法内,按钮是动态生成的,是控制台输出显示的,因此调用该方法。
System.out.println("loaditems method");
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(436, 480));
panel.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
panel.setBackground(Color.gray);
panel.setBorder(BorderFactory.createEtchedBorder(LOWERED, Color.lightGray, Color.gray));
panel.setOpaque(false);
panel.setLocation(jMainPanel.getLocation());
panel.setVisible(true);
jLayeredPane1.add(panel);
jLayeredPane1.revalidate();
答案 0 :(得分:3)
JLayeredPane
默认使用null-layout。因此,您应该实现自己的LayoutManager
来设置JLayeredPane
中每个组件的边界。
见这个例子:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestJLayeredPane {
public static class AbsoluteLayoutManager implements LayoutManager {
private Map<Component, Rectangle> bounds = new LinkedHashMap<Component, Rectangle>();
@Override
public void addLayoutComponent(String name, Component comp) {
bounds.put(comp, new Rectangle(comp.getPreferredSize()));
}
@Override
public void removeLayoutComponent(Component comp) {
bounds.remove(comp);
}
@Override
public Dimension preferredLayoutSize(Container parent) {
Rectangle rect = new Rectangle();
for (Rectangle r : bounds.values()) {
rect = rect.union(r);
}
return rect.getSize();
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override
public void layoutContainer(Container parent) {
for (Entry<Component, Rectangle> e : bounds.entrySet()) {
e.getKey().setBounds(e.getValue());
}
}
public void setBounds(Component c, Rectangle bounds) {
this.bounds.put(c, bounds);
}
}
protected void initUI() {
JFrame frame = new JFrame("test");
AbsoluteLayoutManager layout = new AbsoluteLayoutManager();
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(layout);
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
panel.setBackground(Color.gray);
panel.setVisible(true);
layeredPane.add(panel);
layout.setBounds(panel, new Rectangle(17, 59, 436, 480));
frame.add(layeredPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJLayeredPane().initUI();
}
});
}
}