一个菜单是呈现给用户的选项或命令的列表,通常菜单中包含的执行某些操作的项目。菜单的内容称为菜单项,菜单栏包含多个菜单。
通常,按钮在用户界面应用程序中进行控制,单击该按钮将执行相应的操作。
甲SplitMenuButton提供两个按钮和一个菜单的功能。它分为两个区域-操作区域和菜单区域。单击这些区域之一时,将显示相应的功能。
您可以通过实例化javafx.scene.control.SplitMenuButton类来创建拆分菜单按钮。
下面的示例演示了SplitMenuButton的创建。
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.MenuItem; import javafx.scene.control.SplitMenuButton; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class SplitMenuButtonExample extends Application { public void start(Stage stage) { //Creating an ImageView ImageView img = new ImageView("UIControls/globe.png"); img.setFitWidth(20); img.setFitHeight(20); //Creating a label Label label = new Label("Select A Language"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); //Creating a menu SplitMenuButton menu = new SplitMenuButton(); //Setting text to the SplitMenuButton menu.setText("Language"); //Setting an image to the button menu.setGraphic(img); //Creating menu Items menu.setMnemonicParsing(true); MenuItem item1 = new MenuItem("Telugu"); MenuItem item2 = new MenuItem("Hindi"); MenuItem item3 = new MenuItem("English"); MenuItem item4 = new MenuItem("Tamil"); MenuItem item5 = new MenuItem("Malayalam"); //Adding all the menu items to the menu menu.getItems().addAll(item1, item2, item3, item4, item5); //Adding the choice box to the scene HBox layout = new HBox(25); layout.getChildren().addAll(label, menu); layout.setPadding(new Insets(15, 50, 50, 130)); layout.setStyle("-fx-background-color: BEIGE"); //Setting the stage Scene scene = new Scene(layout, 595, 200); stage.setTitle("Split Menu Button"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果