Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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_Alignment_Jcombobox_Renderer - Fatal编程技术网

Java JComboBox如何显示项目的右端?

Java JComboBox如何显示项目的右端?,java,swing,alignment,jcombobox,renderer,Java,Swing,Alignment,Jcombobox,Renderer,我有以下Java代码: import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JComboBox; import javax.swing.JFrame; public class EnumsRightVisible { public void show() { JFrame frame = new JFrame("Combo box"); frame.se

我有以下Java代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class EnumsRightVisible {

    public void show() {
        JFrame frame = new JFrame("Combo box");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] items = {"aaaaaaaaa__________zzzzzzzzz",
                          "aaaaaaaaa__________zzzzzzzzz",
                          "aaaaaaaaa__________zzzzzzzzz"};
        JComboBox combo = new JComboBox(items);
        combo.setPreferredSize(new Dimension(100,20));
        frame.add(combo, BorderLayout.CENTER);
        frame.setLocation(600, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EnumsRightVisible enumsRightVisible = new EnumsRightVisible();
        enumsRightVisible.show();
    }
}
运行它,您可以看到可见文本是左向的

请注意,此代码不能解决我的问题(它将文本向右对齐,但仅当组合框展开时):

如何在同一窗口中显示文本的右端(…_zzzz)?
提前谢谢

使用自定义渲染器,该渲染器使用FontMetrics测量字符串的宽度,并手动执行省略号(…)。更多信息请点击此处:

方法1 方法2(当然,第一种更好)
感谢您提供的示例,但它们并不能解决我的问题—当组合框太小而无法显示整个文本时—只显示其最左边的部分,而不是文本的最后部分。
((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ComboBoxDemo extends JFrame {

    public ComboBoxDemo() {
        JComboBox comboBox = new JComboBox();
        
        ((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
        
        comboBox.addItem("Apple");
        comboBox.addItem("Orange");
        comboBox.addItem("Mango");
        
        getContentPane().add(comboBox, "North");
        setSize(200, 100);
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
                new ComboBoxDemo().setVisible(true);
    }
}
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.ListCellRenderer;

public class ComboBoxDemo extends JFrame {

    public ComboBoxDemo() {
        JComboBox comboBox = new JComboBox();

        setListCellRendererOf(comboBox);
        
        comboBox.addItem("Apple");
        comboBox.addItem("Orange");
        comboBox.addItem("Mango");

        getContentPane().add(comboBox, "North");
        setSize(200, 100);
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void setListCellRendererOf(JComboBox comboBox) {
        comboBox.setRenderer(new ListCellRenderer() {
            
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

                Component component = new DefaultListCellRenderer()
                        .getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

                component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                return component;
            }
        });
    }
    public static void main(String[] args) {
                new ComboBoxDemo().setVisible(true);
    }
}