我有ANTLR解析器和工作正常的樹語法。我希望用戶在JTextarea中輸入語法(我正在使用java swing來進行GUI開發)。一旦語法在JTextarea中,用戶應按下按鈕(運行),語法結果(操作)應顯示在另一個文本區域中。從Java Swing GUI文本區域運行ANTLR語法
爲簡單起見,可以使用最簡單的HelloWorld ANTLR語法(解析器和樹語法)來演示如何從Java Swing gui的JTextarea運行ANTLR語法。
我還沒有找到任何關於ANTLR與JavaSwing GUI或任何有關如何從Java Swing GUI的JTextArea運行ANTLR語法的內容。
任何想法/建議/簡單示例/演示?
EDIT-Solution
下面是我的GUI類中處理模擬按鈕的部分代碼。用戶在JTextarea中輸入ANTLR語法。
private void addReRunButtonListener() {
btnReRun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
//for Random Simulation
if(rdbtnRandomWithSeed.isSelected())
{
String line = "";
String nl ;
Process p = Runtime.getRuntime().exec("java org.antlr.Tool protocol.g protocolWalker.g");
p.waitFor();
p = Runtime.getRuntime().exec("javac protocolLexer.java protocolParser.java protocolWalker.java SimulatorRandom.java");
p.waitFor();
p = Runtime.getRuntime().exec("java RandomSimulator");
OutputStreamWriter stdin = new OutputStreamWriter(p.getOutputStream());
stdin.write(simAlgoEdit.getText()+"\n");
stdin.close();
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
nl = bri.readLine();
while (nl != null) {
line = line + nl + "\n";
nl = bri.readLine();
}
bri.close();
System.out.println("Done.");
simGraphicsArea.setText(line);
graphicsArea.setText(line);
}
上面的代碼也抓取輸出並將其插入到我的GUI中的另一個JTextarea中。 @trashgod:非常感謝您的幫助。
謝謝
如果你不使用Swing,你如何運行它?除了在ActionListener代碼中,您可以執行相同的操作,也可以使用SwingWorker。 –