• 第十四周


    实现简单的记事本:
    public class FXMLDocumentController implements Initializable {
    private Stage stage;
    private File fileOpened;
    private TextArea textArea;
    public void initialize(URL url, ResourceBundle rb) {
    }

    void setStage(Stage stage) {
        this.stage = stage;
        openFile(null);
    }
    private void openFile(File file) {
        fileOpened = file;
        if (fileOpened == null) {
            stage.setTitle("CodePad");
        } else {
            stage.setTitle(fileOpened.getAbsolutePath());
        }
    }
    
    private void readFile(File file) {
        if (file == null) {
            textArea.setText("");
            return;
        }
        try {
            textArea.setText(new String(Files.readAllBytes(Paths.get(file.getAbsolutePath()))));
        } catch (IOException e) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("file open error: " + e.getMessage());
            alert.show();
        }
    }
    private void saveFileAs(File file) {
        try {
            Files.write(Paths.get(file.getAbsolutePath()), textArea.getText().getBytes());
        } catch (IOException e) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("file write error: " + e.getMessage());
            alert.show();
        }
        openFile(file);
    }
    private void onNotImplementedItemClick(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        String text = ((MenuItem) event.getTarget()).getText();
        alert.setContentText(text + " not implemented");
        alert.show();
    }
    private void onFileNewClick(ActionEvent event) {
        openFile(null);
        readFile(null);
    }
    private void onFileOpenClick(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open File");
        File file = fileChooser.showOpenDialog(stage);
        if (file == null) {
            // 选择文件被取消才会是null,
            return;
        }
        openFile(file);
        readFile(file);
    }
    private void onFileSaveClick(ActionEvent event) {
        if (fileOpened == null) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("no file opened");
            alert.show();
            return;
        }
        saveFileAs(fileOpened);
    }
    private void onFileSaveAsClick(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Save File As");
        saveFileAs(fileChooser.showSaveDialog(stage));
    }
    private void onFindClick() {
        Stage stage = new Stage();
        stage.setTitle("查找替换");
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));
        // 添加查找框
        Label findText = new Label("查找:");
        grid.add(findText, 0, 1);
        TextField findTextField = new TextField();
        grid.add(findTextField, 1, 1);
        // 添加替换框
        Label replaceText = new Label("替换:");
        grid.add(replaceText, 0, 2);
        TextField replaceTextField = new TextField();
        grid.add(replaceTextField, 1, 2);
        // 添加按钮
        Button btn1 = new Button("查找");
        HBox hbBtn1 = new HBox(10);
        hbBtn1.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn1.getChildren().add(btn1);
        grid.add(hbBtn1, 0, 4);
        // 给查找按钮添加方法
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                // 获取文本框中所有内容
                String text = textArea.getText();
                // 获取查找框的文本内容
                String findWords =  findTextField.getText();
                // 不为空,则进行查找
                if(findWords != null && !findWords.isEmpty()) {
                    // 查找出内容,高亮显示
                    Text t = new Text(findWords);
                    // 设置字体颜色为红色
                    t.setFill(Color.RED);
                    text = text.replaceAll(findWords, t.toString());
                    // 清空原来的内容
                    textArea.clear();
                    textArea.appendText(text);
                } else {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setContentText("请输入内容!");
                    alert.show();
                }
            }
        });
        Button btn2 = new Button("全部替换");
        HBox hbBtn2 = new HBox(10);
        hbBtn2.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn2.getChildren().add(btn2);
        grid.add(hbBtn2, 1, 4);
        // 给替换按钮添加方法
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                // 获取文本框中所有内容
                String text = textArea.getText();
                // 获取查找框的文本内容
                String findWords =  findTextField.getText();
                // 获取替换框的文本内容
                String replaceWords = replaceTextField.getText();
                // 不为空,则进行替换
                if(findWords != null && !findWords.isEmpty() && replaceWords != null && !replaceWords.isEmpty()) {
                    System.out.println("text1 = " + text);
                    // 进行替换
                    text = text.replaceAll(findWords, replaceWords);
                    System.out.println("text2 = " + text);
                    // 清空原来的内容
                    textArea.clear();
                    textArea.appendText(text);
                } else {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setContentText("请输入内容!");
                    alert.show();
                }
            }
        });
        Scene scene = new Scene(grid, 300, 275);
        stage.setScene(scene);
        stage.show();
    }
    private void onFileQuitClick(ActionEvent event) {
        Platform.exit();
    }
    

    }

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。是Java访问数据库的标准规范,JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。

    原理:
      Java提供访问数据库规范称为JDBC,而生产厂商提供规范的实现类称为驱动。
      JDBC是接口,驱动是接口的实现类,没有驱动将无法完成数据库连接,从而不能操作数据库!每个数据库厂商都需要提供自己的驱动,用来连接自己公司的数据库,也就是说驱动一般都由数据库生成厂商提供。

  • 相关阅读:
    Metabase研究 开源的数据报表
    Redis配置不当致使root被提权漏洞
    一个程序员被骗去养猪
    调度器简介,以及Linux的调度策略
    Linux的内存分页管理
    在地铁11号线上写书
    为什么说“概率”带来一场现代革命?
    快速学习Bash
    用树莓派玩转蓝牙
    树莓派的GPIO编程
  • 原文地址:https://www.cnblogs.com/ludada007/p/11958537.html
Copyright © 2020-2023  润新知