java - 如何在组合框中显示操作的名称

标签 java swing jcombobox itemlistener

我已经尝试了几天让我的代码正常工作,但没有成功。 我认为我的代码解释了我的问题。我不知道如何在组合框项目中查看例如“Serif”..我看到操作的哈希代码..注释中的代码部分是我使其正常工作的不成功尝试。 我认为这是我的代码的解决方案,但我无法让它工作: action listener in another class - java

 public class ComboBoxFontFamilyAction extends JFrame implements ItemListener {
    public ComboBoxFontFamilyAction() {
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new StyledEditorKit.ForegroundAction("Red", Color.red));
        comboBox.addItem(new StyledEditorKit.FontFamilyAction("Serif"
                .toString(), "Serif"));

        // When I click on this item I get the error
        comboBox.addItem(new FontSetting(new StyledEditorKit.ForegroundAction(
                "Red", Color.red), "Read what you want", comboBox));

        comboBox.addItemListener(this);

        getContentPane().add(comboBox, BorderLayout.SOUTH);
        JTextPane textPane = new JTextPane();
        textPane.setText("Some text to change attributes of.");
        getContentPane().add(new JScrollPane(textPane));

        // I think that i should do something like that
        // ItemListener itemListener = new FontSetting(new
        // StyledEditorKit.ForegroundAction("Red", Color.red),
        // "Wohoo",comboBox);
        // comboBox.addItemListener(itemListener);

    }

    public void itemStateChanged(ItemEvent e) {
        Action action = (Action) e.getItem();
        action.actionPerformed(null);
    }

    public static void main(String[] args) {
        ComboBoxFontFamilyAction frame = new ComboBoxFontFamilyAction();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}


    //public class FontSetting implements ItemListener {

public class FontSetting {

    private StyledEditorKit.StyledTextAction textAction;
    private String displayValue;

    // private JComboBox comboBox1;

    public FontSetting(StyledEditorKit.StyledTextAction textAction,
            String displayValue, JComboBox comboBox1) {
        this.textAction = textAction;
        this.displayValue = displayValue;
        // this.comboBox1 = comboBox1;

    }

    // public void itemStateChanged(ItemEvent e) {
    // Action action = (Action) e.getItem();
    // action.actionPerformed(null);
    // }

    public String toString() {
        return displayValue;
    }
}

提前感谢您的帮助:)

最佳答案

您看到哈希代码可能是因为您没有重写 StyledEditorKit.FontFamilyActiontoString() 方法。

您应该重写它以返回合理的字符串(“Serif”),或使用 JComboBox.setRenderer(ListCellRenderer) 设置将创建的 StyledEditorKit.FontFamilyAction 渲染为的自定义渲染器合理的文字。

关于java - 如何在组合框中显示操作的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18057872/

相关文章:

java - x x x 输入的 JFrame setsize 不是正方形

Java Swing,无法显示下拉菜单

java - JDK7半透明+jframe+jcombobox

java - Gradle共享依赖版本问题

java - 如何创建静态最终 java.net.URL?

java - 在 Eclipse Juno IDE 中将警告设置为错误

java - 在 JList 中显示文件对象的简单名称

java - 如何手动从org.springframework.context.MessageSource获取消息?

JAVA AWT SWING代码移植到Android NDK

java - jcombobox 正在列出,但无法选择 linux java1.6 中的项目