有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我已经尝试了几天让我的代码工作,但没有成功。 我认为我的代码解释了我的问题。我不知道如何在组合框中看到项目,例如“衬线”。。我看到了动作的散列码。。注释中的代码部分是我试图使其正确工作的失败尝试。 我认为这是我的代码的解决方案,但我无法让它工作: 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;
    }
}

Thx提前提供任何帮助:)


共 (0) 个答案