【问题标题】:Is there any way to right align the text in a JCombobox有什么方法可以右对齐 JCombobox 中的文本
【发布时间】:2016-10-11 03:22:29
【问题描述】:

我想要一个右对齐的 JComboBox。我怎样才能做到这一点? 之前有人说过“您可以为 JComboBox 设置一个渲染器,它可以是一个具有 JLabel#setHorizo​​ntalAlignment(JLabel.RIGHT) 的 JLabel”,但我不知道该怎么做?

【问题讨论】:

    标签: java swing jcombobox renderer right-align


    【解决方案1】:

    之前有人说过“你可以为 JComboBox 设置一个渲染器,它可以是一个具有 JLabel#setHorizo​​ntalAlignment(JLabel.RIGHT) 的 JLabel”

    是的,默认渲染器是 JLabel,因此您无需创建自定义渲染器。您可以使用:

    ((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
    

    【讨论】:

    • +1,您的回答比我的好,感谢您提供此信息。
    • Idk...我认为更好的答案仍然是您的两个答案的组合。特别是,您的回答对我更有用,因为我使用的是自己的自定义渲染器。另一方面,我可以看到如果你只是在模型中使用一个普通的字符串数组(或者组合框中的对象的 toString() 就足够了),那么这样做会更有用。
    【解决方案2】:

    好吧,你可以使用 ListCellRenderer,像这样:

    import java.awt.Component;
    import java.awt.ComponentOrientation;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.SwingUtilities;
    
    public class ComboboxDemo extends JFrame{
        public ComboboxDemo(){
            JComboBox<String> comboBox = new JComboBox<String>();
            comboBox.setRenderer(new MyListCellRenderer());
            comboBox.addItem("Hi");
            comboBox.addItem("Hello");
            comboBox.addItem("How are you?");
    
            getContentPane().add(comboBox, "North");
            setSize(400, 300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        private static class MyListCellRenderer extends DefaultListCellRenderer {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                return component;
            }
        }
    
        public static void main(String [] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ComboboxDemo().setVisible(true);
                }
            });
        }
    }
    

    【讨论】:

    • 我无法选择方向 CENTER !!只有从左到右和从右到左
    【解决方案3】:

    这对我很有效

    comboFromDuration.setRenderer(new DefaultListCellRenderer() {
                @Override
                public void paint(Graphics g) {
                    setHorizontalAlignment(DefaultListCellRenderer.CENTER);
                    setBackground(Color.WHITE);
                    setForeground(Color.GRAY);  
                    setEnabled(false);
                    super.paint(g);
                }
            });
    

    为避免每次调用paint(Graphics) 时都使用setter,您还可以使用匿名构造函数块:

    comboFromDuration.setRenderer(new DefaultListCellRenderer() {
        {
            setHorizontalAlignment(DefaultListCellRenderer.CENTER);
            setBackground(Color.WHITE);
            setForeground(Color.GRAY);  
            setEnabled(false);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2010-11-26
      • 2010-11-22
      相关资源
      最近更新 更多