JavaFx菜单

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class App15_13 extends Application {

    private TextArea textArea = new TextArea("我喜欢用JavaFx编程");

    @Override
    public void start(Stage primaryStage) {

        BorderPane rootBP = new BorderPane();
        rootBP.setCenter(textArea);

        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu("菜单");
        Menu menuEdit = new Menu("编辑");
        Menu menuView = new Menu("查看");

        menuFile.getItems().addAll(new MenuItem("文件"));

        menuBar.getMenus().addAll(menuFile, menuEdit, menuView);

        rootBP.setTop(menuBar);


        RadioMenuItem rMI = new RadioMenuItem("红色");// 创建单选菜单项
        RadioMenuItem gMI = new RadioMenuItem("绿色");
        RadioMenuItem bMI = new RadioMenuItem("蓝色");

        rMI.setOnAction(e -> textArea.setStyle("-fx-text-fill: red"));
        gMI.setOnAction(e -> textArea.setStyle("-fx-text-fill: green"));
        bMI.setOnAction(e -> textArea.setStyle("-fx-text-fill: blue"));

        // 创建单选菜单组
        ToggleGroup toggleGroup = new ToggleGroup();
        rMI.setToggleGroup(toggleGroup);
        gMI.setToggleGroup(toggleGroup);
        bMI.setToggleGroup(toggleGroup);

        MenuItem newMenu = new MenuItem("哈哈");
        MenuItem newMenu1 = new MenuItem();


        //创建弹出菜单
        ContextMenu contextMenu = new ContextMenu();
        contextMenu.getItems().addAll(rMI, bMI, new SeparatorMenuItem(), gMI);


        // 建立文本区组件与弹出菜单关联
        textArea.setContextMenu(contextMenu);

        Scene scene = new Scene(rootBP, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("编出菜单应用程序");
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
posted @ 2021-02-16 10:39  xl4ng  阅读(136)  评论(0编辑  收藏  举报