在JTextPane中转换为大写或小写时,文本样式和格式被更改

时间:2018-12-11 11:18:54

标签: java swing jtextpane

我正在创建一个简单的RTF文档编辑器。我添加了代码来创建新的RTF文档,打开RTF文档,保存文档以及其他格式功能,例如粗体,斜体,下划线和更改大小写(从大写字母转换为小写字母,反之亦然)。我正在使用JTextPane组件。

我的问题:虽然JTextPane的内容包含不同的字体和具有不同颜色的不同字体大小,但是如果我选择所有内容并选择“ UPPERCASE”菜单选项,则内容将变为大写,但是所有不同的字体,字体大小和颜色更改为相同的字体,相同的字体大小和颜色,请验证图像: 转换为大写之前:

enter image description here

选择所有内容并选择“大写”选项:

enter image description here

转换为大写字母后:

enter image description here

我该如何解决?提前致谢。 最小代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

public class MyNotepadMini implements ActionListener {
    public JFrame frame;
    public JPanel panel;
    public JTextPane textPane;
    public RTFEditorKit rtf;
    public StyleContext styleContext;
    public Document document;
    public JScrollPane scrollPane;
    public JToolBar toolBar;
    public StyledDocument styledDocument;
    public Style defaultStyle;
    public AttributeSet attrs;
    public Style style;
    public MutableAttributeSet mas;
    public JButton lowerAndUpperCaseBtn;
    public JPopupMenu popupMenu;
    public JMenuItem lowerCaseMI;
    public JMenuItem upperCaseMI;
    public SimpleAttributeSet simpleAttrs;

    public MyNotepadMini() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        frame = new JFrame("My Wordpad");
        panel = new JPanel(new BorderLayout());              
        toolBar = new JToolBar();
        toolBar.setBounds(0,0,100,30);

        rtf = new RTFEditorKit();
        textPane = new JTextPane();
        textPane.setEditorKit(rtf);
        textPane.setMargin(new Insets(10,5,5,5));
        styleContext = new StyleContext();
        mas = textPane.getInputAttributes();
        simpleAttrs = new SimpleAttributeSet();
        styledDocument = textPane.getStyledDocument();
        textPane.setDocument(styledDocument);

        scrollPane = new JScrollPane();
        scrollPane.getViewport().add(textPane);

        lowerAndUpperCaseBtn = new JButton("Change Case");
        lowerAndUpperCaseBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                int startPosition = 0;
                int endPosition = 0;
                if ((textPane.getSelectionStart() != textPane.getSelectionEnd())) {
                    startPosition = textPane.getSelectionStart();
                    endPosition = textPane.getSelectionEnd();
                    textPane.setSelectionStart(startPosition);
                    textPane.setSelectionEnd(endPosition);
                    textPane.getCaret().setSelectionVisible(true);
                }
            }
        });
        popupMenu = new JPopupMenu();
        lowerCaseMI = new JMenuItem("lowercase");
        upperCaseMI = new JMenuItem("UPPERCASE");
        popupMenu.add(lowerCaseMI);
        popupMenu.add(upperCaseMI);

        lowerAndUpperCaseBtn.addActionListener(this);
        lowerCaseMI.addActionListener(this);
        upperCaseMI.addActionListener(this);

        toolBar.setFloatable(false);        
        toolBar.add(lowerAndUpperCaseBtn);
        toolBar.setBackground(Color.WHITE);
        scrollPane.setPreferredSize(new Dimension(600,400));

        textPane.setFont(new Font("Arial", Font.PLAIN, 12));
        textPane.setInheritsPopupMenu(true);
        panel.add(toolBar, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(new JLabel(" "), BorderLayout.EAST);
        panel.add(new JLabel(" "), BorderLayout.WEST);
        panel.add(new JLabel(" "), BorderLayout.SOUTH);

        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        textPane.requestFocus();                
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new MyNotepadMini();
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == lowerAndUpperCaseBtn) {                                               
            popupMenu.show(lowerAndUpperCaseBtn, 0, lowerAndUpperCaseBtn.getBounds().height);
        } else if (ae.getSource() == lowerCaseMI) { 
            boolean lowerCaseFlag = false;
            int startPosition = 0;
            int endPosition = 0;
            try {
                if ((textPane.getSelectionStart() != textPane.getSelectionEnd()) && (!lowerCaseFlag)) {
                    startPosition = textPane.getSelectionStart();
                    endPosition = textPane.getSelectionEnd();
                    System.out.println("Selected text: " + textPane.getSelectedText());
                    textPane.replaceSelection(textPane.getSelectedText().toLowerCase());
                    textPane.setSelectionStart(startPosition);
                    textPane.setSelectionEnd(endPosition);
                    textPane.getCaret().setSelectionVisible(true);
                    lowerCaseFlag = true;
                }
                lowerAndUpperCaseBtn.setFocusable(false);
                textPane.requestFocus();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (ae.getSource() == upperCaseMI) {
            boolean upperCaseFlag = false;
            int startPosition = 0;
            int endPosition = 0;
            try {
                if ((textPane.getSelectionStart() != textPane.getSelectionEnd()) && (!upperCaseFlag)) {
                    startPosition = textPane.getSelectionStart();
                    endPosition = textPane.getSelectionEnd();
                    System.out.println("Selected text: " + textPane.getSelectedText());
                    textPane.replaceSelection(textPane.getSelectedText().toUpperCase());
                    textPane.setSelectionStart(startPosition);
                    textPane.setSelectionEnd(endPosition);
                    textPane.getCaret().setSelectionVisible(true);
                    upperCaseFlag = true;
                }
                lowerAndUpperCaseBtn.setFocusable(false);
                textPane.requestFocus();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } 
    }
}

1 个答案:

答案 0 :(得分:1)

textPane.replaceSelection替换所选内容时,将删除所选文本,然后使用当前输入样式添加新文本。

要实现所需的功能,您需要使用文档提供的方法setCharacterAttributes更新所选内容的字符属性。