If statement 如何使用HandleButtoAction和if语句使更多按钮工作
我使用if和else语句成功地实现了两个按钮。如果我添加另一条if/else语句,程序将不再工作。如何添加更多语句,以便GUI中的其他按钮正常工作?我还有大约7个按钮要编码If statement 如何使用HandleButtoAction和if语句使更多按钮工作,if-statement,button,javafx,scenebuilder,If Statement,Button,Javafx,Scenebuilder,我使用if和else语句成功地实现了两个按钮。如果我添加另一条if/else语句,程序将不再工作。如何添加更多语句,以便GUI中的其他按钮正常工作?我还有大约7个按钮要编码 package finalgui; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML;
package finalgui;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class FXMLDocumentController{
@FXML
private Button btnViewSPC;
@FXML
private Button btnBack;
@FXML
private void handleButtonAction(ActionEvent event) throws IOException{
Stage stage;
Parent root;
if(event.getSource()==btnViewSPC) {
//get reference to the button's stage
stage=(Stage) btnViewSPC.getScene().getWindow();
//load up next scene
root = FXMLLoader.load(getClass().getResource("addviewdel.fxml"));
}
else{
stage=(Stage) btnBack.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FinalGUI.fxml"));
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
当然,您可以使用多个
if
语句来取消多个按钮的语言
Object source = event.getSource();
if (source == button1) {
...
} else if (source == button2) {
...
} else if (source == button2) {
...
}
...
else {
...
}
但是就个人而言,我更喜欢使用userData
属性将数据与按钮关联:
@FXML
private void initialize() {
btnViewSPC.setUserData("addviewdel.fxml");
btnViewSPC.setUserData("FinalGUI.fxml");
...
}
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(source.getUserData().toString()));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
或者使用不同的方法处理来自不同按钮的事件。您仍然可以向控制器添加“帮助器方法”,以避免重复所有代码:
@FXML
private void handleButtonAction1(ActionEvent event) throws IOException {
showStage(event, "addviewdel.fxml");
}
@FXML
private void handleButtonAction2(ActionEvent event) throws IOException {
showStage(event, "FinalGUI.fxml");
}
...
private void showStage(ActionEvent event, String fxmlResource) throws IOException {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(fxmlResource));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
当然,您可以使用多个if
语句来取消多个按钮的语言
Object source = event.getSource();
if (source == button1) {
...
} else if (source == button2) {
...
} else if (source == button2) {
...
}
...
else {
...
}
但是就个人而言,我更喜欢使用userData
属性将数据与按钮关联:
@FXML
private void initialize() {
btnViewSPC.setUserData("addviewdel.fxml");
btnViewSPC.setUserData("FinalGUI.fxml");
...
}
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(source.getUserData().toString()));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
或者使用不同的方法处理来自不同按钮的事件。您仍然可以向控制器添加“帮助器方法”,以避免重复所有代码:
@FXML
private void handleButtonAction1(ActionEvent event) throws IOException {
showStage(event, "addviewdel.fxml");
}
@FXML
private void handleButtonAction2(ActionEvent event) throws IOException {
showStage(event, "FinalGUI.fxml");
}
...
private void showStage(ActionEvent event, String fxmlResource) throws IOException {
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(fxmlResource));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}