2012-05-13 6 views
3

JTextPanesのトピックについては、インターネット上の参考資料/チュートリアルの量が不足しています。私はシンプルなテキストプロセッサをやろうとしています。ユーザーがシステムにインストールしたフォントに基づいて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 

だから、私の問題を総括する:私はきちんとa)は、個々のフォント選択、Bに敏感だようなコンボボックスにリスナーを設定する方法は考えている)何とかLifeを簡単にするためにStyledEditorKit.FontFamilyActionを使用していますか?

スラッシュ、これについて何か間違った方法で近づいているなら、正しい方法を聞いてみたいと思います。私が言ったように、インターネット上の私の情報源はあまり明確ではありません。

ありがとうございます!

答えて

3

StyledEditorTestActionJToolBarに置きますが、考え方は同じです。 Charles BellのHTMLDocumentEditorhereを参照してください。例えば、

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; 
        } 
       } 
      } 
     }); 
で使用する全ての目的のフォントを含む文字列の配列です

関連する問題