Java执行cmd等命令、JavaFX原始窗口置底

package BottomTest;
/**
 * @Author: ZhangHao
 * @Description: 置底测试
 * @Date: 2020/4/1 15:07
 * @Version: 1.0
 */

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;

public class BottomTest extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        HBox hBox = new HBox(5);
        hBox.getChildren().add(getJavaFXDialogButton());
        hBox.getChildren().add(getCMDDialogButton());
        Scene scene = new Scene(hBox,300,300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * 备选:
     * JavaFX能使窗口置底
     * Runtime的环境变量配置
     * cmd 弹出的窗口置顶
     * cmd 工具.exe 弹出的窗口置顶
     * cmd 程序.bat 弹出的窗口置底
     * @return JavaFX弹框Button
     */
    private static Button getCMDDialogButton(){
        Button button = new Button("CMD弹框");
        button.setOnAction(e -> {
            // Runtime相关:https://www.cnblogs.com/mingforyou/p/3551199.html
            // Runtime和cmd命令相关:https://www.iteye.com/blog/liyixing1-2442586
            Runtime runtime = Runtime.getRuntime();
            try {
                // cmd /c 命令 执行命令后关闭原窗口
                // cmd /k 命令 执行命令后关闭原窗口
                // cmd /c start 命令 打开一个新窗口后执行命令,关闭原窗口
                // cmd /k start 命令 打开一个新窗口后执行命令,不关闭原窗口
                // cmd.exe /c start http://www.baidu.com 和 cmd /c start http://www.baidu.com 等效
                runtime.exec("cmd /c start http://www.baidu.com");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });
        return button;
    }

    private static Button getJavaFXDialogButton(){
        Button button = new Button("JavaFX弹框");
        button.setOnAction(e -> {
            Stage stage = new Stage();
            Pane pane = new Pane();
            Scene scene = new Scene(pane,300,100);
            stage.setScene(scene);
            stage.setAlwaysOnTop(true);
            stage.show();
        });
        return button;
    }
}
发布了388 篇原创文章 · 获赞 105 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/haoranhaoshi/article/details/105273075