-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonApp.java
More file actions
80 lines (71 loc) · 2.36 KB
/
ButtonApp.java
File metadata and controls
80 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package module3.gui.buttonapp;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
* ButtonApp
*/
public class ButtonApp extends Application {
public static String[] buttonNames = {
"Button #1",
"Button #2",
"Button #3",
"Button #4",
"Button #5",
"Button #6"
};
public static String[] messages = {
"Blam! You pressed %s!",
"Wowie! %s was pressed!",
"Bazinga! You pressed the heck out of %s!",
"Wowie! You just pressed %s!",
"Outstanding! That was %s!",
"Kapow! Nice work on pressing %s!",
"Whoa mama! Did you just press %s?",
"Hachi machi, you pressed %s.",
"Press %s again. See what happens.",
"Huh. Somebody just pushed %s.",
"何!? %s が押されました!"
};
private TextField textField;
private Pane buttonList;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox root = new VBox();
primaryStage.setScene(new Scene(root, 400, 100));
// Create display window
textField = new TextField("Welcome! Press any button.");
textField.editableProperty().set(false);
root.getChildren().add(textField);
// Create panel of Buttons
buttonList = new FlowPane(5,5);
for (String btnName : buttonNames) {
// Create Button
Button btn = new Button(btnName);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
handleButtonClick(btnName);
}});
buttonList.getChildren().add(btn);
}
root.getChildren().add(buttonList);
primaryStage.show();
}
/** */
public void handleButtonClick(String buttonName) {
int index = (int)(Math.random() * messages.length);
String format = messages[index];
String message = String.format(format, buttonName); // Insert button name ("You pressed Button #3")
showText(message); // Display message
}
public void showText(String message) {
textField.setText(message);
}
}