JavaFX TitledPane

2018-01-09 19:24 更新

JavaFX教程 - JavaFX TitledPane


標(biāo)題窗格是具有標(biāo)題的面板,窗格可以打開和關(guān)閉。我們可以添加Node(如UI控件或圖像)和一組元素到窗格。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
//from  w w w . j  a v  a 2 s  .  c o m
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 350, 250);
    TitledPane titledPane = new TitledPane("My Title", new CheckBox("OK"));

    HBox hbox = new HBox(10);
    hbox.setPadding(new Insets(20, 0, 0, 20));
    hbox.getChildren().setAll(titledPane);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(hbox);
    stage.setScene(scene);
    stage.show();
  }
}

上面的代碼生成以下結(jié)果。

null


創(chuàng)建標(biāo)題窗格

要創(chuàng)建一個TitledPane控件,請調(diào)用其構(gòu)造函數(shù)。

以下代碼使用TitledPane的雙參數(shù)構(gòu)造函數(shù)。它將標(biāo)題窗格命名為“我的窗格",并用一個Button控件填充窗格。

TitledPane tp = new TitledPane("My Pane", new Button("Button"));

接下來的幾行做了與上面的代碼相同的事情,而沒有使用構(gòu)造函數(shù)帶參數(shù)。 它創(chuàng)建一個帶有默認(rèn)空構(gòu)造函數(shù)的TittedPane并設(shè)置標(biāo)題并進行內(nèi)容控制。

TitledPane tp = new TitledPane();
tp.setText("My Titled Pane");
tp.setContent(new Button("Button"));

以下代碼使用GridPane在TitledPane中布局控件。

TitledPane gridTitlePane = new TitledPane();
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
...
gridTitlePane.setText("Grid");
gridTitlePane.setContent(grid);

我們可以定義標(biāo)題窗格的打開和關(guān)閉方式。默認(rèn)情況下,所有標(biāo)題窗格都是可折疊的,打開和關(guān)閉操作都是動畫。

setCollapsible(false)關(guān)閉Collapsible狀態(tài)。setAnimated(false)停止動畫。

TitledPane tp = new TitledPane();
tp.setCollapsible(false);//remove closing action
tp.setAnimated(false);//stop animating

完整的源代碼

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/*from   ww w . j a  v a 2s  .  c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    TitledPane titledPane = new TitledPane("My Title", new CheckBox("OK"));
    titledPane.setCollapsible(false);//remove closing action
    titledPane.setAnimated(false);//stop animating
    
    HBox hbox = new HBox(10);
    hbox.setPadding(new Insets(20, 0, 0, 20));
    hbox.getChildren().setAll(titledPane);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(hbox);
    stage.setScene(scene);
    stage.show();
  }
}

上面的代碼生成以下結(jié)果。

null


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號