jPanel 没有出现在 jLayeredPane 中
jPanel not showing up inside jLayeredPane
我已经阅读了有关此问题的所有主题,但我无法弄清楚问题是什么。
下面的代码在单击按钮时调用的方法中,按钮是动态生成的,是的,控制台输出正在显示,因此调用了该方法。
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();
JLayeredPane
默认使用空布局。因此,您应该实现自己的 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();
}
});
}
}
我已经阅读了有关此问题的所有主题,但我无法弄清楚问题是什么。
下面的代码在单击按钮时调用的方法中,按钮是动态生成的,是的,控制台输出正在显示,因此调用了该方法。
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();
JLayeredPane
默认使用空布局。因此,您应该实现自己的 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();
}
});
}
}