Javafx 2 在JavaFX中创建向导
有没有关于如何在JavaFX中创建向导的示例 例如,设置程序或进行配置。这可以用简单的代码完成,还是我需要创建自定义组件?这里是 您可以修改此代码以使向导看起来更专业Javafx 2 在JavaFX中创建向导,javafx-2,javafx,javafx-8,Javafx 2,Javafx,Javafx 8,有没有关于如何在JavaFX中创建向导的示例 例如,设置程序或进行配置。这可以用简单的代码完成,还是我需要创建自定义组件?这里是 您可以修改此代码以使向导看起来更专业 导入javafx.application.application; 导入javafx.beans.property.*; 导入javafx.beans.value.*; 导入javafx.collections.*; 导入javafx.scene.*; 导入javafx.scene.control.*; 导入javafx.s
导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.beans.value.*;
导入javafx.collections.*;
导入javafx.scene.*;
导入javafx.scene.control.*;
导入javafx.scene.layout.*;
导入javafx.stage.stage;
导入java.util.Stack;
/**
*此类使用向导显示调查
*/
公营班级调查扩展应用{
公共静态void main(字符串[]args)引发异常{
发射(args);
}
@凌驾
public void start(Stage)引发异常{
//配置并显示场景和舞台。
舞台场景(新场景(新调查向导(舞台),400250);
stage.show();
}
}
/**
*基本向导基础结构类
*/
类向导扩展了StackPane{
私有静态final int UNDEFINED=-1;
私有ObservableList pages=FXCollections.observableArrayList();
私有堆栈历史=新堆栈();
private int curPageIdx=未定义;
向导(向导页面…节点){
pages.addAll(节点);
导航(0);
setStyle(“-fx填充:10;-fx背景颜色:cornsilk;”);
}
作废下一页(){
如果(hasNextPage()){
导航(curPageIdx+1);
}
}
无效优先权(){
if(hasPriorPage()){
navTo(history.pop(),false);
}
}
布尔hasNextPage(){
返回值(curPageIdx=pages.size())返回;
if(curPageIdx!=未定义){
如果(历史记录){
历史推送(curPageIdx);
}
}
WizardPage nextPage=pages.get(nextPageIdx);
curPageIdx=nextPageIdx;
getChildren().clear();
getChildren().add(下一页);
nextPage.manageButtons();
}
无效导航(int nextPageIdx){
navTo(nextPageIdx,真);
}
无效navTo(字符串id){
if(id==null){
返回;
}
pages.stream()
.filter(page->id.equals(page.getId()))
.findFirst()
.ifPresent(第->
导航(第页,索引(第页))
);
}
公共空间整理(){
}
公开作废取消(){
}
}
/**
*基本向导页面类
*/
抽象类向导页扩展了VBox{
按钮优先级按钮=新按钮(“上一个”);
按钮下一个按钮=新按钮(“N_ext”);
按钮取消按钮=新按钮(“取消”);
按钮完成按钮=新按钮(“完成”);
向导页面(字符串标题){
标签=新标签(标题);
label.setStyle(“-fx字体大小:粗体;-fx填充:0;”);
setId(标题);
起搏器(5);
setStyle(“-fx填充:10;-fx背景颜色:honeydew;-fx边框颜色:派生(honeydew,-30%);-fx边框宽度:3;”);
区域弹簧=新区域();
setVgrow(spring,Priority.ALWAYS);
getChildren().addAll(getContent(),spring,getButtons());
setOnAction(事件->优先级页());
setOnAction(事件->下一页());
cancelButton.setOnAction(事件->获取向导().cancel());
设置操作(事件->获取向导().finish());
}
HBox getButtons(){
区域弹簧=新区域();
HBox.setHgrow(spring,Priority.ALWAYS);
HBox按钮BAR=新的HBox(5);
cancelButton.setCancelButton(真);
finishButton.setDefaultButton(true);
buttonBar.getChildren().addAll(spring、priorButton、nextButton、cancelButton、finishButton);
返回按钮栏;
}
抽象父getContent();
布尔hasNextPage(){
返回getWizard().hasNextPage();
}
布尔hasPriorPage(){
返回getWizard().hasPriorPage();
}
作废下一页(){
getWizard().nextPage();
}
无效优先权(){
getWizard().priorPage();
}
无效navTo(字符串id){
getWizard().navTo(id);
}
向导getWizard(){
返回(向导)getParent();
}
公用按钮(){
如果(!hasPriorPage()){
设置禁用(true);
}
如果(!hasNextPage()){
设置禁用(true);
}
}
}
/**
*这节课展示了一个满意度调查
*/
类SurveyWizard扩展向导{
舞台主人;
公共调查向导(舞台所有者){
超级(新投诉页面(),新更多信息页面(),新感谢页面());
this.owner=所有者;
}
公共空间整理(){
System.out.println(“Had complaint?”+SurveyData.instance.hascamplaints.get());
if(SurveyData.instance.hasComplaints.get()){
System.out.println(“投诉:”+
(SurveyData.instance.complaints.get().isEmpty())
“没有细节”
:“\n”+SurveyData.instance.complaints.get())
);
}
owner.close();
}
公开作废取消(){
系统输出打印项次(“取消”);
owner.close();
}
}
/**
*客户输入的调查响应的简单占位符类。
*/
类调查数据{
BooleanProperty=new SimpleBoleanProperty();
StringProperty=new SimpleStringProperty();
静态SurveyData实例=新建SurveyData();
}
/**
*此类确定用户是否有投诉。
*如果没有,则跳到向导的最后一页。
*/
类投诉页面扩展向导页面{
专用单选按钮
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.Stack;
/**
* This class displays a survey using a wizard
*/
public class Survey extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// configure and display the scene and stage.
stage.setScene(new Scene(new SurveyWizard(stage), 400, 250));
stage.show();
}
}
/**
* basic wizard infrastructure class
*/
class Wizard extends StackPane {
private static final int UNDEFINED = -1;
private ObservableList<WizardPage> pages = FXCollections.observableArrayList();
private Stack<Integer> history = new Stack<>();
private int curPageIdx = UNDEFINED;
Wizard(WizardPage... nodes) {
pages.addAll(nodes);
navTo(0);
setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
}
void nextPage() {
if (hasNextPage()) {
navTo(curPageIdx + 1);
}
}
void priorPage() {
if (hasPriorPage()) {
navTo(history.pop(), false);
}
}
boolean hasNextPage() {
return (curPageIdx < pages.size() - 1);
}
boolean hasPriorPage() {
return !history.isEmpty();
}
void navTo(int nextPageIdx, boolean pushHistory) {
if (nextPageIdx < 0 || nextPageIdx >= pages.size()) return;
if (curPageIdx != UNDEFINED) {
if (pushHistory) {
history.push(curPageIdx);
}
}
WizardPage nextPage = pages.get(nextPageIdx);
curPageIdx = nextPageIdx;
getChildren().clear();
getChildren().add(nextPage);
nextPage.manageButtons();
}
void navTo(int nextPageIdx) {
navTo(nextPageIdx, true);
}
void navTo(String id) {
if (id == null) {
return;
}
pages.stream()
.filter(page -> id.equals(page.getId()))
.findFirst()
.ifPresent(page ->
navTo(pages.indexOf(page))
);
}
public void finish() {
}
public void cancel() {
}
}
/**
* basic wizard page class
*/
abstract class WizardPage extends VBox {
Button priorButton = new Button("_Previous");
Button nextButton = new Button("N_ext");
Button cancelButton = new Button("Cancel");
Button finishButton = new Button("_Finish");
WizardPage(String title) {
Label label = new Label(title);
label.setStyle("-fx-font-weight: bold; -fx-padding: 0 0 5 0;");
setId(title);
setSpacing(5);
setStyle("-fx-padding:10; -fx-background-color: honeydew; -fx-border-color: derive(honeydew, -30%); -fx-border-width: 3;");
Region spring = new Region();
VBox.setVgrow(spring, Priority.ALWAYS);
getChildren().addAll(getContent(), spring, getButtons());
priorButton.setOnAction(event -> priorPage());
nextButton.setOnAction(event -> nextPage());
cancelButton.setOnAction(event -> getWizard().cancel());
finishButton.setOnAction(event -> getWizard().finish());
}
HBox getButtons() {
Region spring = new Region();
HBox.setHgrow(spring, Priority.ALWAYS);
HBox buttonBar = new HBox(5);
cancelButton.setCancelButton(true);
finishButton.setDefaultButton(true);
buttonBar.getChildren().addAll(spring, priorButton, nextButton, cancelButton, finishButton);
return buttonBar;
}
abstract Parent getContent();
boolean hasNextPage() {
return getWizard().hasNextPage();
}
boolean hasPriorPage() {
return getWizard().hasPriorPage();
}
void nextPage() {
getWizard().nextPage();
}
void priorPage() {
getWizard().priorPage();
}
void navTo(String id) {
getWizard().navTo(id);
}
Wizard getWizard() {
return (Wizard) getParent();
}
public void manageButtons() {
if (!hasPriorPage()) {
priorButton.setDisable(true);
}
if (!hasNextPage()) {
nextButton.setDisable(true);
}
}
}
/**
* This class shows a satisfaction survey
*/
class SurveyWizard extends Wizard {
Stage owner;
public SurveyWizard(Stage owner) {
super(new ComplaintsPage(), new MoreInformationPage(), new ThanksPage());
this.owner = owner;
}
public void finish() {
System.out.println("Had complaint? " + SurveyData.instance.hasComplaints.get());
if (SurveyData.instance.hasComplaints.get()) {
System.out.println("Complaints: " +
(SurveyData.instance.complaints.get().isEmpty()
? "No Details"
: "\n" + SurveyData.instance.complaints.get())
);
}
owner.close();
}
public void cancel() {
System.out.println("Cancelled");
owner.close();
}
}
/**
* Simple placeholder class for the customer entered survey response.
*/
class SurveyData {
BooleanProperty hasComplaints = new SimpleBooleanProperty();
StringProperty complaints = new SimpleStringProperty();
static SurveyData instance = new SurveyData();
}
/**
* This class determines if the user has complaints.
* If not, it jumps to the last page of the wizard.
*/
class ComplaintsPage extends WizardPage {
private RadioButton yes;
private RadioButton no;
private ToggleGroup options = new ToggleGroup();
public ComplaintsPage() {
super("Complaints");
nextButton.setDisable(true);
finishButton.setDisable(true);
yes.setToggleGroup(options);
no.setToggleGroup(options);
options.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
@Override
public void changed(ObservableValue<? extends Toggle> observableValue, Toggle oldToggle, Toggle newToggle) {
nextButton.setDisable(false);
finishButton.setDisable(false);
}
});
}
Parent getContent() {
yes = new RadioButton("Yes");
no = new RadioButton("No");
SurveyData.instance.hasComplaints.bind(yes.selectedProperty());
return new VBox(
5,
new Label("Do you have complaints?"), yes, no
);
}
void nextPage() {
// If they have complaints, go to the normal next page
if (options.getSelectedToggle().equals(yes)) {
super.nextPage();
} else {
// No complaints? Short-circuit the rest of the pages
navTo("Thanks");
}
}
}
/**
* This page gathers more information about the complaint
*/
class MoreInformationPage extends WizardPage {
public MoreInformationPage() {
super("More Info");
}
Parent getContent() {
TextArea textArea = new TextArea();
textArea.setWrapText(true);
textArea.setPromptText("Tell me what's wrong Dave...");
nextButton.setDisable(true);
textArea.textProperty().addListener((observableValue, oldValue, newValue) -> {
nextButton.setDisable(newValue.isEmpty());
});
SurveyData.instance.complaints.bind(textArea.textProperty());
return new VBox(
5,
new Label("Please enter your complaints."),
textArea
);
}
}
/**
* This page thanks the user for taking the survey
*/
class ThanksPage extends WizardPage {
public ThanksPage() {
super("Thanks");
}
Parent getContent() {
StackPane stack = new StackPane(
new Label("Thanks!")
);
VBox.setVgrow(stack, Priority.ALWAYS);
return stack;
}
}
String[] pageNames = { "page1","page2","page3" };
Platform.runLater(() ->{
try {
runWizard(I18n.get(I18n.WELCOME),"/com/bitplan/demo/",pageNames);
} catch (Exception e) {
ErrorHandler.handle(e)
}
});
<!-- https://mvnrepository.com/artifact/org.controlsfx/controlsfx -->
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.40.12</version>
</dependency>
/**
* run the wizard with the given title
* @param title - of the wizard
* @param resourcePath - where to load the fxml files from
* @param pageNames - without .fxml extenion
* @throws Exception - e.g. IOException
*/
public void runWizard(String title,String resourcePath,String ...pageNames) throws Exception {
Wizard wizard = new Wizard();
wizard.setTitle(title);
WizardPane[] pages = new WizardPane[pageNames.length];
int i = 0;
for (String pageName : pageNames) {
Parent root = FXMLLoader.load(getClass()
.getResource(resourcePath + pageName + ".fxml"));
WizardPane page = new WizardPane();
page.setHeaderText(I18n.get(pageName));
page.setContent(root);
pages[i++] = page;
}
wizard.setFlow(new LinearFlow(pages));
wizard.showAndWait().ifPresent(result -> {
if (result == ButtonType.FINISH) {
System.out
.println("Wizard finished, settings: " + wizard.getSettings());
}
});
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.controlsfx.dialog.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="WizardController"
>
<WizardPane fx:id="step1Pane" headerText="Step 1">
<content>
<Label text="Do action 1, then action 2."/>
<ButtonBar>
<buttons>
<Button text="Action 1" onAction="#displayScreenForAction1"/>
</buttons>
</ButtonBar>
</content>
</WizardPane>
<WizardPane fx:id="step2Pane" headerText="Step 2">
...
</WizardPane>
</AnchorPane>
public class WizardController {
@FXML
private WizardPane step1Pane;
@FXML
private WizardPane step2Pane;
...
void show() {
Wizard wizard = new Wizard();
wizard.setFlow(new Wizard.LinearFlow(
step1Pane,
step2Pane,
...
));
wizard.resultProperty().addListener((observable, oldValue, newValue) -> {
wizardStage.close();
});
// show wizard and wait for response
Stage wizardStage = new Stage();
wizardStage.setTitle("... wizard");
wizardStage.setScene(wizard.getScene());
wizardStage.show();
}
}
public class WizardApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws Exception {
super.init();
...
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("WizardView.fxml"));
wizardController = loader.getController();
}
@FXML
private void showWizard(ActionEvent actionEvent) {
wizardController.show();
}