从JComboBox移除边框 [英] Remove border from JComboBox
问题描述
您知道从Java中的JComboBox删除边框的任何方法吗?我尝试以下代码
Do you know any way to remove the border from a JComboBox in Java? I try the following code
public class ComboFrame extends JFrame {
public ComboFrame() {
JPanel container = new JPanel();
JComboBox cmb = new JComboBox(new String[] { "one", "two" });
cmb.setBorder(BorderFactory.createEmptyBorder());
container.add(cmb);
getContentPane().add(container);
pack();
}
}
和
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
EventQueue.invokeLater(new Runnable() {
public void run() {
new ComboFrame().setVisible(true);
}
});
}
不要问为什么有人要从combobx删除边框...我想这没有太大意义,但这就是想要的样子,我真的很好奇是否可以做到.我尝试了几种技巧,但没有一个起作用.
Don't ask why would someone want to remove the border from a combobx... I guess it does not make too much sense, but this is how it's wanted, and I got really curious if it can be done. I tried several tricks, but none of them worked.
最有效的方法是更改用户界面
The most effective was changing the UI with
cmb.setUI(new BasicComboBoxUI());
这使边界消失了,但是改变了L& F,如果可能,我需要保留Windows L& F.
This makes the border go away, but alters the L&F, and I need to keep the Windows L&F if possible.
谢谢.
推荐答案
我做了一些研究,发现此错误
I did a bit of research and found this bug
我为自己尝试过,它似乎确实影响了边境.您可能需要自己尝试以下一个或两个代码块.
I tried it for myself and it does seem to affect the border. You might want to try one or both of the following code blocks for yourself.
for (int i = 0; i < combo.getComponentCount(); i++)
{
if (combo.getComponent(i) instanceof JComponent) {
((JComponent) combo.getComponent(i)).setBorder(new EmptyBorder(0, 0,0,0));
}
if (combo.getComponent(i) instanceof AbstractButton) {
((AbstractButton) combo.getComponent(i)).setBorderPainted(false);
}
}
请务必注意,在错误条目的底部,您可以阅读以下内容:
It is important to note that at the bottom of the bug entry, you can read the following:
JButton保留了自己的边框,因此JComponent paintBorder()和 paintComponent()不了解JComboBox边框.
The JButton maintains it's own border so JComponent paintBorder() and paintComponent() has no awareness of the JComboBox border.
祝你好运
沙滩!
这篇关于从JComboBox移除边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!