如何在 Java 组合框中居中项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11008431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:24:11  来源:igfitidea点击:

How to center items in a Java combobox

javaswingjcomboboxrenderer

提问by AndroidDev

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:

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

myCombobox.setAlignmentY(CENTER_ALIGNMENT);

Thanks!

谢谢!

回答by aterai

Try this link: How to Use Combo Boxes (The Java? Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

试试这个链接:如何使用组合框(Java?教程 > 使用 JFC/Swing 创建 GUI > 使用 Swing 组件)

class ComboBoxRenderer extends JLabel
                   implements ListCellRenderer {
  public ComboBoxRenderer() {
    setOpaque(true);
    setHorizontalAlignment(CENTER);
    setVerticalAlignment(CENTER);
  }
  //. . .

or

或者

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

回答by mKorbel

You have look at Renderersconcept, described about in the JTable tutorial on Oracles pages, thic concept is similair for JComboBox, JList, JTableand JTree, in the Rendereryou can centering desired text

你必须看看Renderers概念,大约在描述上的神谕页JTable的教程,THIC概念similair为JComboBoxJListJTableJTree,在Renderer可以定心所需的文本