【问题标题】:How do I allow a user to change his font in a JTextPane using a JComboBox?如何允许用户使用 JComboBox 在 JTextPane 中更改其字体?
【发布时间】:2023-03-09 04:39:01
【问题描述】:

当谈到 JTextPanes 的主题时,我发现 Internet 上缺乏有用的文档/教程。我正在尝试做一个简单的文本处理器,并且我希望它能够从 JComboBox 中选择一个字体系列,该字体系列根据用户在其系统上安装的字体自行填充。但是,无论我尝试什么实验,我都无法弄清楚如何让它发挥作用。

我有一个工具栏类,它建立在 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 的侦听器,以便 a) 它对所选的单个字体敏感 b) 以某种方式使用 StyledEditorKit.FontFamilyAction 让生活变得轻松?

Slash,如果我以错误的方式处理这件事,我很想听听正确的方式。正如我所说,我在互联网上的消息来源不是很清楚。

非常感谢!

【问题讨论】:

    标签: java swing fonts jcombobox jtextpane


    【解决方案1】:

    StyledEditorTest 将Actions 放在JToolBar 中,但想法是一样的。另请参阅 Charles Bell 的 HTMLDocumentEditor,提到了 here。例如,

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

    附录:当您使用JComboBox时,您可以将事件转发到对应的StyledEditorKit Action,默认对当前选择进行操作。

    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);
            }
        }
    });
    

    【讨论】:

      【解决方案2】:

      这可能有点晚了,但我发现自己陷入了类似的问题,虽然前面的答案确实有帮助,但当你想使用计算机上所有可用的字体时,这里有一个更完整的答案..

      其中“fonts”是一个字符串数组,其中包含要在您的程序中使用的所有所需字体

              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;
                          }
                      }
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2016-12-10
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多