本节概要
本节实现修改界面皮肤的功能,即使整个程序应用不同的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());
现在再次运行程序,就可以切换主题了,可能主题皮肤并不是那么好看,因为没有写完所有的样式代码,只写了部分组件的。
经典黑样式:
”优雅白“样式:
”默认“样式:
最后发现还是系统默认的配色好看些,自己写的着实丑了些。
可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。
注意:在公众号后台回复【20200425】可获取本章的源码。