Java Swing TextArea:如何在输出TextArea中同时打印音译文本? [英] Java Swing TextArea: How to simultaneously print Transliterated text/s in an Output TextArea?
问题描述
我正在尝试编写文本编辑器.我想要两个TextAreas:第一个,用于在unicode脚本中键入/编辑,以及第二个,用于同时打印由我自己设置的相应罗马脚本(ASCII)输入的unicode脚本的输出.在音译方案的基础上. 调用音译方法时,我无法更新并同时在输出文本区域中打印输入文本.将输出textArea设置为同时打印时,我可以感觉到有些问题,但是我无法确定到底是什么.
I am trying to write a text editor. I want two TextAreas: 1st one for typing/editing in unicode script and 2nd one for simultaneously printing output of the unicode script input in corresponding Roman Script (ASCII) set by myself on the basis of a transliteration scheme. I am not able to update and simultaneously print the input text in output textarea while I call the transliteration method. I can sense a little bit that there is something wrong while I set the output textArea to print simultaneously but I am unable to find out what exactly is that.
编辑器类------->
The Editor class------->
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;
}
}
推荐答案
要将一个文档中的更改反映到另一文档中,可以使用 mre ,它演示了在处理输入" JTextArea
中的更改之后更新输出" JTextArea
.
用于演示目的的处理是将输入简单地转换为大写形式.应该将其更改为您的特定需求:
To reflect changes in one document onto another document you can use a DocumentListener
as suggested by camickr.
The following is an mre that demonstrates updating the "output" JTextArea
after processing the changes in the "input" JTextArea
.
The processing used for demonstration purposes is a simply converting the input to upper case. This should be changed to your specific needs :
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 TextArea:如何在输出TextArea中同时打印音译文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!