本应直截了当的事情很快就走下坡路。
到目前为止,我已经在stackOverflow上尝试了所有与onAction相关的问题,对任何错别字都进行了两次和三次检查,甚至跌至与Reddit的/ r / javahelp上的搜索一样低,但是可惜我屈服了。
我和一个朋友正在开发一个网络文本编辑器,作为概念/宠物项目的证明,尽管我努力将自己的头包裹在网络的一面,但我尝试通过添加皮肤来提供帮助-较深的部分,例如麻烦的设置屏幕。我要做的就是为用户提供一个对话框,允许他们更改主题,然后将其加载到新的CSS工作表中。
我有以下文件: SettingsTools.java
public class SettingsTools {
String ret = null;
Stage stage;
Parent root;
Scene scene;
SettingsController sc = new SettingsController();
public void changeTheme() {
stage = new Stage();
root = ApplicationTools.loadFromFXML("/fxml/settings.fxml", sc);
scene = new Scene(root, 350, 350);
stage.setScene(scene);
stage.setTitle("Settings");
stage.showAndWait();
}
public String showSettings() {
changeTheme();
return sc.getData();
}
}
settings.fxml
<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" >
<columnConstraints>
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
</rowConstraints>
<children>
<RadioButton id="light" selected="true" text="Light">
<toggleGroup>
<ToggleGroup fx:id="t" />
</toggleGroup>
</RadioButton>
<RadioButton id="dark" text="Dark" toggleGroup="$t" GridPane.rowIndex="1" />
<Button text="Accept"
GridPane.columnIndex="1"
GridPane.rowIndex="2"
fx:id="accept"
onAction="#acceptButton"/>
<Button text="Cancel"
GridPane.rowIndex="2"
fx:id="cancel"
onAction="#cancelButton"/>
</children>
</GridPane>
SettingsController.java
public class SettingsController {
String ret = null;
Stage stage;
Parent root;
Scene scene;
@FXML
Button accept;
@FXML
Button cancel;
@FXML
RadioButton dark;
@FXML
RadioButton light;
@FXML
protected void acceptButon() {
if (light.isSelected()) {
ret = "light";
} else if (dark.isSelected()) {
ret = "dark";
}
Stage stage = (Stage) accept.getScene().getWindow();
stage.close();
}
@FXML
protected void cancelButton(ActionEvent event) {
ret = null;
stage.close();
}
public String getData() {
return ret;
}
}
ApplicationTools.java(仅供参考)
public static <T> T loadFromFXML(String path, Object controller) {
T root;
// Attempt to load FXML file
try {
FXMLLoader loader = new FXMLLoader();
URL location = ApplicationTools.class.getResource(path);
loader.setLocation(location);
if(controller != null) {
loader.setController(controller);
}
root = loader.load();
} catch (Exception e) {
// FXML file load failed, return error scene instead
System.err.format("Failed loading FXML file at path: '%s':\n" + e.getMessage(), path);
VBox t_root = new VBox();
t_root.getChildren().add(new Label("Failed loading FXML file at path: '" + path + "':\n" + e.getMessage()));
root = (T) t_root;
}
return root;
}
当用户选择希望查看设置屏幕时,将创建一个新的SettingsTools实例,然后在其上调用showSettings()。我目前试图实现的全部目的是根据选择的选项返回一个字符串。我知道不需要为呼叫更改主题进行整个呼叫显示设置,但是这样做是为了在需要更多设置时限制将来的更改。
由于某些原因,FXML / JavaFX不允许onAction =“ acceptButton”,但所有的Google答案都与错别字有关。