标题窗格只是带有标题的窗格。它包含一个或多个用户界面元素,例如按钮,标签等。您可以展开和折叠它。
您可以通过实例化javafx.scene.control.TitledPane类在JavaFX中创建标题窗格。创建后,您可以使用setText()方法向窗格添加标题,并可以使用setContent()方法向其中添加内容。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TitledPane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class TitledPaneExample extends Application { @Override public void start(Stage stage) { //创建一个按钮 Button button = new Button(); //设置按钮的标题 button.setText("Click Here"); //创建TitlePane- TitledPane pane = new TitledPane(); pane.setLayoutX(200); pane.setLayoutY(75); pane.setText("Sample Titled Pane"); //将内容设置到标题窗格 pane.setContent(button); //设置舞台 Group root = new Group(pane); Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Titled Pane Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TitledPane; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class TiledPane2 extends Application { @Override public void start(Stage stage) { //创建切换按钮 ToggleButton button1 = new ToggleButton("Java"); ToggleButton button2 = new ToggleButton("Python"); ToggleButton button3 = new ToggleButton("C++"); //切换按钮组 ToggleGroup group = new ToggleGroup(); button1.setToggleGroup(group); button2.setToggleGroup(group); button3.setToggleGroup(group); //将切换按钮添加到窗格中 VBox box = new VBox(); box.getChildren().addAll(button1, button2, button3); //创建TitlePane- TitledPane pane = new TitledPane(); pane.setLayoutX(10); pane.setLayoutY(10); pane.setText("OpenCV Examples"); //将切换按钮添加到窗格中 pane.setContent(box); //设置舞台 Scene scene = new Scene(pane, 595, 150, Color.BEIGE); stage.setTitle("Titled Pane Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果