如何让JComboBox中的内容显示在中心



当前我有这个JComboBox,如何将内容集中在其中?

String[] strs = new String[]{"15158133110", "15158133124", "15158133458"};
JComboBox com = new JComboBox(strs);

您需要创建一个Renderer,然后将其应用于JComboBox

DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); 
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER); 
com.setRenderer(dlcr); 

也导入这个,

import javax.swing.DefaultListCellRenderer; 
((JLabel)jComboBox1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

试试这个

最新更新