Button JavaFX主题切换按钮

Button JavaFX主题切换按钮,button,javafx,model-view-controller,switch-statement,themes,Button,Javafx,Model View Controller,Switch Statement,Themes,我目前正在使用JavaFXMVC创建游戏PacMan。我使用SceneBuilder创建了一个FXML文件 我还添加了两个主题:经典主题和圣诞节主题。我现在想做的事情是使用在FXML文件中创建的按钮切换主题(参见图片:buttons'classic'和'xmas' 请帮帮我 这就是它的样子: 这是我当前的代码: FXMLSideMenucontroller.fxml package侧菜单; 导入com.jfoenix.controls.JFXButton; 导入de.jensd.fx.gly

我目前正在使用JavaFXMVC创建游戏PacMan。我使用SceneBuilder创建了一个FXML文件

我还添加了两个主题:经典主题和圣诞节主题。我现在想做的事情是使用在FXML文件中创建的按钮切换主题(参见图片:buttons'classic'和'xmas'

请帮帮我

这就是它的样子:

这是我当前的代码: FXMLSideMenucontroller.fxml

package侧菜单;
导入com.jfoenix.controls.JFXButton;
导入de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.ancorpane;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.VBox;
公共类FXMLSideMenuController实现可初始化{
@FXML
私人安可邦ap_main;
@FXML
私人安可邦ap_游戏场;
@FXML
私人窗格pnl_高分;
@FXML
专用窗格pnl_main;
@FXML
私人窗格pnl_Moeilijkhide;
@FXML
私人VBox VBox_Moeilijkhide;
@FXML
私人JFXButton btn_easy;
@FXML
专用JFXButton btn_介质;
@FXML
私人JFXButton btn_硬;
@FXML
私人版pnl_学分;
@FXML
私人窗格pnl_Spellers;
@FXML
私人JFXButton btn\u MRERTS;
@FXML
私有图像视图视图;
@FXML
私人JFXButton btn_mrlemmens;
@FXML
私人ImageView iview_mrlemmons;
@FXML
私人窗格pnl_thema;
@FXML
私人VBox VBox_thema;
@FXML
私人JFXButton btn_classic;
@FXML
私人JFXButton btn_圣诞节;
@FXML
私人主播ap_侧菜单;
@FXML
私人锚烷ap_侧窗格;
@FXML
私人JFXButton btn_Speler;
@FXML
私人JFXButton btn_Moeilijkhide;
@FXML
私人JFXButton btn_thema;
@FXML
私人JFXButton btn_高分;
@FXML
私人JFXButton btn_学分;
@FXML
私人FontAwesomeIconView iview_spelers;
@FXML
私人FontAwesomeIconView iview_niveau;
@FXML
私人FontAwesomeIconView-iview_thema;
@FXML
private FontAwesomeIconView iview_highscore;
@FXML
私人FontAwesomeIconView iview_学分;
@FXML
私有无效把手按钮操作(ActionEvent事件){
if(event.getSource()==btn\u spellers){
pnl_spelers.toFront();
}否则
if(event.getSource()==btn_moeilijkhide){
pnl_moeilijkhide.toFront();
}否则
if(event.getSource()==btn_thema){
pnl_thema.toFront();
}否则
if(event.getSource()==btn_highscore){
pnl_highscore.toFront();
}否则
if(event.getSource()==btn\u学分){
pnl_学分。toFront();
}
}
/**
@FXML
私有无效handleThemeSwitcher(ActionEvent事件){
if(event.getSource()==btn_classic){
scene.getStylesheets().remove(getClass().getResource(“Resources/style.css”).toExternalForm());
scene.getStylesheets().remove(getClass().getResource(“Resources/xmas.css”).toExternalForm());
scene.getStylesheets().add(getClass().getResource(“Resources/style.css”).toExternalForm();//加载VO或自定义样式表
}
if(event.getSource()==btn_xmas){
scene.getStylesheets().remove(getClass().getResource(“Resources/style.css”).toExternalForm());
scene.getStylesheets().remove(getClass().getResource(“Resources/xmas.css”).toExternalForm());
scene.getStylesheets().add(getClass().getResource(“Resources/xmas.css”).toExternalForm();//加载VO或自定义样式表
} 
}   */
/**@FXML
私有无效关闭菜单项(ActionEvent事件){
if(event.getSource()==图标\u关闭){
pnl_main.toFront();
}
}*/
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
pnl_main.toFront();
}

}
您可以这样做:

首先,为两个主题创建URL:

private String urlTheme1 = getClass().getResource("css1.css").toExternalForm();
private String urlTheme2 = getClass().getResource("css2.css").toExternalForm();
单击“更改样式”按钮时,您可以执行以下操作:

  changeStylebtn.setOnAction(new EventHandler<ActionEvent>() {
                 @Override
                 public void handle(ActionEvent event) {
//Remove from scene the theme1(asumming you added to your scene when your app starts)
                     scene.getStylesheets().remove(urlTheme1); 

                     //Add the new theme
                     scene.getStylesheets().add(urlTheme2); 
                 }
            }
changeStylebtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
//从场景中删除主题1(当应用程序启动时,将添加到场景中)
scene.getStylesheets().remove(urlTheme1);
//添加新主题
scene.getStylesheets().add(urlTheme2);
}
}

这就可以了。

已经读过了,但仍然不知道如何..谢谢!所以我将所有这些行添加到我的控制器?@RingtoneFusion是的,如果你在单击“更改样式”按钮时触发了一个方法,你可以在那里执行“删除”和“添加”行。仍然不起作用。我将你发送给我的所有行添加到控制器中,但是它仍然不会更改主题。@RingtoneFusion请使用更新的代码编辑您的问题以帮助您。@RingtoneFusion您只需要在控制器中引用场景或窗格并调用(主应用程序中的场景或“根”变量)添加或删除样式。在当前控制器中,控制器中没有场景变量。