Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让JComboBox中的内容显示在中心?_Java_Swing_Center_Jcombobox_Cellrenderer - Fatal编程技术网

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

Java 如何让JComboBox中的内容显示在中心?,java,swing,center,jcombobox,cellrenderer,Java,Swing,Center,Jcombobox,Cellrenderer,目前我有这个JComboBox,我如何才能将内容集中在里面 String[] strs = new String[]{"15158133110", "15158133124", "15158133458"}; JComboBox com = new JComboBox(strs); 您需要创建一个渲染器,然后将其应用于JComboBox DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); dlcr.setHorizont

目前我有这个
JComboBox
,我如何才能将内容集中在里面

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

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

DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); 
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER); 
com.setRenderer(dlcr); 
也进口这个,

import javax.swing.DefaultListCellRenderer; 

试试这个

在我(愚蠢地)建议HTML格式之前,我很高兴你做到了这一点。现在我可以假装我再也不会那样说了我会比“简单得多”更进一步。你的答案最好归类为“正确的方法”。HTML格式是一种黑客行为。
((JLabel)jComboBox1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);