JavaFX中菜单控件的常见 *** 作

JavaFX中菜单控件的常见 *** 作,第1张


import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.InputStream;
import java.util.ArrayList;

/**菜单栏的使用
 * @ClassName MenuTest
 * @Description TODO
 * @Author Mr_X
 * @Date 2022/4/25 16:36
 * @Version 1.0
 *
 */
public class MenuTest extends Application {
    private ToggleGroup tg = new ToggleGroup();
    private PropertyListener pl = new PropertyListener(tg);
    private MenuItemsFactory mf = new MenuItemsFactory(tg);

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        MenuBar menuBar = new MenuBar();



        pl.propertyListen();

        for (Menu menu : mf.getMenus()) {

            menuBar.getMenus().add(menu);
        }

        menuBar.setPrefWidth(900);

        anchorPane.getChildren().add(menuBar);




        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle(this.getClass().getSimpleName());
        primaryStage.setHeight(600);
        primaryStage.setWidth(900);
        
        primaryStage.show();

        anchorPane.widthProperty().addListener((observable, oldValue, newValue) -> menuBar.setPrefWidth(newValue.doubleValue()));
    }
}

/**
 * 处理事件最好放在一个另一个类中,避免代码臃肿。
 */
class PropertyListener{
    private ToggleGroup tg;
    private ObservableList toggles;

    public PropertyListener() {
    }

    public PropertyListener(ToggleGroup tg) {
        this.tg = tg;
        toggles = tg.getToggles();
    }

    public void propertyListen(){
        tg.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
            FilteredList filtered = toggles.filtered(e -> e.isSelected());
            for (Toggle toggle : filtered) {
                RadioMenuItem rd = (RadioMenuItem) toggle;
                System.out.println(rd.getText()+"选中!");
            }
        });
    }
}

/**
 * 返回带有MenuItems选项的List
 */
class MenuItemsFactory{
    private ArrayList menus = new ArrayList<>();
    private ToggleGroup tg;
    public MenuItemsFactory(ToggleGroup tg) {
        this.tg = tg;
        Menu menu0 = new Menu("颜色");
        Menu menu1 = new Menu("粗细");
        Menu menu2 = new Menu("图形");
        Menu menu3 = new Menu("快捷工具");
        Menu menu4 = new Menu("单选按钮菜单");
        Menu menu5 = new Menu("复选按钮菜单");


        MenuItem colorR = new MenuItem("红色");
        ImageView redView = new ImageView("resource/img_1.png");
        redView.setFitHeight(10);
        redView.setFitWidth(10);
        colorR.setGraphic(redView);

        MenuItem colorY = new MenuItem("黄色");
        colorY.setStyle(
                "-fx-background-color:#EFE030FF;"
        );
        MenuItem colorB = new MenuItem("蓝色");
        colorB.setStyle(
                "-fx-background-color:#538edc;"
        );
        menu0.getItems().addAll(colorR,colorB,colorY);

        //###################################################################################################

        MenuItem borderT = new MenuItem("粗");
        MenuItem borderM = new MenuItem("中");
        MenuItem borderS = new MenuItem("细");
        menu1.getItems().addAll(borderS,borderM,borderT);
        //###################################################################################################


        MenuItem spaceD = new MenuItem("默认");
        MenuItem spaceR = new MenuItem("矩形");
        MenuItem spaceE = new MenuItem("椭圆");
        MenuItem spaceL = new MenuItem("直线");

        SeparatorMenuItem separator0 = new SeparatorMenuItem();
        SeparatorMenuItem separator1 = new SeparatorMenuItem();

        menu2.getItems().addAll(spaceD,separator0,spaceR,separator1,spaceE,spaceL);
        //###################################################################################################

        MenuItem manageC = new MenuItem("清空");
        MenuItem manageE = new MenuItem("擦除");

        Menu manageElse = new Menu("其他");
        manageElse.getItems().addAll(new MenuItem("About"),new MenuItem("Welcome"));

        manageC.setAccelerator(KeyCombination.valueOf("ctrl+alt+c"));
        manageC.setOnAction(e-> System.out.println("清空成功!"));
        menu3.getItems().addAll(manageC,manageE,manageElse);
        //###################################################################################################


        RadioMenuItem red = new RadioMenuItem("RED");
        RadioMenuItem yellow = new RadioMenuItem("YELLOW");
        RadioMenuItem blue = new RadioMenuItem("BLUE");
        red.setToggleGroup(tg);
        yellow.setToggleGroup(tg);
        blue.setToggleGroup(tg);
        menu4.getItems().addAll(red,yellow,blue);

        //###################################################################################################


        CheckMenuItem checkMenuItem = new CheckMenuItem("Option_1");
        CheckMenuItem checkMenuItem1 = new CheckMenuItem("Option_2");
        CheckMenuItem checkMenuItem2 = new CheckMenuItem("Option_3");
        menu5.getItems().addAll(checkMenuItem,checkMenuItem1,checkMenuItem2);
        //###################################################################################################

        menus.add(menu0);
        menus.add(menu1);
        menus.add(menu2);
        menus.add(menu3);
        menus.add(menu4);
        menus.add(menu5);


    }


    public MenuItemsFactory(ArrayList menus,ToggleGroup tg) {
        this.tg = tg;
        this.menus = menus;
    }


    public ArrayList getMenus() {
        return menus;
    }

    public void setMenus(ArrayList menus) {

        this.menus = menus;
    }

    public ToggleGroup getTg() {
        return tg;
    }

    public void setTg(ToggleGroup tg) {
        this.tg = tg;
    }
}

效果:

javafxUI

 代码涉及Lambda表达式、流 *** 作,可能需要一定的Java基础才能理解个别代码段。

推荐的JavaFX学习视频课程:JavaFX视频教程

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/787038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存