如何让用户使用JComboBox在JTextPane中更改其字体? [英] How do I allow a user to change his font in a JTextPane using a JComboBox?

查看:155
本文介绍了如何让用户使用JComboBox在JTextPane中更改其字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现互联网上有用的文档/教程的数量在JTextPanes的话题上是缺乏的。我试图做一个简单的文本处理器,我希望它能够从一个JComboBox中选择一个字体系列,该JComboBox根据用户在系统上安装的字体自行填充。但是,不管我尝试什么,我都无法弄清楚如何使它工作。

我拥有的是一个由JTextPane构建的工具栏类。目前,它有一堆样式按钮,用于设置对齐和粗体,斜体和下划线。



以下是我的代码:

  / ** 
* StyleBar用于自定义样式文档中的样式。这将需要一个
* JTextPane作为它的构造函数的参数,然后所有要采取的操作
*将影响其中的文本。
*
* @author Andrew
* /
public class StyleBar extends JToolBar {

private JLabel fontLbl;
私人JComboBox fontBox;

// ...无关紧要的问题。

/ **
* initEvents方法用于初始化
*工具栏的必要事件以实际完成其工作。它将焦点监听器建立在条上的
*按钮上,并为每个按钮提供单独的功能。它
*也建立了字体选择界面。
* /
public void initEvents(){
//对于工具栏中的每个项目,添加焦点侦听器以及
//格式化侦听器:
boldFormat.addActionListener(new StyledEditorKit.BoldAction()); // boldFormat是
boldFormat.addActionListener(resetFocus); //一个JButton

//同上我的italicsFormat和underlineFormat按钮(s)在工具栏中

leftAlign.addActionListener(new StyledEditorKit.AlignmentAction(null,
StyleConstants.ALIGN_LEFT));
leftAlign.addActionListener(resetFocus); //这个监听器只是将焦点
//重置到TextPane上。

//同上右键和centerAlign按钮

//设置字体列表,并向组合框添加一个监听器
buildFontMenu();

$ b / **
* buildFontMenu检测所有SYStem的可用字体并将
*添加到Font Selection框中。
* /
public void buildFontMenu(){
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final String [] fontNames = ge.getAvailableFontFamilyNames();
for(int i = 0; i< fontNames.length; i ++){
//我在这里做什么来获取String [i]中的条目,使得当
//用户选择它,它以类似于
的方式设置字体Family //按下boldFormat按钮或leftAlign按钮?



//一切都不相关



所以总结我的问题:我不知道如何正确设置监听器到组合框,以便a)它对所选的单个字体很敏感,并且b)以某种方式使用StyledEditorKit.FontFamilyAction使生活变得简单?



斜线,如果我正在接近这个错误的方式,我很乐意听到正确的方法。正如我所说,在互联网上的来源是不是很清楚这个问题。



非常感谢!

解决方案

StyledEditorTest Action 放在 JToolBar 中,但是这个想法是一样的。另请参阅Charles Bell的 HTMLDocumentEditor ,提及此处。例如,

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


I'm finding the amount of helpful documentation/tutorials on the internet are lacking when it comes to the topic of JTextPanes. I'm trying to do a simple text processor, and I want it to be able to select a font family from a JComboBox that populates itself based on the fonts a user has installed on their system. However, no matter what I try with experimenting, I can't figure out how to make it work.

What I have is a toolbar class that is built off of a JTextPane. Currently, it has a bunch of style buttons that work to set alignment and bold, italics and underline.

Here's my code:

/**
* 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

So to sum up my problem: I have no idea how to properly set the listener to the ComboBox such that a) it's sensitive to the individual font selected and b) somehow uses StyledEditorKit.FontFamilyAction to make life easy?

Slash, if I'm approaching anything about this the wrong way, I'd love to hear the right way. As I said, my sources on the internet aren't very clear on the subject.

Thanks so much!

解决方案

StyledEditorTest puts the Actions in a JToolBar, but the idea is the same. See also Charles Bell's HTMLDocumentEditor, mentioned here. For example,

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

Addendum: As you are using JComboBox, you can forward the event to the corresponding StyledEditorKit Action, which operates on the current selection by default.

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

这篇关于如何让用户使用JComboBox在JTextPane中更改其字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆