java桌面程序

目标之一是把打印导出的功能最终用java实现一套,首先选定javafx,因为idea默认创建工程就带的javafx,没找到swing。

创建工程,这里要选1.8,高版本jdk默认不带fx
java桌面程序_第1张图片

实现主界面的代码

package sample;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.scene.control.Button;

import java.util.concurrent.atomic.AtomicInteger;

public class Main extends Application {

    //数字
    static int num = 0;

    //场景
    Scene scene;

    //场景1
    Scene scene1;

    @Override
    public void start(Stage stage) throws Exception {
        //舞台标题
        stage.setTitle("第一个java程序");
        // 流式布局:按照控件的添加次序按个摆放,按照从上到下、从左到右的次序摆放。
        FlowPane pane = new FlowPane(10, 10);
        // 居中显示
        pane.setAlignment(Pos.CENTER);
        // 场景
        scene = new Scene(pane, 800, 600);


        // 标签
        Label label = new Label("初始值:" + num);


        // 加1按钮
        Button addButton = new Button("加1");
        addButton.setOnMouseClicked(e -> {
            num++;
            label.setText("当前值:" + num);
        });

        //减1按钮
        Button subButton = new Button("减1");
        subButton.setOnMouseClicked(e -> {
            num--;
            label.setText("当前值:" + num);
        });

        //切换到场景1
        Button btnScene1 = new Button("场景1");
        btnScene1.setOnMouseClicked(e -> {
            stage.setScene(scene1);
        });

        //弹出一个子窗口
        Button btnShowChildForm = new Button("子窗口");
        btnShowChildForm.setOnMouseClicked(e -> {
            Child.ShowChild("子窗口", "传给子窗口的参数");
        });

        pane.getChildren().addAll(addButton, subButton, btnScene1, btnShowChildForm, label);


        // 场景1
        // 流式布局:按照控件的添加次序按个摆放,按照从上到下、从左到右的次序摆放。
        FlowPane pane1 = new FlowPane(10, 10);
        // 居中显示
        pane1.setAlignment(Pos.CENTER);
        scene1 = new Scene(pane1, 200, 150);

        //返回开始的场景
        Button btnReturn = new Button("返回");
        btnReturn.setOnMouseClicked(e -> {
            stage.setScene(scene);
        });
        pane1.getChildren().addAll(btnReturn);

        //默认场景和显示
        stage.setScene(scene);
        stage.show();
    }


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

实现弹窗的代码

package sample;

import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * 显示一个子窗口
 */
public class Child {
    /**
     * 显示一个子窗口
     *
     * @param title 标题
     * @param info  信息
     */
    public static void ShowChild(String title, String info) {
        //创建舞台
        Stage stage = new Stage();
        //设置标题
        stage.setTitle(title);
        //模态
        stage.initModality(Modality.APPLICATION_MODAL);
        // 标签
        Label label = new Label(info);


        // 关闭按钮
        Button closeButton = new Button("关闭");
        closeButton.setOnMouseClicked(e -> {
            stage.close();
        });

        FlowPane pane = new FlowPane(10, 10);
        // 居中显示
        pane.setAlignment(Pos.CENTER);
        Scene scene = new Scene(pane, 500, 350);

        //添加控件
        pane.getChildren().addAll(closeButton, label);
        //设置场景和显示
        stage.setScene(scene);
        stage.show();
    }



}

测试
java桌面程序_第2张图片

java桌面程序_第3张图片

打包配置,这里没生成Exe也是奇怪
java桌面程序_第4张图片

java桌面程序_第5张图片

写CS的第一步探索就完成了

你可能感兴趣的:(java,python,开发语言)