Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据输入的文本向JtextPane添加规则_Java_Swing_Document_Jtextpane - Fatal编程技术网

Java 如何根据输入的文本向JtextPane添加规则

Java 如何根据输入的文本向JtextPane添加规则,java,swing,document,jtextpane,Java,Swing,Document,Jtextpane,我希望在JtextPane中有一个规则,例如 如果包含“-”(破折号)字符,则加粗,否则不加粗 我可以在documentFilter中添加一些规则吗? 在哪里可以添加此规则hmmm,是教程,可以这样做,例如: import java.awt.*; import java.util.ArrayList; import java.util.List; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*

我希望在JtextPane中有一个规则,例如 如果包含“-”(破折号)字符,则加粗,否则不加粗

我可以在documentFilter中添加一些规则吗? 在哪里可以添加此规则

hmmm,是教程,可以这样做,例如:

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class TextAreaTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea textArea;

    public TextAreaTest() {
        textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(60, 32));
        textArea.setOpaque(true);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {

            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                string = string.replaceAll("-", "");
                super.insertString(fb, offset, string, attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                text = text.replaceAll("-", "");
                //TODO must do something here
                super.replace(fb, offset, length, text, attrs);
            }
        });

        textArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update(e);
            }

            private void update(DocumentEvent e) {
                List<String> lines = getLines(textArea);
                String lastLine = lines.get(lines.size() - 1);
                int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
                        lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
                int lineHeight = getLineHeight(textArea);
                if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
                    System.out.println("Too big! Should refuse the update!");
                }
            }
        });

        getContentPane().add(textArea);
    }

    private static List<String> getLines(JTextArea textArea) {
        int lineHeight = getLineHeight(textArea);
        List<String> list = new ArrayList<String>();
        for (int num = 0;; num++) {
            int i = textArea.viewToModel(new Point(0, num * lineHeight));
            int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
            if (i == 0 && j == 0) {
                continue;
            }
            if (textArea.getDocument().getLength() == i && i == j) {
                break;
            }
            String s = removeTrailingNewLine(textArea.getText().substring(i, j));
            list.add(s);
            //System.out.println(i + " " + j + " = " + s);
        }
        return list;
    }

    private static int getLineHeight(JTextArea textArea) {
        return textArea.getFontMetrics(textArea.getFont()).getHeight();
    }

    private static String removeTrailingNewLine(String s) {
        if (s.endsWith("\n")) {
            return s.substring(0, s.length() - 1);
        } else {
            return s;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TextAreaTest test = new TextAreaTest();
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.pack();
                test.setVisible(true);
            }
        });
    }
}
import java.awt.*;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.*;
导入javax.swing.event.*;
导入javax.swing.text.*;
公共类TextAreaTest扩展了JFrame{
私有静态最终长serialVersionUID=1L;
私人JTEXTEXTAREA textArea;
公共文本区域测试(){
textArea=新的JTextArea();
setPreferredSize(新维度(60,32));
textArea.set不透明(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
((AbstractDocument)textArea.getDocument()).setDocumentFilter(新DocumentFilter()){
@凌驾
public void insertString(FilterBypass fb,int offset,String String,AttributeSet attr)引发BadLocationException{
string=string.replaceAll(“-”,“”);
super.insertString(fb,offset,string,attr);
}
@凌驾
public void replace(FilterBypass fb、int offset、int length、字符串文本、AttributeSet attrs)引发BadLocationException{
text=text.replaceAll(“-”和“”);
//托多必须在这里做点什么
super.replace(fb、偏移量、长度、文本、属性);
}
});
textArea.getDocument().addDocumentListener(新DocumentListener()){
@凌驾
公共作废更改日期(记录事件e){
更新(e);
}
@凌驾
公共作废插入更新(文档事件e){
更新(e);
}
@凌驾
公共作废移除更新(文档事件e){
更新(e);
}
私有作废更新(文档事件e){
列表行=获取行(文本区域);
字符串lastLine=lines.get(lines.size()-1);
int tabbedTextWidth=Utilities.getTabbedTextWidth(新段)(
lastLine.tocharray(),0,lastLine.length()),textArea.getFontMetrics(textArea.getFont()),0,null,0);
int lineHeight=getLineHeight(textArea);
if(lines.size()*lineHeight>textArea.getHeight()|| tabbedTextWidth>textArea.getWidth()){
System.out.println(“太大了!应该拒绝更新!”);
}
}
});
getContentPane().add(文本区域);
}
私有静态列表getLines(JTextArea textArea){
int lineHeight=getLineHeight(textArea);
列表=新的ArrayList();
for(int num=0;num++){
inti=textArea.viewToModel(新点(0,num*lineHeight));
int j=textArea.viewToModel(新点(0,(num+1)*线宽));
如果(i==0&&j==0){
持续
}
if(textArea.getDocument().getLength()==i&&i==j){
打破
}
字符串s=removeTrailingNewLine(textArea.getText().substring(i,j));
列表。添加(s);
//系统输出打印项次(i+“”+j+“”=“”+s);
}
退货清单;
}
私有静态int getLineHeight(JTextArea textArea){
返回textArea.getFontMetrics(textArea.getFont()).getHeight();
}
私有静态字符串删除RailingNewline(字符串s){
如果(s.endsWith(“\n”)){
返回s.substring(0,s.length()-1);
}否则{
返回s;
}
}
公共静态void main(字符串[]args){
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
text区域测试=新的text区域测试();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.pack();
test.setVisible(真);
}
});
}
}
hmmm,是教程,可以这样做,例如:

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class TextAreaTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea textArea;

    public TextAreaTest() {
        textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(60, 32));
        textArea.setOpaque(true);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {

            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                string = string.replaceAll("-", "");
                super.insertString(fb, offset, string, attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                text = text.replaceAll("-", "");
                //TODO must do something here
                super.replace(fb, offset, length, text, attrs);
            }
        });

        textArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update(e);
            }

            private void update(DocumentEvent e) {
                List<String> lines = getLines(textArea);
                String lastLine = lines.get(lines.size() - 1);
                int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
                        lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
                int lineHeight = getLineHeight(textArea);
                if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
                    System.out.println("Too big! Should refuse the update!");
                }
            }
        });

        getContentPane().add(textArea);
    }

    private static List<String> getLines(JTextArea textArea) {
        int lineHeight = getLineHeight(textArea);
        List<String> list = new ArrayList<String>();
        for (int num = 0;; num++) {
            int i = textArea.viewToModel(new Point(0, num * lineHeight));
            int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
            if (i == 0 && j == 0) {
                continue;
            }
            if (textArea.getDocument().getLength() == i && i == j) {
                break;
            }
            String s = removeTrailingNewLine(textArea.getText().substring(i, j));
            list.add(s);
            //System.out.println(i + " " + j + " = " + s);
        }
        return list;
    }

    private static int getLineHeight(JTextArea textArea) {
        return textArea.getFontMetrics(textArea.getFont()).getHeight();
    }

    private static String removeTrailingNewLine(String s) {
        if (s.endsWith("\n")) {
            return s.substring(0, s.length() - 1);
        } else {
            return s;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TextAreaTest test = new TextAreaTest();
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.pack();
                test.setVisible(true);
            }
        });
    }
}
import java.awt.*;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.*;
导入javax.swing.event.*;
导入javax.swing.text.*;
公共类TextAreaTest扩展了JFrame{
私有静态最终长serialVersionUID=1L;
私人JTEXTEXTAREA textArea;
公共文本区域测试(){
textArea=新的JTextArea();
setPreferredSize(新维度(60,32));
textArea.set不透明(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
((AbstractDocument)textArea.getDocument()).setDocumentFilter(新DocumentFilter()){
@凌驾
public void insertString(FilterBypass fb,int offset,String String,AttributeSet attr)引发BadLocationException{
string=string.replaceAll(“-”,“”);
super.insertString(fb,offset,string,attr);
}
@凌驾
public void replace(FilterBypass fb、int offset、int length、字符串文本、AttributeSet attrs)引发BadLocationException{
text=text.replaceAll(“-”和“”);
//托多必须在这里做点什么
super.replace(fb、偏移量、长度、文本、属性);
}
});
textArea.getDocument().addDocumentListener(新DocumentListener()){
@凌驾
公共作废更改日期(记录事件e){
更新(e);
}
@凌驾
公共作废插入更新(文档事件e){
更新(e);
}
@凌驾
公共作废移除更新(文档事件e){
更新(e);
}
私有作废更新(文档事件e){
列表行=获取行(文本区域);
字符串最后一行=