按Ctrl + A更改JTextPane中的字体 [英] Pressing Ctrl+A is changing the Font in JTextPane

查看:53
本文介绍了按Ctrl + A更改JTextPane中的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有JTextPane组件的Java Swing创建一个简单的Text编辑器.我添加了使文本变为粗体,斜体,下划线的代码,并且还添加了在JComboBox中获取系统字体的代码,以便可以更改JTextPane内容的字体.如果内容具有多种字体样式,则会根据光标位置显示相应的字体名称.

I am creating a simple Text editor using Java Swing with JTextPane component. I have added the code to make the text as Bold, Italics, Underline and also I have added code to get the system fonts in a JComboBox, so that I can change the font of the JTextPane content. And if the content has multiple font styles, it will display the corresponding font name according to the cursor position.

我有一个问题:如果内容具有多个字体样式,请按Ctrl + A组合键选择所有内容,并将整个内容字体更改为相同的字体(这是第一行的字体样式) .在按Ctrl + A之前:

I have an issue: if the content has more than one font styles, pressing Ctrl+A is selecting all the content and also it is changing whole content font to the same font (which is the font style of the first line). Before pressing Ctrl+A:

按Ctrl + A后,第一行的字体样式-Calibri字体将应用于所有三行,如下图:

After pressing Ctrl+A, the first line's font style - Calibri font is applied to all the three lines, below picture:

这是最简单的代码

import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;

public class Editor2 {
    private JTextPane editor;
    private DefaultStyledDocument doc;
    private DefaultComboBoxModel<String> fontFamilyComboBoxModel;
    private JComboBox<String> fontSizeComboBox;
    private JComboBox<String> fontFamilyComboBox;
    private AttributeSet attrs;
    private String fontFamilyStr;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Editor2().createAndShowGUI();
            }
        });
    }

    private void createAndShowGUI() {
        editor = new JTextPane();
        editor.setMargin(new Insets(5, 5, 5, 5));
        RTFEditorKit rtf = new RTFEditorKit();
        editor.setEditorKit(rtf);
        editor.addCaretListener(new MyCaretListener());
        JScrollPane editorScrollPane = new JScrollPane(editor);
        doc = new DefaultStyledDocument();
        initDocAttrs();
        editor.setDocument(doc);
        final String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        fontFamilyComboBoxModel = new DefaultComboBoxModel<>(fonts);
        fontFamilyComboBox = new JComboBox<String>(fontFamilyComboBoxModel);
        fontFamilyComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String name = (String) fontFamilyComboBox.getSelectedItem();
                new StyledEditorKit.FontFamilyAction("font-family-" + name, name).actionPerformed(ae);
                editor.requestFocus();
            }
        });

        final String[] fontSizes = { "Font Size", "10", "11", "12", "14", "16", "18", "20", "24", "28", "30", "34",
                "40", "50" };
        fontSizeComboBox = new JComboBox<String>(fontSizes);
        fontSizeComboBox.setEditable(false);

        JFrame frame = new JFrame("Text Editor");
        frame.add(fontFamilyComboBox, BorderLayout.SOUTH);
        frame.add(fontSizeComboBox, BorderLayout.NORTH);
        frame.add(editorScrollPane, BorderLayout.CENTER);
        frame.add(editorScrollPane);
        frame.setSize(800, 400);
        frame.setLocation(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        editor.requestFocusInWindow();
    }

    private void initDocAttrs() {
        Style style = doc.addStyle("my_doc_style", null);
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setFontFamily(style, "Arial");
        doc.setParagraphAttributes(5, doc.getLength(), style, true);
    }

    private class MyCaretListener implements CaretListener {
        @Override
        public void caretUpdate(CaretEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    attrs = ((StyledEditorKit) editor.getEditorKit()).getInputAttributes();
                    System.out.println(attrs);
                    fontFamilyStr = (String) attrs.getAttribute(StyleConstants.FontFamily);
                    System.out.println("Font: " + fontFamilyStr);
                    fontFamilyComboBox.setSelectedItem(fontFamilyStr);
                }
            });
            System.out.println("---");
        }
    }
}

为什么按Ctrl + A会更改所有字体样式?有人可以帮我吗?

Why pressing Ctrl+A is changing all the font style? can anyone help me on this?

推荐答案

fontFamilyComboBox.setSelectedItem(fontFamilyStr);

在您的CaretListener中,您正在更改选定的项目,这将导致调用组合框的ActionListener,这将导致您更改选定文本的字体.

In your CaretListener you are changing the selected item which results in the ActionListener for the combo box to be invoked, which causes you to change the font for the selected text.

一种解决方案是在更改所选项目之前从组合框中删除ActionListner:

One solution would be to remove the ActionListner from the combo box before changing the selected item:

comboBox.removeActionListener(...);
comboBox.setSelectedItem(fontFamilyStr);
comboBox.addActionListener(..)

这篇关于按Ctrl + A更改JTextPane中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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