我在让 MatteBorder 使用从右到左的组件方向时遇到问题。下面的代码说明了这个问题。
我将一个 JPanel 放在另一个 JPanel 中。两个 JPanel 都有一个 MatteBorder。内边框的厚度为 10,10,10,10,而外边框的厚度为 20,20,20,10(注意外边框是不对称的)。
如果 JPanel 的组件方向为 LEFT_TO_RIGHT,则一切看起来都不错;但如果方向是 RIGHT_TO_LEFT,则边界会重叠。
考虑以下代码:
import java.awt.*;
import javax.swing.*;
public class Xyzzy extends JFrame{
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Xyzzy frame = new Xyzzy();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 5));
frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
{
JPanel outer = new JPanel();
outer.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 5));
outer.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
outer.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 10, Color.black));
JPanel inner = new JPanel();
inner.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 5));
inner.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
inner.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.red));
inner.add(Box.createRigidArea(new Dimension(10,10)));
outer.add(inner);
frame.add(outer);
}
{
JPanel outer = new JPanel();
outer.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
outer.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
outer.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 10, Color.blue));
JPanel inner = new JPanel();
inner.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
inner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
inner.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.yellow));
inner.add(Box.createRigidArea(new Dimension(10,10)));
outer.add(inner);
frame.add(outer);
}
frame.setSize(600, 600);
frame.setVisible(true);
}
});
}
}
代码的前半部分生成带有黑色和红色边框的 RIGHT_TO_LEFT JPanel,后半部分生成带有黄色和蓝色边框的 LEFT_TO_RIGHT JPanel。如果您运行该程序,您会在蓝色边框内看到黄色边框,但红色边框与黑色边框重叠。
为什么?
最佳答案
注意:这不是一个答案 - 只是一个带有代码的扩展评论:-)
从 OP 示例中剥离了一点,以专注于 FlowLayout-issue-with-asym-borders-in-RToL - 添加了一个标签而不是刚性区域以更好地(对我来说 :-) 看看它在哪里位于。布局简直要疯了......
public void run() {
MatteBorderCO frame = new MatteBorderCO();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(createRToLPanel(50, 100, true));
frame.add(createRToLPanel(100, 100, true));
frame.add(createRToLPanel(100, 50, true));
frame.setSize(600, 600);
frame.setVisible(true);
}
private JPanel createRToLPanel(int leftInset, int rightInset, boolean matte) {
JPanel outer = new JPanel();
Border asymBorder = matte ?
BorderFactory.createMatteBorder(20, leftInset, 20, rightInset, Color.black) :
BorderFactory.createEmptyBorder(20, leftInset, 20, rightInset) ;
outer.setBorder(BorderFactory.createCompoundBorder(
asymBorder, BorderFactory.createLineBorder(Color.RED)
));
JPanel inner = new JPanel();
inner.setBackground(Color.YELLOW);
inner.setBorder(BorderFactory.createLineBorder(Color.BLUE));
inner.add(new JLabel("RToL"));
outer.add(inner);
outer.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
return outer;
}
编辑
罪魁祸首是 flowLayout.moveComponents 中的位置计算不正确:
if (ltr) {
m.setLocation(x, cy);
} else { // RtoL orientation
// here's the error: location is adjusted relative to the target width
// without taking the insets into account ...
m.setLocation(target.width - x - m.width, cy);
}
编辑2
删除了假设的“简单”修复 - 不够好;-)
编辑3
无法抗拒,这是一个 FixedFlowLayout(正式未经测试,只是在上面的示例中使用,看起来不错!)
public static class FixedFlowLayout extends FlowLayout {
/**
* C&p mostly - RToL border fix implemented.
*/
protected int moveComponents(Container target, int x, int y, int width,
int height, int rowStart, int rowEnd, boolean ltr,
boolean useBaseline, int[] ascent, int[] descent) {
switch (getAlignment()) {
case LEFT:
x += ltr ? 0 : width;
break;
case CENTER:
x += width / 2;
break;
case RIGHT:
x += ltr ? width : 0;
break;
case LEADING:
break;
case TRAILING:
x += width;
break;
}
int maxAscent = 0;
int nonbaselineHeight = 0;
int baselineOffset = 0;
if (useBaseline) {
int maxDescent = 0;
for (int i = rowStart; i < rowEnd; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
if (ascent[i] >= 0) {
maxAscent = Math.max(maxAscent, ascent[i]);
maxDescent = Math.max(maxDescent, descent[i]);
} else {
nonbaselineHeight = Math.max(m.getHeight(),
nonbaselineHeight);
}
}
}
height = Math.max(maxAscent + maxDescent, nonbaselineHeight);
baselineOffset = (height - maxAscent - maxDescent) / 2;
}
int right = target.getWidth() - target.getInsets().right - getHgap();
for (int i = rowStart; i < rowEnd; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
int cy;
if (useBaseline && ascent[i] >= 0) {
cy = y + baselineOffset + maxAscent - ascent[i];
} else {
cy = y + (height - m.getHeight()) / 2;
}
if (ltr) {
m.setLocation(x, cy);
x += m.getWidth() + getHgap();
} else {
m.setLocation(right - m.getWidth(), cy);
right -= m.getWidth() + getHgap();
}
}
}
return height;
}
/**
* C&p, to be able to call the fixed moveComponent.
*/
@Override
public void layoutContainer(Container target) {
synchronized (target.getTreeLock()) {
Insets insets = target.getInsets();
int maxwidth = target.getWidth()
- (insets.left + insets.right + getHgap() * 2);
int nmembers = target.getComponentCount();
int x = 0, y = insets.top + getVgap();
int rowh = 0, start = 0;
boolean ltr = target.getComponentOrientation().isLeftToRight();
boolean useBaseline = getAlignOnBaseline();
int[] ascent = null;
int[] descent = null;
if (useBaseline) {
ascent = new int[nmembers];
descent = new int[nmembers];
}
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
m.setSize(d.width, d.height);
if (useBaseline) {
int baseline = m.getBaseline(d.width, d.height);
if (baseline >= 0) {
ascent[i] = baseline;
descent[i] = d.height - baseline;
} else {
ascent[i] = -1;
}
}
if ((x == 0) || ((x + d.width) <= maxwidth)) {
if (x > 0) {
x += getHgap();
}
x += d.width;
rowh = Math.max(rowh, d.height);
} else {
rowh = moveComponents(target, insets.left + getHgap(),
y, maxwidth - x, rowh, start, i, ltr,
useBaseline, ascent, descent);
x = d.width;
y += getVgap() + rowh;
rowh = d.height;
start = i;
}
}
}
moveComponents(target, insets.left + getHgap(), y, maxwidth - x,
rowh, start, nmembers, ltr, useBaseline, ascent,
descent);
}
}
public FixedFlowLayout() {
}
public FixedFlowLayout(int align, int hgap, int vgap) {
super(align, hgap, vgap);
}
public FixedFlowLayout(int align) {
super(align);
}
}
关于java - MatteBorder 不适用于从右到左的组件方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8402818/