JavaFX手風(fēng)琴

2018-01-09 19:24 更新

JavaFX教程 - JavaFX手風(fēng)琴


可以使用手風(fēng)琴控件對(duì)標(biāo)題窗格進(jìn)行分組。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;
//from   w  w w  .j  ava 2  s  .com
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group g = new Group();
    Scene scene = new Scene(g, 550, 250);

    TitledPane t1 = new TitledPane("T1", new Button("B1"));
    TitledPane t2 = new TitledPane("T2", new Button("B2"));
    TitledPane t3 = new TitledPane("T3", new Button("B3"));
    Accordion accordion = new Accordion();
    accordion.getPanes().addAll(t1, t2, t3);

    g.getChildren().add(accordion);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

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

null


手風(fēng)琴事件

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;
/*  w  w w  .  ja va  2 s  . c  o m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group g = new Group();
    Scene scene = new Scene(g, 550, 250);

    TitledPane t1 = new TitledPane("T1", new Button("B1"));
    TitledPane t2 = new TitledPane("T2", new Button("B2"));
    TitledPane t3 = new TitledPane("T3", new Button("B3"));
    Accordion accordion = new Accordion();
    accordion.getPanes().addAll(t1, t2, t3);

    accordion.expandedPaneProperty().addListener(
        (ObservableValue<? extends TitledPane> ov, TitledPane old_val, 
        TitledPane new_val) -> {
            if (new_val != null) {
                System.out.println(accordion.getExpandedPane().getText());
            }
    });

    
    g.getChildren().add(accordion);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

當(dāng)在手風(fēng)琴中打開標(biāo)題窗格時(shí),手風(fēng)琴的擴(kuò)展窗格屬性改變。

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

null


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)