Java实战之管家婆记账系统(22)——实现修改软件主题皮肤功能

本节概要

本节实现修改界面皮肤的功能,即使整个程序应用不同的CSS样式。

 

实现功能

关于软件不同的皮肤要能保存起来,即在软件关闭后再次重启也能显示改变的皮肤,因此需要将其保存在电脑本地,所以在properties包下创建一个名为styles的properties文件,该文件用来保存用户选择的皮肤。

可以看到主题皮肤有三种选择:默认、经典黑和优雅白。用户可以选择不同的菜单按钮来切换主题。

那么首先要将用户选中的主题保存下来,所以,”默认“、”经典黑“和”优雅白“菜单项的事件方法如下:

    /**
     * “默认”菜单项的事件监听器方法
     *
     * @param event 事件
     */
    public void defaultRadioMenuItemEvent(ActionEvent event) throws IOException {
        File file = new File("src\\AccountSystem\\properties\\styles.properties");
        if (!file.exists()) {
            file.createNewFile();
        }
        // 实例化Properties对象
        Properties properties = new Properties();
        // 写入默认属性
        properties.setProperty("default", "");
        // 文件输出流
        FileOutputStream fos = new FileOutputStream(file);
        // 向properties文件中写入信息,有两个参数:第一个参数是文件输出流,第二个参数是properties文件注释
        properties.store(fos, "默认");
        SimpleTools.informationDialog(Alert.AlertType.INFORMATION,"信息","信息","切换默认皮肤成功,请重启软件使用新皮肤。");
        // 关闭流
        fos.close();
    }
​
    /**
     * “经典黑”菜单项的事件监听器方法
     *
     * @param event 事件
     */
    public void blackRadioMenuItemEvent(ActionEvent event) throws IOException {
        File file = new File("src\\AccountSystem\\properties\\styles.properties");
        if (!file.exists()) {
            file.createNewFile();
        }
        // 实例化Properties对象
        Properties properties = new Properties();
        // 写入经典黑属性
        properties.setProperty("black", "styles/BlackStyle.css");
        // 文件输出流
        FileOutputStream fos = new FileOutputStream(file);
        // 向properties文件中写入信息,有两个参数:第一个参数是文件输出流,第二个参数是properties文件注释
        properties.store(fos, "经典黑");
        SimpleTools.informationDialog(Alert.AlertType.INFORMATION,"信息","信息","切换经典黑皮肤成功,请重启软件使用新皮肤。");
        // 关闭流
        fos.close();
    }
​
    /**
     * “优雅白”菜单项的事件监听器方法
     *
     * @param event 事件
     */
    public void whiteRadioMenuItemEvent(ActionEvent event) throws IOException {
        File file = new File("src\\AccountSystem\\properties\\styles.properties");
        if (!file.exists()) {
            file.createNewFile();
        }
        // 实例化Properties对象
        Properties properties = new Properties();
        // 写入优雅白属性
        properties.setProperty("white", "styles/WhiteStyle.css");
        // 文件输出流
        FileOutputStream fos = new FileOutputStream(file);
        // 向properties文件中写入信息,有两个参数:第一个参数是文件输出流,第二个参数是properties文件注释
        properties.store(fos, "优雅白");
        SimpleTools.informationDialog(Alert.AlertType.INFORMATION,"信息","信息","切换优雅白皮肤成功,请重启软件使用新皮肤。");
        // 关闭流
        fos.close();
    }

当点击不同的主题菜单后,将用户选中的主题保存到properties文件中,比如当切换成”经典黑“主题后,properties文件中就会保存为黑色主题的CSS样式文件路径。

而”优雅白“保存的是白色主题WhiteStyle.css的路径,”默认“保存的路径为空。

同时要考虑的还有当用户再次打开软件后,怎么清楚自己的当前软件是哪一种主题的,所以有如下方法来初始化显示当前主题皮肤:

    /**
     * 操作结果:初始主题皮肤菜单项单选框被选中情况
     */
    public void initThemeRadioMenuItem() {
        String key = "";
        try {
            Properties properties = new Properties();
            FileInputStream fis = new FileInputStream(new File("src\\AccountSystem\\properties\\styles.properties"));
            properties.load(fis);
            Iterator<String> iterator = properties.stringPropertyNames().iterator();
            while (iterator.hasNext()) {
                key = iterator.next();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 判断properties文件key的值
        if (key.equals("black")) {
            blackRadioMenuItem.setSelected(true);
        } else if (key.equals("white")) {
            whiteRadioMenuItem.setSelected(true);
        } else {
            defaultRadioMenuItem.setSelected(true);
        }
    }

并在initialize()方法中调用该方法:

但是现在运行项目,切换的主题没有加载到软件界面上,只是properties文件的内容发生了变化而已。

接下来就是加载应用JavaFX的CSS样式。

所以在MainApp.java中创建下面方法获取styles.properties文件中的值,即当前主题。

    /**
     * 获取styles.properties文件中的值
     *
     * @return 返回值
     * @throws IOException 抛出IOException
     */
    private String getStyleValue() throws IOException {
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream(new File("src\\AccountSystem\\properties\\styles.properties"));
        properties.load(fis);
        Iterator<String> iterator = properties.stringPropertyNames().iterator();
        String key = "";
        while (iterator.hasNext()) {
            key = iterator.next();
        }
        return properties.getProperty(key, "");
    }

然后在每个init方法中都添加下面这行代码来加载CSS样式文件:

// 加载CSS样式文件        

scene.getStylesheets().add(MainApp.class.getResource(getStyleValue()).toExternalForm());

0?wx_fmt=pnguploading.4e448015.gif转存失败重新上传取消

现在再次运行程序,就可以切换主题了,可能主题皮肤并不是那么好看,因为没有写完所有的样式代码,只写了部分组件的。

经典黑样式:

”优雅白“样式:

”默认“样式:

最后发现还是系统默认的配色好看些,自己写的着实丑了些。

 

可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。

注意:在公众号后台回复【20200425】可获取本章的源码。

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值