如何使用JavaFx建立標題窗格?


標題窗格只是帶有標題的窗格。它包含一個或多個使用者介面元素,如按鈕、標籤等,你可以展開和摺疊它。

你可以透過例項化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) {
      //Creating a Button
      Button button = new Button();
      //Setting the title of the button
      button.setText("Click Here");
      //Creating the TitlePane
      TitledPane pane = new TitledPane();
      pane.setLayoutX(200);
      pane.setLayoutY(75);
      pane.setText("Sample Titled Pane");
      //Setting contents to the titled pane
      pane.setContent(button);
      //Setting the stage
      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) {
      //Creating toggle buttons
      ToggleButton button1 = new ToggleButton("Java");
      ToggleButton button2 = new ToggleButton("Python");
      ToggleButton button3 = new ToggleButton("C++");
      //Toggle button group
      ToggleGroup group = new ToggleGroup();
      button1.setToggleGroup(group);
      button2.setToggleGroup(group);
      button3.setToggleGroup(group);
      //Adding the toggle button to the pane
      VBox box = new VBox();
      box.getChildren().addAll(button1, button2, button3);
      //Creating the TitlePane
      TitledPane pane = new TitledPane();
      pane.setLayoutX(10);
      pane.setLayoutY(10);
      pane.setText("OpenCV Examples");
      //Adding the toggle button to the pane
      pane.setContent(box);
      //Setting the stage
      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);
   }
}

輸出

更新於:16-May-2020

711次瀏覽

開啟您的 職業

完成課程後獲得認證

開始
廣告