【问题标题】:javafx putting textarea value into a hashmapjavafx将textarea值放入哈希图中
【发布时间】:2017-08-30 17:28:37
【问题描述】:

我想制作一个小型 java fx 应用程序,它在舞台上只有 textarea 和一个按钮,当您在 textarea 中键入一些字符串并按下提交时,它会在舞台小桌子上显示每个单词出现的次数. 所以我的问题是:即使我不知道查找事件的关键是什么以及如何将文本区域中的字符串连接到地图,map 是否是查找事件的最佳解决方案。

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);
    }
}

第二个类又是带有表格视图的gui类

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();
    }
}

第三类应该是魔法发生的类:

public class Logic {

    public void logic()

    }
}

【问题讨论】:

    标签: javafx hashmap textarea


    【解决方案1】:

    你可以做类似的事情

    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()));
    }
    

    检查 Javadocs 以了解每个步骤的作用:PatternCollectors.groupingBy()Function 等。

    如果要不区分大小写,可以将Function.identity()替换为String::toLowerCase

    .collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));
    

    如果你想忽略标点符号,你可以添加

    map(s -> s.replaceAll("[^a-zA-Z]",""))
    

    到管道。

    【讨论】:

    • 谢谢你,你真的帮了我,这有可能以某种方式放在tableview中吗?
    • @Pera 嗯,是的,只需用地图中的数据填充表格视图。
    • 我设法将所有结果放在一行中,我希望一行一个单词和出现次数/对不起所有这些,但我正在尝试完成 tjis,我我不是程序员,我只知道 db 和 sql。提前谢谢
    • 你如何将这个应用到我的例子中,我没有找到任何与我的例子几乎相同的东西。抱歉中断
    • @Pera 你有数据,把它放在表里。
    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多