gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:16:29 25 4
gpt4 key购买 nike

这是我的一些作业。我让它一直运行,直到我添加 FX。现在它将弹出一个带有标题 Pane 和标签的窗口,但代码的实际功能并未出现在窗口中。代码在 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私有(private)Labeled TextLine;在 setOnAction 之外,就像使用 Label l 一样。

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

这是关于 Java FX 事件处理的好文章 link

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

关于java - 我的 JavaFX 项目停止在 EventHandler 上运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61375848/

25 4 0