如何在JavaFX中创建ButtonBar?
以下示例演示了ButtonBar的创建。
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonBar.ButtonData; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class ButtonBarExample extends Application { @Override public void start(Stage stage) { //创建切换按钮 ToggleButton button1 = new ToggleButton("Java"); button1.setPrefSize(60, 40); ToggleButton button2 = new ToggleButton("Python"); button2.setPrefSize(60, 40); ToggleButton button3 = new ToggleButton("C++"); button3.setPrefSize(60, 40); //将按钮添加到切换组 ToggleGroup group = new ToggleGroup(); button1.setToggleGroup(group); button2.setToggleGroup(group); button3.setToggleGroup(group); //创建一个ButtonBar- ButtonBar buttonBar = new ButtonBar(); //将按钮添加到按钮栏 ButtonBar.setButtonData(button1, ButtonData.APPLY); ButtonBar.setButtonData(button2, ButtonData.APPLY); ButtonBar.setButtonData(button3, ButtonData.APPLY); buttonBar.getButtons().addAll(button1, button2, button3); //将切换按钮添加到窗格中 HBox box = new HBox(5); box.setPadding(new Insets(50, 50, 50, 150)); box.getChildren().addAll(buttonBar); box.setStyle("-fx-background-color: BEIGE"); //设置舞台 Scene scene = new Scene(box, 595, 150); stage.setTitle("Button Bar"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果