2013-08-05 2 views
0

나는 제 코드를 작동시키기 위해 며칠을 노력하고 있지만 성공하지 못했습니다. 내 코드가 내 문제를 설명한다고 생각합니다. 나는 "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; 
    } 
} 

들으을 사전에 어떤 도움 :)

+0

JComboBox 및 Document (JTextPane/EditorPane 용)의 하드 코드 된 값으로 컴파일 할 수있는 [SSCCE] (http://sscce.org/), 짧은 실행 가능한 게시물을 더 빨리 게시 할 것입니다. – mKorbel

+0

이것은 이미 SSCCE 내 문제. – DJack

+0

은 (http://stackoverflow.com/q/9958004/714968) 또는 왜 귀찮게하지 않습니까? [Visual Font Designer by Darryl Burke] (http://tips4java.wordpress.com)/2008/11/30/visual-font-designer /) – mKorbel

답변

1

당신은 해시 코드를 참조하십시오 toString() 방법은 StyledEditorKit.FontFamilyAction이다.

적절한 문자열 ("Serif")을 반환하려면이 값을 재정의하거나 JComboBox.setRenderer(ListCellRenderer)을 사용하여 StyledEditorKit.FontFamilyAction으로 작성한 사용자 지정 렌더러를 적당한 텍스트로 설정해야합니다.

+0

끝내 주셔서 고맙습니다. 메뉴 모음을 만들고 메뉴 모음에 모든 것을 넣기로했습니다. 나는 그것을 여기에서 좋아한다 : http://stackoverflow.com/questions/766396/how-do-i-easily-edit-the-style-of-the-selected-text-in-a-jtextpane – DJack

관련 문제