出于某种原因,窗口LAF将覆盖按钮的默认布局。这导致按钮变窄。但是,编辑器的宽度并没有增加以解释更窄的按钮,因此会出现间隙。以下是WindowsComboBoxUI中的代码:
protected LayoutManager createLayoutManager() {
return new BasicComboBoxUI.ComboBoxLayoutManager() {
public void layoutContainer(Container parent) {
super.layoutContainer(parent);
if (XPStyle.getXP() != null && arrowButton != null) {
Dimension d = parent.getSize();
Insets insets = getInsets();
int buttonWidth = arrowButton.getPreferredSize().width;
arrowButton.setBounds(WindowsGraphicsUtils.isLeftToRight((JComboBox)parent)
? (d.width - insets.right - buttonWidth)
: insets.left,
insets.top,
buttonWidth, d.height - insets.top - insets.bottom);
}
}
};
}
更好的布局可能是:
comboBox.setUI( new WindowsComboBoxUI()
{
@Override
protected LayoutManager createLayoutManager()
{
return new BasicComboBoxUI.ComboBoxLayoutManager()
{
public void layoutContainer(Container parent)
{
super.layoutContainer(parent);
System.out.println(editor.getBounds());
System.out.println(arrowButton.getBounds());
// if (XPStyle.getXP() != null && arrowButton != null)
// {
Dimension d = parent.getSize();
Insets insets = getInsets();
int buttonWidth = arrowButton.getPreferredSize().width;
boolean isLeftToRight = parent.getComponentOrientation().isLeftToRight();
arrowButton.setBounds(isLeftToRight
? (d.width - insets.right - buttonWidth)
: insets.left, insets.top, buttonWidth, d.height - insets.top - insets.bottom);
System.out.println(editor.getBounds());
System.out.println(arrowButton.getBounds());
Dimension size = editor.getSize();
editor.setSize(arrowButton.getLocation().x - 1, size.height);
// }
}
};
}
});
我添加了一些输出来显示在xp调整之前/之后编辑器宽度是如何变化的。另外,我不知道如何检查xp laf,因为xpstyle类不是公共的。
LAF的进口:
import javax.swing.plaf.basic.*;
import com.sun.java.swing.plaf.windows.*;