如何将项目集中在Java组合框中

时间:2012-06-13 05:00:04

标签: java swing jcombobox renderer

Java中是否有一种组合框的方法可以使组合框中的项目居中?我试过这个,但它不起作用:

myCombobox.setAlignmentY(CENTER_ALIGNMENT);

谢谢!

2 个答案:

答案 0 :(得分:12)

试试此链接: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);
  }
  //. . .

((JLabel)comboBox.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

答案 1 :(得分:5)

您已了解Renderers概念,在JTable tutorial on Oracles pages中描述,概念是JComboBoxJListJTable和{{1}的类似概念在JTree中,您可以将所需文字居中