-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuBar_1.java
93 lines (75 loc) · 2.37 KB
/
MenuBar_1.java
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
81
82
83
84
85
86
87
88
89
90
91
92
// Java program to create a menu bar and add
// menu to it and also add menuitems to menu
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
import java.time.LocalDate;
import java.util.ListIterator;
import java.util.List;
import java.util.Arrays;
public class MenuBar_1 extends Application {
// launch the application
public void start(Stage s)
{
// set title for the stage
s.setTitle("creating MenuBar");
List <String> properties = Arrays.asList (
"os.name", "os.arch", "os.version",
"java.vendor", "java.version", "javafx.version"
);
ListIterator <String> listIterator = properties.listIterator ();
while (listIterator.hasNext ()){
String property = listIterator.next ();
listIterator.set (property + ": " + System.getProperty(property));
}
Label labelVersion = new Label (String.join (";\n", properties) + ".");
// create a menu
Menu m = new Menu("Menu");
MenuItem [] menuItems = {
new MenuItem("menu_item_1"),
new MenuItem("menu-item 2"),
new MenuItem("menu-item_3"),
new SeparatorMenuItem(),
new MenuItem("_"),
new MenuItem("__"),
new MenuItem("_menu item 7"),
new MenuItem("__menu item 8"),
new MenuItem("_id_menu item 9")
};
Label labelSelected = new Label("\nItem selected: none");
// create events for menu items
// action event
EventHandler<ActionEvent> event = e -> labelSelected.setText("\nItem selected: " +((MenuItem)e.getSource()).getText());
for (MenuItem menuItem: menuItems){
menuItem.setMnemonicParsing (false); // to avoid underscore problem
m.getItems().add(menuItem);
menuItem.setOnAction (event);
}
m.getItems().forEach (menuItem ->{
System.out.println (menuItem.getText());
});
// create a menubar
MenuBar mb = new MenuBar();
// add menu to menubar
mb.getMenus().add(m);
// create a VBox
VBox vb = new VBox(mb, labelVersion, labelSelected);
vb.setAlignment(javafx.geometry.Pos.TOP_CENTER);
// create a scene
Scene sc = new Scene(vb, 512, 342);
// set the scene
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}