Question: JavaFX I was trying to use the Event Handler, when you press the button it would say Thank you in three languages. I've been trying
JavaFX
I was trying to use the Event Handler, when you press the button it would say "Thank you" in three languages. I've been trying to modify my codes but something seems missing or else wrong. Need help and advice how to add in the buttons so that they can work.
Here is the code I have so far.
@Override
public void start(Stage primaryStage) {
try {
primaryStage.setTitle("Say Thank You");
// BorderPane Layout and HBox layout
BorderPane rt = new BorderPane();
HBox hbox = addHBox();
rt.setBottom(hbox);
// Create Label
Label label = addLabel();
rt.setCenter(label);
// Font of the Label
label.setFont(Font.font("Cambria", 20));
label.setTextFill(Color.BLACK);
// Hbox
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #B22222;");
hbox.setAlignment(Pos.BASELINE_CENTER);
hbox.getChildren().addAll(addButton(), addButton1(), addButton2());
Button button = addButton();
Button button1 = addButton1();
Button button2 = addButton2();
addButton1().setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
label.setText("Thank you");
}
});
Scene scene = new Scene(rt, 400, 200);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public Label addLabel() {
Label label = new Label();
label.setText(" ");
BorderPane.setAlignment(addHBox(), Pos.CENTER);
return label;
}
// HBox Layout and Buttons
public HBox addHBox() {
HBox hbox = new HBox();
return hbox;
}
public Button addButton() {
Button buttonEnglish = new Button("English");
return buttonEnglish;
}
public Button addButton1() {
Button buttonSpanish = new Button("Spanish");
return buttonSpanish;
}
public Button addButton2() {
Button buttonHmong = new Button("Hmong");
return buttonHmong;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
