我有以下问题:
我需要为JComboBoxComponent
设置自定义用户界面(修改颜色,箭头按钮等)。目前,我在构造函数中执行此操作,如下所示:
public MyComboBox() {
setUI(new MyComboBoxUI);
}
问题是,在以这种方式设置UI之后,我以某种方式松开了组合框弹出窗口中列表的所有InputMap
和ActionMap
内容,即它不会使用箭头键向上或向下滚动列表。
我在这里做错了什么?
以下是代码:
public class CurrencyPairComboBox extends JComboBox {
public CurrencyPairComboBox() {
setUI(new CurrencyPairComboBoxUI());
}
}
class CurrencyPairComboBoxUI extends BasicComboBoxUI {
@Override
public void installUI(JComponent c) {
super.installUI(c);
listBox.setSelectionBackground(Color.BLACK);
listBox.setSelectionForeground(Color.WHITE);
}
@Override
protected JButton createArrowButton() {
arrowButton = new JButton();
arrowButton.setIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_ICON);
arrowButton.setRolloverIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_HOVER_ICON);
return arrowButton;
}
}
答案 0 :(得分:3)
我尝试过你在这里发布的代码,我没有看到任何键盘问题,一切都按照我的预期工作
import java.awt.*;
import javax.swing.*;
class ComboBoxTest extends JFrame {
private static final long serialVersionUID = 1L;
private JComboBox comboBox;
private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
ComboBoxTest() {
String[] items = {"Item1", "Item2"};
comboBox = new JComboBox(items);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(comboBox);
comboBox.setUI(new MyUI());
}
public JFrame getCurrentInstance() {
return this;
}
public static void main(String[] args) {
ComboBoxTest frame = new ComboBoxTest();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
class MyUI extends javax.swing.plaf.basic.BasicComboBoxUI {
@Override
protected JButton createArrowButton() {
JButton btn = new JButton();
btn.setIcon(infoIcon);
btn.setRolloverIcon(warnIcon);
return btn;
}
}
}