Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何允许用户使用JComboBox更改JTextPane中的字体?_Java_Swing_Fonts_Jcombobox_Jtextpane - Fatal编程技术网

Java 如何允许用户使用JComboBox更改JTextPane中的字体?

Java 如何允许用户使用JComboBox更改JTextPane中的字体?,java,swing,fonts,jcombobox,jtextpane,Java,Swing,Fonts,Jcombobox,Jtextpane,我发现当涉及到JTextPanes主题时,互联网上缺少大量有用的文档/教程。我正在尝试做一个简单的文本处理器,我希望它能够从JComboBox中选择一个字体系列,该系列根据用户在其系统上安装的字体进行填充。然而,无论我尝试什么样的实验,我都不知道如何让它工作 我拥有的是一个基于JTextPane构建的工具栏类。目前,它有一系列样式按钮,用于设置对齐、粗体、斜体和下划线 这是我的密码: /** * The StyleBar is used to customize styles in a Styl

我发现当涉及到JTextPanes主题时,互联网上缺少大量有用的文档/教程。我正在尝试做一个简单的文本处理器,我希望它能够从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
因此,总结一下我的问题:我不知道如何正确地将侦听器设置为组合框,以便a对所选的单个字体敏感,b以某种方式使用StyledEditorKit.FontFamilyAction使生活变得简单

斯莱什,如果我用错误的方式处理这件事,我希望听到正确的方式。正如我所说,我在互联网上的信息来源对这个问题不是很清楚

非常感谢

将动作放在JToolBar中,但想法是一样的。另请参见查尔斯·贝尔的HTMLDocumentEditor。比如说,

bar.add(new StyledEditorKit.FontFamilyAction("Serif", Font.SERIF));
bar.add(new StyledEditorKit.FontFamilyAction("SansSerif", Font.SANS_SERIF));
附录:在使用JComboBox时,可以将事件转发到相应的StyledEditorKit操作,默认情况下,该操作对当前选择进行操作

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

这可能有点晚了,但我发现自己也遇到了类似的问题,虽然前面的答案确实有帮助,但这里有一个更完整的答案,可以帮助您使用计算机上所有可用的字体

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

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