我正在尝试使用自定义LayoutManager,我不明白在一个本身有上层布局管理器的组件中使用它的微妙之处。
下面是一个测试程序,它使用一对JPanel制作两个帧。两个JPanel中的每一个都有一个薄的黑色边框,并使用我的WeirdGridLayout强制其子组件在网格中的正方形,JPanel高度从宽度计算。两个JPanel都在另一个使用BorderLayout的薄红色边框的JPanel中。
在一个框架中,带有WeirdGridLayout的JPanels安排在EAST和WEST,另一个框架 安排北部和南部。
问题是在北/南的情况下,如果我改变框架的宽度/高度,那么 具有WeirdGridLayout的两个JPanel具有正确的大小,但不是正确的位置(它们有间隙或垂直重叠)。
在东/西案例中,它最终错了。
我需要做些什么才能让布局管理器与外部布局管理器很好地配合?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.LayoutManager2;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*/
public class WeirdGridLayout implements LayoutManager2
{
static final private int GRIDGAP = 10;
static final private int COMPONENT_SIZE = 30;
static final private int GRIDSPACING = COMPONENT_SIZE + GRIDGAP;
final private List<Component> components
= new ArrayList<Component>();
@Override public void addLayoutComponent(Component comp, Object constraints) {
this.components.add(comp);
}
@Override public void addLayoutComponent(String name, Component comp) {
this.components.add(comp);
}
@Override public void removeLayoutComponent(Component comp) {
this.components.remove(comp);
}
@Override public float getLayoutAlignmentX(Container target) {
return Component.LEFT_ALIGNMENT;
}
@Override public float getLayoutAlignmentY(Container target) {
return Component.TOP_ALIGNMENT;
}
@Override public void invalidateLayout(Container target) {}
@Override public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
@Override public Dimension minimumLayoutSize(Container parent) {
return new Dimension(0,0);
}
@Override public void layoutContainer(Container parent) {
int x = GRIDGAP;
int y = GRIDGAP;
Dimension d = preferredLayoutSize(parent);
parent.setSize(d);
for (Component component : this.components)
{
component.setBounds(x, y, COMPONENT_SIZE, COMPONENT_SIZE);
x += GRIDSPACING;
if (x >= d.getWidth())
{
x = GRIDGAP;
y += GRIDSPACING;
}
}
}
@Override public Dimension preferredLayoutSize(Container parent) {
// how many blocks wide can we fit?
int n = this.components.size();
int nblockwidth = (parent.getWidth() - GRIDGAP) / GRIDSPACING;
int nblockheight = (nblockwidth == 0) ? 0
: ((n-1)/nblockwidth) + 1;
return new Dimension(
nblockwidth*GRIDSPACING+GRIDGAP,
nblockheight*GRIDSPACING+GRIDGAP);
}
/* ---- test methods ---- */
static public class ColorPanel extends JPanel {
final private Color color;
final private String label;
public ColorPanel(String label, Color color) {
this.label = label;
this.color = color;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.color);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.WHITE);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(this.label);
g.drawString(this.label, (getWidth()-w)/2,
(getHeight()+fm.getAscent())/2);
}
}
public static void main(String[] args) {
showFrame(true);
showFrame(false);
}
private static void showFrame(boolean eastWest) {
JFrame frame = new JFrame("WeirdGridLayout test: eastWest="+eastWest);
JPanel framePanel = new JPanel(new BorderLayout());
framePanel.setPreferredSize(new Dimension(400,200));
JPanel panel[] = new JPanel[2];
for (int i = 0; i < 2; ++i)
{
panel[i] = new JPanel(new WeirdGridLayout());
panel[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
final Random r = new Random();
for (int j = 0; j < 24; ++j)
{
Color c = new Color(
r.nextFloat(),
r.nextFloat(),
r.nextFloat());
JPanel subpanel = new ColorPanel(Integer.toString(j), c);
panel[i].add(subpanel);
}
}
framePanel.add(new JButton("test"), BorderLayout.NORTH);
JPanel bottomPanel = new JPanel(new BorderLayout());
framePanel.add(bottomPanel, BorderLayout.SOUTH);
if (eastWest)
{
bottomPanel.add(panel[0], BorderLayout.WEST);
bottomPanel.add(panel[1], BorderLayout.EAST);
}
else
{
bottomPanel.add(panel[0], BorderLayout.NORTH);
bottomPanel.add(panel[1], BorderLayout.SOUTH);
}
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
frame.setContentPane(framePanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:2)
关键是要了解顶级布局管理器正在做什么,并确保较低级别具有首选大小或其他属性设置,以使其正确呈现。
来自API(http://download.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html):
NORTH和SOUTH组件可能会水平拉伸; EAST和WEST组件可以垂直拉伸; CENTER组件可以水平和垂直拉伸以填充剩余的空间。
答案 1 :(得分:1)
回答主题:无:-) LayoutManager不是为与其他LayoutManagers交互而设计的,它们独立于他们负责的目标。