将样式化的文本粘贴到JTextPane总是将文本粘贴在JTextPane的末尾

时间:2018-11-17 07:10:16

标签: java swing jtextpane styleddocument

我正在使用带有RTFEditorKit的JTextPane在Java swing中创建一个简单的记事本。 我面临2个问题。

我要从外部应用程序(如写字板或MS Office Word)粘贴一些样式文本,如下所示: enter image description here

我将光标置于第一行的末尾>按Enter>,然后使用Ctrl + V键再次粘贴相同样式的文本。 问题1:现在内容没有粘贴到第一行下方,而是粘贴到了最后一行之后,如下所示:

enter image description here

问题2:如果我从我的应用程序中复制此样式化的内容并将其粘贴到任何其他外部应用程序(如WordPad或MS Office Word)中,它将显示为纯文本。

我为什么要面对这些问题?谁能帮忙。

我的代码:

import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
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 DefaultStyledDocument defaultStyleDoc;
    public JScrollPane scrollPane;
    public JMenuBar menuBar;
    public JMenu fileMenu;
    public JMenu editMenu;
    public JMenu formatMenu;
    public JMenuItem newSubMenu;
    public JMenuItem openSubMenu;
    public JMenuItem save;
    public JMenuItem saveAs;
    public JMenuItem cut;
    public JMenuItem copy;
    public JMenuItem paste;
    public JMenuItem selectAll;
    public JMenuItem bold;
    public JMenuItem italic;
    public JMenuItem underline;
    public JMenuItem exit;
    public JFileChooser fc;
    public boolean openFileExtFlag = true;
    public boolean saveFileExtFlag = true;
    public File openFile;
    public File saveFile;
    public boolean saveWindowTitle = false;
    public boolean openFileFlag;
    public boolean saveFileFlag;
    public boolean saved = true;
    public boolean dontSaveOption;
    public BufferedReader br;
    public boolean saveForNewOpenExitListener;
    public boolean saveAsFlag;
    public int returnVal;
    public String filePath;
    public boolean flagForOpenListener;
    public StyledDocument styledDocument;
    public Style defaultStyle;

    public MyNotepadMini() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        frame = new JFrame("My Notepad");
        panel = new JPanel(new BorderLayout());      

        styledDocument = new DefaultStyledDocument();
        rtf = new RTFEditorKit();
        textPane = new JTextPane(styledDocument);
        textPane.setEditorKit(rtf);
        styleContext = new StyleContext();

        scrollPane = new JScrollPane();
        scrollPane.getViewport().add(textPane);
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        newSubMenu = new JMenuItem("New");
        newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
        openSubMenu = new JMenuItem("Open...");
        openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
        save = new JMenuItem("Save");
        save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
        editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        cut = new JMenuItem("Cut");
        cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
        copy = new JMenuItem("Copy");
        copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
        paste = new JMenuItem("Paste");
        paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
        selectAll = new JMenuItem("Select All");
        selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
        exit = new JMenuItem("Exit");
        exit.setMnemonic(KeyEvent.VK_X);
        formatMenu = new JMenu("Format");
        formatMenu.setMnemonic(KeyEvent.VK_F);
        bold = new JMenuItem(new StyledEditorKit.BoldAction());
        bold.setText("Bold");
        bold.setMnemonic(KeyEvent.VK_B);
        bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
        italic = new JMenuItem(new StyledEditorKit.ItalicAction());
        italic.setText("Italic");
        italic.setMnemonic(KeyEvent.VK_I);
        italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
        underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
        underline.setText("Underline");
        underline.setMnemonic(KeyEvent.VK_U);
        underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));

        defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        selectAll.addActionListener(this);
        exit.addActionListener(this);
        newSubMenu.addActionListener(this);

        editMenu.addMenuListener(new MenuListener() {
            public void menuSelected(MenuEvent event) {
                if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
                    cut.setEnabled(false);
                    copy.setEnabled(false);
                } else {
                    cut.setEnabled(true);
                    copy.setEnabled(true);
                }
            }

            public void menuDeselected(MenuEvent event) {
            }

            public void menuCanceled(MenuEvent event) {  
            }
        });

        scrollPane.setPreferredSize(new Dimension(700,500));
        fileMenu.add(newSubMenu);
        fileMenu.add(openSubMenu);
        fileMenu.add(save);
        fileMenu.add(exit);
        editMenu.add(cut);
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(selectAll);
        formatMenu.add(bold);
        formatMenu.add(italic);
        formatMenu.add(underline);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(formatMenu);

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

        frame.setJMenuBar(menuBar);
        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() == cut)) {
            textPane.cut();
            textPane.requestFocus();
        } else if ((ae.getSource() == copy)) {
            textPane.copy();
            textPane.requestFocus();
        } else if ((ae.getSource() == paste)) {
            textPane.paste();
            textPane.requestFocus();
        } else if (ae.getSource() == selectAll) {
            textPane.selectAll();
        } else if ((ae.getSource() == exit)){   
            System.exit(0);
        } else if ((ae.getSource() == newSubMenu)) {                        
            textPane.setText("");
            textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
            MutableAttributeSet mas = textPane.getInputAttributes();
            mas.removeAttributes(mas);
            textPane.requestFocus();
        } 
    }
}

0 个答案:

没有答案