2012-05-13 19 views
3

對於JTextPanes的主題,我發現互聯網上有用的文檔/教程的數量很少。我試圖做一個簡單的文本處理器,我希望它能夠從JComboBox中選擇一個字體系列,該JComboBox根據用戶在系統上安裝的字體自行填充。但是,無論我嘗試嘗試什麼,我都無法弄清楚如何使其工作。如何讓用戶使用JComboBox在JTextPane中更改他的字體?

我有一個工具欄類是建立在JTextPane之外的。目前,它有一堆樣式按鈕,用於設置對齊和粗體,斜體和下劃線。

這裏是我的代碼:

/** 
* The StyleBar is used to customize styles in a Styled Document. It will take a 
* JTextPane as an argument for its constructor and then all actions to be taken 
* will affect the text in it. 
* 
* @author Andrew 
*/ 
public class StyleBar extends JToolBar { 

    private JLabel fontLbl; 
    private JComboBox fontBox; 

     // ...Irrelevant stuff to the problem at hand. 

    /** 
    * The initEvents method is used to initialize the necessary events for the 
    * tool bar to actually do its job. It establishes the focus listener to the 
    * buttons on the bar, and gives each one its individual functionality. It 
    * also establishes the Font Selection interface. 
    */ 
    public void initEvents() { 
     //For each item in the tool bar, add the focus listener as well as the 
     //formatting listeners: 
     boldFormat.addActionListener(new StyledEditorKit.BoldAction()); //boldFormat is 
     boldFormat.addActionListener(resetFocus);      //a JButton 

     //Ditto for my italicsFormat and underlineFormat button(s) in the toolbar 

     leftAlign.addActionListener(new StyledEditorKit.AlignmentAction(null, 
       StyleConstants.ALIGN_LEFT)); 
     leftAlign.addActionListener(resetFocus); //This listener just resets focus 
                //back onto the TextPane. 

     //Ditto for my right and centerAlign buttons 

     //Set up the Font list, and add a listener to the combo box 
     buildFontMenu(); 
    } 

    /** 
    * The buildFontMenu detects all of the SYstem's available fonts and adds 
    * them to the Font Selection box. 
    */ 
    public void buildFontMenu(){ 
     GraphicsEnvironment ge = 
       GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     final String[] fontNames = ge.getAvailableFontFamilyNames(); 
     for (int i = 0; i < fontNames.length; i++){ 
      //What do I do here to take the entry at String[i] and make it so that when 
      //the user selects it it sets the font Family in a similar way to that of 
      //pressing the boldFormat button or the leftAlign button? 
     } 
    } 

    //Everything else is irrelevant 

所以總結一下我的問題:我不知道如何正確聽衆設置到ComboBox使得)是所選擇的單個字體敏感和b)不知何故使用StyledEditorKit.FontFamilyAction讓生活變得輕鬆?

斜線,如果我接近這個錯誤的方式,我很樂意聽到正確的方法。正如我所說,我在互聯網上的消息來源不太清楚。

非常感謝!

回答

3

StyledEditorTestAction寫入JToolBar,但是這個想法是相同的。另見Charles Bell的HTMLDocumentEditor,提及的here。例如,

bar.add(new StyledEditorKit.FontFamilyAction("Serif", Font.SERIF)); 
bar.add(new StyledEditorKit.FontFamilyAction("SansSerif", Font.SANS_SERIF)); 

附錄:由於您使用JComboBox,可以向前事件到相應的StyledEditorKitAction,它在默認情況下當前選擇操作。

JComboBox combo = new JComboBox(); 
combo.addItem("Serif"); 
combo.addItem("Sans"); 
combo.addActionListener(new ActionListener() { 

    Action serif = new StyledEditorKit.FontFamilyAction("Serif", Font.SERIF); 
    Action sans = new StyledEditorKit.FontFamilyAction("Sans", Font.SANS_SERIF); 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if ("Sans".equals(e.getActionCommand())) { 
      sans.actionPerformed(e); 
     } else { 
      serif.actionPerformed(e); 
     } 
    } 
}); 
0

這可能有點晚了,但我發現自己被困在了類似的問題,雖然以前的答案沒有幫助,這裏是當你想使用所有字體可在一個更完整的答案你計算機..

其中「字體」是在程序中可以使用含有所有所需字體的字符串數組

 final JComboBox jcb = new JComboBox(fonts); 

    final Action [] actions = new Action[fonts.length]; 

    for (int i = 0; i < actions.length; i++) 
    { 
     actions[i] = new StyledEditorKit.FontFamilyAction(fonts[i], fonts[i]); 
    } 

    jcb.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       for (int i = 0; i < actions.length; i++) 
       { 
        if (fonts[i].equals((String)jcb.getSelectedItem())) 
        { 
         actions[i].actionPerformed(event); 
         break; 
        } 
       } 
      } 
     }); 
相關問題