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】可獲取本章的源碼。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章