【发布时间】:2019-12-23 11:18:28
【问题描述】:
我正在尝试编写一个文本编辑器。我想要两个 TextArea: 第一个 用于在 unicode 脚本中输入/编辑,第二个 用于同时打印我自己设置的相应罗马脚本 (ASCII) 中的 unicode 脚本输入的输出基于音译方案。 在调用音译方法时,我无法更新并同时打印输出文本区域中的输入文本。当我将输出 textArea 设置为同时打印时,我能感觉到有一点问题,但我无法找出究竟是什么。
编辑器类------->
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;
}
}
【问题讨论】:
-
你忘了问问题。
-
我上面的问题是如何翻译输入文本并同时在输出 textArea 中打印翻译后的文本。当我使用 setDocuement 方法时,我无法处理文本输入。然后只打印我在输入 textArea 中键入的内容。
-
请edit 这个问题并补充一下,这是它的重要组成部分。无论如何,当然您需要编写一个音译方法,该方法接受一个文档并输出一个音译文档,您将在其他文本区域中设置它。如果您尝试编写这样的方法,请将其添加到问题中并解释您卡在哪里。
-
点击
JButton时是否要音译输入? -
@c0der 否。我想在输入文本区域的同时打印音译文本。
标签: java swing jtextarea text-editor transliteration