Java Swing 文本区域 : How to simultaneously print Transliterated text/s in an Output TextArea?

标签 java swing jtextarea text-editor transliteration

我正在尝试编写一个文本编辑器。我想要两个 TextAreas:第一个用于在 unicode 脚本中输入/编辑,第二个用于同时打印我自己设置的相应罗马脚本(ASCII)中的 unicode 脚本输入的输出基于音译方案。 当我调用音译方法时,我无法更新并同时打印输出文本区域中的输入文本。当我将输出文本区域设置为同时打印时,我感觉到有点问题,但我无法找出到底是什么问题。

编辑器类-------->



import java.awt.*;
import java.beans.PropertyChangeSupport;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class editor extends JPanel {
    private static final int ROWS = 10;
    private static final int COLS = 50;
    private static final String[] BUTTON_NAMES = {"विविधाः", "द्वाराणि", "Setting", "Parse", "Compile"};
    private static final int GAP = 3;
    private JTextArea inputTextArea = new JTextArea(ROWS, COLS);
    private JTextArea outputTextArea = new JTextArea(ROWS, COLS);
    private JTextArea TA = new JTextArea(ROWS, COLS);

//calling the transliteration method
    transliterator tr = new transliterator();
    Document asciiDocument=tr.DevanagariTransliteration(inputTextArea.getDocument());

    public editor() throws BadLocationException {
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
        for (String btnName : BUTTON_NAMES) {
            buttonPanel.add(new JButton(btnName));
        }

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(buttonPanel);

        add(putInTitledScrollPane(inputTextArea, "देवनागरी <<<<<<>>>>>> Devanagari"));

        inputTextArea.setFocusable(true);
        outputTextArea.setFocusable(false);
        outputTextArea.setEditable(false);

        //String inputCheck = inputTextArea.getText();
        //inputTextArea.setText("x");
        //if (inputTextArea.getText().length()>0) {


       outputTextArea.setDocument(asciiDocument);//printing input in 2nd textarea
        // outputTextArea.setDocument(inputTextArea.getDocument());//printing input in 2nd textarea


        add(putInTitledScrollPane(outputTextArea, "IndicASCII"));

    }

    private JPanel putInTitledScrollPane(JComponent component,
                                         String title) {
        JPanel wrapperPanel = new JPanel(new BorderLayout());
        wrapperPanel.setBorder(BorderFactory.createTitledBorder(title));
        wrapperPanel.add(new JScrollPane(component));
        return wrapperPanel;
    }

    private static void createAndShowGui() throws BadLocationException {
        editor mainPanel = new editor();

        JFrame frame = new JFrame("Unicode Editor");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        ImageIcon imgicon = new ImageIcon("MyIcon.jpg");
        frame.setIconImage(imgicon.getImage());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowGui();
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

以及音译类方法:


import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class transliterator {
    //method that returns a Document type
    // the method - importing devanagari text through the type 'Document' from input textArea by call
    public Document DevanagariTransliteration(Document devanagariTextDocument) throws BadLocationException {
        //extracting the devanagari text from the imported Document type
        String asciiText = devanagariTextDocument.getText(0, devanagariTextDocument.getLength());
        //devanagari unicode a replaced by ascii a
        String transliteratedText = asciiText.replace('\u0905', 'a');

        JTextArea jt = new JTextArea();
        //inserting the TRANSLITERATED text to a textArea to extract as a Document again
        jt.setText(transliteratedText);
        //extracting and creating as a document
        Document ASCIITextDocument = jt.getDocument();
        //returning the document
        return ASCIITextDocument;
    }
}

最佳答案

要将一个文档中的更改反射(reflect)到另一个文档中,您可以按照 camickr 的建议使用 DocumentListener .
以下是mre演示了在处理“输入”JTextArea 中的更改后更新“输出”JTextArea
用于演示目的的处理只是将输入转换为大写。这应该根据您的具体需求进行更改:

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class MyWindow extends JPanel {

    private static final int ROWS = 10, COLS = 50;

    private final JTextArea outputTextArea;

    public MyWindow() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        JTextArea inputTextArea = new JTextArea(ROWS, COLS);
        inputTextArea.getDocument().addDocumentListener(new TransliterateDocumentListener());
        add(putInTitledScrollPane(inputTextArea,"Input"));

        outputTextArea = new JTextArea(ROWS, COLS);
        outputTextArea.setFocusable(false);
        outputTextArea.setEditable(false);

        add(putInTitledScrollPane(outputTextArea, "Output"));
    }


    private JPanel putInTitledScrollPane(JComponent component, String title) {
        JPanel wrapperPanel = new JPanel(new BorderLayout());
        wrapperPanel.setBorder(BorderFactory.createTitledBorder(title));
        wrapperPanel.add(new JScrollPane(component));
        return wrapperPanel;
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Document Listener Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add( new MyWindow());
        frame.pack();
        frame.setVisible(true);
    }


    private void insert(String text, int from) {
        text = process(text);
         try {
            outputTextArea.getDocument().insertString(from, text, null);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

    private void remove(int from, int length) {
         try {
            outputTextArea.getDocument().remove(from, length);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

    private String process(String text) {
        //todo process text as needed
        //returns upper case text for demo
        return text.toUpperCase();
    }

    class TransliterateDocumentListener implements DocumentListener {

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document doc = e.getDocument();
            int from = e.getOffset(), length = e.getLength();
            try {
                insert(doc.getText(from, length), from);
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            remove(e.getOffset(), e.getLength());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
             //Plain text components don't fire these events.
        }
    }

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

关于Java Swing 文本区域 : How to simultaneously print Transliterated text/s in an Output TextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454413/

相关文章:

Java.lang.NoClassDefFoundError : org/apache/poi/ss/usermodel/Font 错误

java - FlowLayout 的顶部对齐

java - 如何在java中循环显示分割字符串中的元素?

java - 正确的逻辑从牌组中随机选择一张牌,直到所有牌都被选中

java - 消息部分 MyClass 未被识别。 (它存在于服务 WSDL 中吗?)

java - 运行方法两次

java - JTable 打印,顶部带有前缀文本(不是页眉/页脚)

java - 为什么 JTextArea 没有出现在这段代码中?

java - 将控制台数据写入文件并在新框架上显示相同的文件

java - 创建分段进度条的最佳方法?