나는 제 코드를 작동시키기 위해 며칠을 노력하고 있지만 성공하지 못했습니다. 내 코드가 내 문제를 설명한다고 생각합니다. 나는 "Seif"콤보 박스 아이템을 어떻게보아야할지 모르겠다. 액션의 해쉬 코드를 볼 수있다. 주석에있는 코드 부분은 제대로 작동하지 않는 나의 실패한 시도이다. 내가 여기 생각 내 코드의 해결책 그러나 나는 그것이 작동 얻을 수 없다 : 당신이 무시되지 않습니다 아마도 때문에 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;
}
}
들으을 사전에 어떤 도움 :)
JComboBox 및 Document (JTextPane/EditorPane 용)의 하드 코드 된 값으로 컴파일 할 수있는 [SSCCE] (http://sscce.org/), 짧은 실행 가능한 게시물을 더 빨리 게시 할 것입니다. – mKorbel
이것은 이미 SSCCE 내 문제. – DJack
은 (http://stackoverflow.com/q/9958004/714968) 또는 왜 귀찮게하지 않습니까? [Visual Font Designer by Darryl Burke] (http://tips4java.wordpress.com)/2008/11/30/visual-font-designer /) – mKorbel