我的JavaFX项目停止在EventHandler上运行代码



这是我的一些家庭作业。我一直在运行它,直到我添加了FX。现在它会弹出一个带有标题窗格和标签的窗口,但代码的实际功能并没有出现在窗口中。代码在EventHandler上停止运行,我不明白为什么或如何修复它。它是一个文本分析器,用于统计单词的出现次数并显示带有计数的单词。如果有人能指出代码错误的位置和原因以及如何修复,我将不胜感激

package application;
/**
* <h1>Word Occurrences</h1>
* The program counts the number of a times words
* appears in the text.
* 
* 
* @author 
* @version 3.0
* @since 4/1/2020
*/
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.scene.text.Text;
import javafx.scene.text.*;

public class TextAnalyzer extends Application {
/**
* Override method to produce the output     * 
* @param s first parameter in the start method 
* @return Output
*/
//public TextAnalyzer() {    }
@Override
public void start(Stage s) {
// title for the stage
s.setTitle("Text Analyzer");
// create a tile pane
TilePane title = new TilePane();
// create a label
Label l = new Label("Word          :          Count");

// Read in the file
URLReader obj = new URLReader();
// run
EventHandler event = new EventHandler<ActionEvent>() {
private Labeled TextLine;
public void handle(ActionEvent e) {
// variables
String inputString = null;
// Read in the file
URLReader obj = new URLReader();
// string from URLReader
try {
inputString = obj.reader();
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// word array
String[] wordsArray = inputString.split("\s+");
// mapping the array
Map<String, Integer> map = new HashMap<>();
String[] var10 = wordsArray;
int var9 = wordsArray.length;
// count words
//for (String word : wordsArray) {
for(int var8 = 0; var8 < var9; ++var8) {
String word = var10[var8];
if (map.containsKey(word)) {
int count = map.get(word);
map.put(word, count + 1);
} else {
map.put(word, 1);
}
}
// output
Iterator var13 = map.entrySet().iterator();
while(var13.hasNext()) {
Entry<String, Integer> entry = (Entry)var13.next();
this.TextLine.setText((String)entry.getKey() + " : " + entry.getValue());
}
/*for (Map.Entry<String, Integer> entry : map.entrySet()) {
TextLine.setText(entry.getKey() + " : " + entry.getValue());

}*/
}
};

title.getChildren().add(l);
Scene scene = new Scene(title, 400, 400);
s.setScene(scene);
s.show();

}
/**
* This is the main method which produces the window and contents
* from the start method
* @param args
*/
public static void main(String[] args) throws Exception {
launch(args);
//CreateTable();
}
}
--------------------------------
package application;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* This is the URL reader to read the URL into a string
* for processing.
* 
* @return FileString
* @exception IOException on input error 
* @see IOException
*
*/
public class URLReader {
String fileString;
//public URLReader() {    }
public String reader() throws MalformedURLException {
// create the URL
URL file = new URL("http://shakespeare.mit.edu/macbeth/full.html");
// Open the URL stream and create readers convert to string
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(file.openStream()));
// write the output
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null)
response.append(inputLine);
// close reader
reader.close();         
fileString = response.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("error");
}
return fileString.toString();
}
}

如上所述,将操作设置为需要创建的startButton,然后设置如下操作:

startButton.setOnAction((event) -> {
System.out.println(„Start Button pressed!“);
// variables
String inputString = null;
// Read in the file
URLReader obj = new URLReader();
// string from URLReader
try {
inputString = obj.reader();
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// word array
String[] wordsArray = inputString.split("\s+");
// mapping the array
Map<String, Integer> map = new HashMap<>();
String[] var10 = wordsArray;
int var9 = wordsArray.length;
// count words
//for (String word : wordsArray) {
for(int var8 = 0; var8 < var9; ++var8) {
String word = var10[var8];
if (map.containsKey(word)) {
int count = map.get(word);
map.put(word, count + 1);
} else {
map.put(word, 1);
}
}
// output
Iterator var13 = map.entrySet().iterator();
while(var13.hasNext()) {
Entry<String, Integer> entry = (Entry)var13.next();
System.out.println((String)entry.getKey() + " : " + 
entry.getValue());
}
/*for (Map.Entry<String, Integer> entry : map.entrySet()) {
TextLine.setText(entry.getKey() + " : " + entry.getValue());}*/
});

此外,创建/定义您的Label专用Labeled TextLine;在setOnAction之外,就像使用Label l一样。

然后,如果您想更新标签TextLine的文本,请在setOnAction内部进行更新

这是一篇关于Java FX事件处理链接的好文章

此外,我建议在setOnAction中的另一个线程上运行您的东西,而不是阻塞您的UI。但这只是微调。

最新更新