问题
Is there a method for a combobox in Java that will center the items in the combobox? I tried this but it didn't work:
myCombobox.setAlignmentY(CENTER_ALIGNMENT);
Thanks!
回答1:
Try this link: How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
class ComboBoxRenderer extends JLabel
implements ListCellRenderer {
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
//. . .
or
((JLabel)comboBox.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
回答2:
You have look at Renderers
concept, described about in the JTable tutorial on Oracles pages, thic concept is similair for JComboBox
, JList
, JTable
and JTree
, in the Renderer
you can centering desired text
来源:https://stackoverflow.com/questions/11008431/how-to-center-items-in-a-java-combobox