1

所以我有一个JTextPane基本上用作控制台的。我在 a 的中心场和南场的JFramea有它。JTextFieldJTextField获取它拥有的文本,并JTextPane在用户按下回车时将其添加到。为了使JTextPane用户无法编辑,我不得不这样做setFocusable(false),因为使用setEditable(false)阻止了任何文本出现在JTextPane. 但是虽然我不希望用户编辑窗格,但我仍然希望用户能够突出显示窗格中的文本,但我似乎找不到这样做的方法。

下面是一个示例,说明我的意思

样本

package resources;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class SampeTextPane extends JFrame
{
    public SampeTextPane()
    {
        setPreferredSize(new Dimension(350, 200));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JTextPane display = new JTextPane();
        display.setBorder(new EmptyBorder(5, 5, 5, 5));
        display.setMargin(new Insets(5, 5, 5, 5));
        display.setFocusable(false);
        appendToPane(display, "Example", Color.BLUE);

        JTextField field = new JTextField();
        field.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                appendToPane(display, field.getText(), Color.BLACK);
                field.setText("");
            }
        });

        add(display, BorderLayout.CENTER);
        add(field, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    private void appendToPane(JTextPane pane, String text, Color color)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);

        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = pane.getDocument().getLength();
        pane.setCaretPosition(len);
        pane.setCharacterAttributes(aset, false);
        pane.replaceSelection(text + "\n");
    }

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

提前感谢您的帮助。

4

2 回答 2

3

使用 setEditable(false) 阻止任何文本出现在 JTextPane 上。

您可以将其JTextPane设为不可编辑,但无法通过文本窗格更新文本。

相反,您可以通过以下方式更新文本Document

//int len = pane.getDocument().getLength();
//pane.setCaretPosition(len);
//pane.setCharacterAttributes(aset, false);
//pane.replaceSelection(text + "\n");

try
{
    StyledDocument doc = pane.getStyledDocument();
    doc.insertString(doc.getLength(), text, aset);
}
catch(BadLocationException e) { System.out.println(e); }
于 2016-09-30T00:22:42.067 回答
3

另一种选择,虽然更复杂,是使用布尔标志来允许或禁止对文档的更改,这里建议使用 StanislavL 。

在您的情况下,它可能类似于:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.DocumentFilter;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

@SuppressWarnings("serial")
public class SampeTextPane extends JFrame {
    private boolean isApi = false;

    public SampeTextPane() {
        setPreferredSize(new Dimension(350, 200));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JTextPane display = new JTextPane();

        ((DefaultStyledDocument) display.getDocument()).setDocumentFilter(new DocFilter());

        display.setBorder(new EmptyBorder(5, 5, 5, 5));
        display.setMargin(new Insets(5, 5, 5, 5));
        // !! display.setFocusable(false);
        appendToPane(display, "Example", Color.BLUE);

        JTextField field = new JTextField();
        field.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                appendToPane(display, field.getText(), Color.BLACK);
                field.setText("");
            }
        });

        add(display, BorderLayout.CENTER);
        add(field, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    private void appendToPane(JTextPane pane, String text, Color color) {
        isApi = true;

        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
                color);

        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
        int len = pane.getDocument().getLength();

        String selection = pane.getSelectedText();
        if (selection == null) {
            pane.setCaretPosition(len);
            text += "\n";
        }        
        pane.setCharacterAttributes(aset, false);
        pane.replaceSelection(text);

        isApi = false;
    }

    private class DocFilter extends DocumentFilter {
        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
                throws BadLocationException {
            if (isApi) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            if (isApi) {
                super.remove(fb, offset, length);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException {
            if (isApi) {
                super.replace(fb, offset, length, text, attrs);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new SampeTextPane());
    }

}
于 2016-09-30T00:40:01.720 回答