JavaFX使用TextField、TextArea实现单行文本、多行文本(查找与排序功能)

目录

 

一、环境

二、模拟

三、代码


一、环境

jdk8

二、模拟

三、代码

package xyz.hashdog.class32_33_34;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;


/**
 * @author th
 * @description: TODO
 * @projectName hashdog
 * @date 2020/2/1620:48
 */
public class Launch extends Application {
    int index=0;
    String substr="";

    public static void main(String[] args) {
        Application.launch(Launch.class,args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane an = new AnchorPane();
        TextField tf =new TextField();
        //过滤器现在用户输入
        tf.setTextFormatter(new TextFormatter<String>(new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change change) {
                System.out.println(change.getText());
                //只能输入a-z
                if(change.getText().matches("[a-z]")){
                    return change;
                }
                return null;
            }
        }));
        //文本框
        TextArea ta = new TextArea();
        ta.setText("初始值");
        //大小
        ta.setFont(Font.font(16));
        //允许自动换行
        ta.setWrapText(true);
        //初始化设置行数
        ta.setPrefRowCount(10);
        //设置宽高
        ta.setPrefWidth(200);
        ta.setPrefHeight(200);
        //删除
//        ta.deleteText(0,2);



        TextField find_tf =new TextField();
        Button find_bu = new Button("查找");
        Button sort_bu = new Button("排序");

        HBox hbox = new HBox(10);
        hbox.getChildren().addAll(find_tf,find_bu,sort_bu);
        TextArea find_ta = new TextArea();
        VBox vbox = new VBox(10);
        vbox.getChildren().addAll(hbox,find_ta);



        AnchorPane.setTopAnchor(ta,60.0);
        AnchorPane.setLeftAnchor(vbox,250.0);
        an.getChildren().addAll(tf,ta,vbox);
        an.setStyle("-fx-background-color: deepskyblue");
        Scene s= new Scene(an);
        primaryStage.setScene(s);
        primaryStage.setTitle("hashdog");
        primaryStage.setWidth(1000);
        primaryStage.setHeight(600);
        //设置窗口不可拉伸
        primaryStage.setResizable(false);
        primaryStage.show();


        //查找按钮点击事件
        find_bu.setOnAction(item->{
            String find_value = find_tf.getText();
            //获取段落
            find_ta.getParagraphs().forEach(new Consumer<CharSequence>() {
                @Override
                public void accept(CharSequence t) {
                    String value = t.toString();
                    String find_value = find_tf.getText();
                    substr = value.substring(index);
                    if(substr.contains(find_value)){
                        //获取包含的第一个
                       int i = substr.indexOf(find_value);
                       int tem = i+index;

                        //获取包含的最后一个累加
                       index = tem+find_value.length();
                       find_ta.selectRange(tem,index);
                       return;
                    }
                    index = 0;
                    substr=value;
                }

            });
        });

        //排序按钮点击事件
        sort_bu.setOnAction(e->{
            char[] chars = find_ta.getText().toCharArray();
            List<String> list = new ArrayList<String>();
            for (char c : chars) {
                list.add(String.valueOf(c));
            }
            find_ta.clear();
            list.stream().sorted((item1,item2)->item1.compareTo(item2)).forEach(item->find_ta.appendText(item));
        });






    }

}

发布了133 篇原创文章 · 获赞 32 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/corleone_4ever/article/details/104395303