javafx 将 textarea 值放入哈希图中
javafx putting textarea value into a hashmap
我想制作一个小型 java fx 应用程序,它只有文本区域和舞台上的一个按钮,当您在文本区域中键入一些字符串并按下提交时,它会在舞台上显示小 table 结果是每个单词出现的次数。
所以我的问题是:地图是否是查找事件的最佳解决方案,即使我不知道查找事件的关键是什么以及如何将字符串从文本区域连接到地图。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Word counting");
TextArea txt=new TextArea();
txt.setMaxSize(450, 200);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.hide();
ShowResults.drugiProzor();
}
});
BorderPane root = new BorderPane();
root.setTop(txt);
HBox hbox=new HBox();
hbox.setPadding(new Insets(20,20,100,180));
hbox.getChildren().add(btn);
root.setBottom(hbox);
Scene scene = new Scene(root, 450, 300);
primaryStage.setTitle("Word counting!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
第二个 class 又是 gui class 和 table view
public class ShowResults {
static Stage secondaryStage;
public static void drugiProzor() {
secondaryStage=new Stage();
TableView table=new TableView();
TableColumn column1=new TableColumn("Word");
column1.setMinWidth(200);
TableColumn column2=new TableColumn("Number of occurencies");
column2.setMinWidth(200);
table.getColumns().addAll(column1,column2);
StackPane pane=new StackPane();
pane.getChildren().add(table);
Scene scene = new Scene(pane, 450, 300);
secondaryStage.setScene(scene);
secondaryStage.setTitle("Counting words");
secondaryStage.show();
}
}
和第三个 class 应该是 class 魔法发生的地方是这样的:
public class Logic {
public void logic()
}
}
你可以做类似的事情
public Map<String, Long> countWordOccurences(String text) {
return Pattern.compile("\s+") // regular expression matching 1 or more whitespace
.splitAsStream(text) // split at regular expression and stream words between
// group by the words themselves and count each group:
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
查看 Javadoc 以查看每个步骤的作用:Pattern
, Collectors.groupingBy()
, Function
,等等
如果想不区分大小写的计数,可以将Function.identity()
换成String::toLowerCase
.collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));
如果你想忽略标点符号,你可以添加
map(s -> s.replaceAll("[^a-zA-Z]",""))
到管道。
我想制作一个小型 java fx 应用程序,它只有文本区域和舞台上的一个按钮,当您在文本区域中键入一些字符串并按下提交时,它会在舞台上显示小 table 结果是每个单词出现的次数。 所以我的问题是:地图是否是查找事件的最佳解决方案,即使我不知道查找事件的关键是什么以及如何将字符串从文本区域连接到地图。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Word counting");
TextArea txt=new TextArea();
txt.setMaxSize(450, 200);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.hide();
ShowResults.drugiProzor();
}
});
BorderPane root = new BorderPane();
root.setTop(txt);
HBox hbox=new HBox();
hbox.setPadding(new Insets(20,20,100,180));
hbox.getChildren().add(btn);
root.setBottom(hbox);
Scene scene = new Scene(root, 450, 300);
primaryStage.setTitle("Word counting!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
第二个 class 又是 gui class 和 table view
public class ShowResults {
static Stage secondaryStage;
public static void drugiProzor() {
secondaryStage=new Stage();
TableView table=new TableView();
TableColumn column1=new TableColumn("Word");
column1.setMinWidth(200);
TableColumn column2=new TableColumn("Number of occurencies");
column2.setMinWidth(200);
table.getColumns().addAll(column1,column2);
StackPane pane=new StackPane();
pane.getChildren().add(table);
Scene scene = new Scene(pane, 450, 300);
secondaryStage.setScene(scene);
secondaryStage.setTitle("Counting words");
secondaryStage.show();
}
}
和第三个 class 应该是 class 魔法发生的地方是这样的:
public class Logic {
public void logic()
}
}
你可以做类似的事情
public Map<String, Long> countWordOccurences(String text) {
return Pattern.compile("\s+") // regular expression matching 1 or more whitespace
.splitAsStream(text) // split at regular expression and stream words between
// group by the words themselves and count each group:
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
查看 Javadoc 以查看每个步骤的作用:Pattern
, Collectors.groupingBy()
, Function
,等等
如果想不区分大小写的计数,可以将Function.identity()
换成String::toLowerCase
.collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));
如果你想忽略标点符号,你可以添加
map(s -> s.replaceAll("[^a-zA-Z]",""))
到管道。