我尝试使用由Scene Builder创建的按钮切换场景。
这是我的Controller类
public class Controller implements Initializable {
@FXML Button LoginButton;
@FXML private void handleButtonAction(ActionEvent event) throws IOException {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
还有FXML
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
<MenuBar VBox.vgrow="NEVER" />
<AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
<children>
<AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
<children>
<TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
<effect>
<InnerShadow />
</effect></TextField>
<PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
<effect>
<InnerShadow blurType="TWO_PASS_BOX" />
</effect></PasswordField>
<Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
<AnchorPane />
</children>
</VBox>
出于某种原因,IntelliJ显示错误无法解决&#39; handleButtonAction&#39;
onAction="#handleButtonAction"
答案 0 :(得分:2)
您可以使用属于它的任何FXML元素检索场景(和舞台)。您只需anyElement.getScene().getWindow()
public class Controller {
@FXML
Button loginButton;
@FXML
private void handleButtonAction() {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
// Instead of retrieving the stage by the event's source, you can do it by one of your FXML component.
Stage window = (Stage) loginButton.getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
public void initialize() {
//initialize the field you want here.
}
}
和FXML:
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
<!-- Fabian was right, you forgot this children. -->
<children>
<MenuBar VBox.vgrow="NEVER" />
<AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
<children>
<AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
<children>
<TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
<effect>
<InnerShadow />
</effect>
</TextField>
<PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
<effect>
<InnerShadow blurType="TWO_PASS_BOX" />
</effect>
</PasswordField>
<Button fx:id="loginButton" layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
<AnchorPane />
</children>
</VBox>
对您的代码进行一些解释/评论。
initialize
方法现已足够(查看note in interface definition)。
fx:id
。你忘记了吗?我在FXML的Button
元素中添加了它。 (我不知道它是否实际上是loginButton ......)
答案 1 :(得分:0)
在控制器中写下以下代码
@FXML
protected void handleButtonAction() {
System.out.println("In handleButtonAction");
// your logic
}
和 FXML文件
<Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
我希望它能运作......我使用了场景构建器 FXML ,如果没有作品发送给我错误截图