我想在我的应用程序中创建一个默认的JComboBox。但是我在JComboBox项目的设置边框中遇到了一个小问题。
这里是JComboBox
package test.combobox;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;
public class MyComboBox extends JComboBox<Object> {
public MyComboBox() {
super();
setBackground(new Color(0xFFFFFF));
setFont(new Font("Tahoma", 0, 14));
}
}
我需要将JComboxBox的项边框设置为:
Border itemBorder = BorderFactory.createCompoundBorder(
new LineBorder(new Color(0xCCCCCC), 1, true),
BorderFactory.createEmptyBorder(0, 7, 0, 7));
测试ComboBox。
package test.combobox;
import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import net.sf.jasperreports.engine.JRException;
public class TestComboBox {
public static void main(String[] args) throws JRException {
UIManager.getCrossPlatformLookAndFeelClassName();
JFrame frame = new JFrame("MyComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300, 70));
MyComboBox cb = new MyComboBox();
cb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
cb.setPreferredSize(new Dimension(300, 30));
frame.getContentPane().add(cb);
frame.pack();
frame.setVisible(true);
}
}
答案 0 :(得分:2)
JComboBox
中的项目通过ListCellRenderer
界面呈现。
查看How to Use Combo Boxes和Providing a Custom Renderer示例
这是一个重要的概念,JTable
,JTree
和JList
也使用此概念......