/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package chapter2; import java.util.List; import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Rectangle2D; import javafx.geometry.VPos; import javafx.scene.Group; import javafx.scene.GroupBuilder; import javafx.scene.Scene; import javafx.scene.SceneBuilder; import javafx.scene.control.ButtonBuilder; import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBoxBuilder; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.TextFieldBuilder; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBoxBuilder; import javafx.scene.layout.VBoxBuilder; import javafx.scene.paint.Color; import javafx.scene.shape.RectangleBuilder; import javafx.scene.text.Text; import javafx.scene.text.TextBuilder; import javafx.stage.Screen; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.stage.WindowEvent; /** * * @author admin */ public class StageCoach extends Application { StringProperty title = new SimpleStringProperty(); Text textStageX; Text textStageY; Text textStageW; Text textStageH; Text textStageF; Text textStageR; CheckBox checkBoxResizable; CheckBox checkBoxFullScreen; double dragAnchorX; double dragAnchorY; @Override public void start(Stage stage) { StageStyle stageStyle = StageStyle.DECORATED; List<String> unnamedParams = getParameters().getUnnamed(); if(unnamedParams.size() > 0) { String stageStyleParam = unnamedParams.get(0); if(stageStyleParam.equalsIgnoreCase("transparent")) { stageStyle = StageStyle.TRANSPARENT; } else if(stageStyleParam.equalsIgnoreCase("undecorated")) { stageStyle = StageStyle.UNDECORATED; } else if(stageStyleParam.equalsIgnoreCase("utility")) { stageStyle = StageStyle.UTILITY; } } final Stage stageRef = stage; Group rootGroup; TextField titleTextField; Scene scene = SceneBuilder.create() .width(270) .height(370) .fill(Color.TRANSPARENT) .root( rootGroup = GroupBuilder.create() .children( RectangleBuilder.create() .width(250) .height(350) .arcWidth(50) .arcHeight(50) .fill(Color.SKYBLUE) .build(), VBoxBuilder.create() .layoutX(30) .layoutY(20) .spacing(10) .children( textStageX = TextBuilder.create() .textOrigin(VPos.TOP) .build(), textStageY = TextBuilder.create() .textOrigin(VPos.TOP) .build(), textStageW = TextBuilder.create() .textOrigin(VPos.TOP) .build(), textStageH = TextBuilder.create() .textOrigin(VPos.TOP) .build(), textStageF = TextBuilder.create() .textOrigin(VPos.TOP) .build(), textStageR = TextBuilder.create() .textOrigin(VPos.TOP) .build(), checkBoxResizable = CheckBoxBuilder.create() .text("resizable") .disable(stageStyle == StageStyle.TRANSPARENT || stageStyle == StageStyle.UNDECORATED) .build(), checkBoxFullScreen = CheckBoxBuilder.create() .text("fullScreen") .build(), HBoxBuilder.create() .spacing(10) .children( new Label("title: "), titleTextField = TextFieldBuilder.create() .text("Stage Coach") .prefColumnCount(15) .build() ) .build(), ButtonBuilder.create() .text("toBack()") .onAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { stageRef.toBack(); } }) .build(), ButtonBuilder.create() .text("toFront()") .onAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { stageRef.toFront(); } }) .build(), ButtonBuilder.create() .text("close()") .onAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { stageRef.close(); } }) .build() ) .build() ) .build() ) .build(); // when mouse button is pressed, save the initial position of screen rootGroup.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { dragAnchorX = me.getScreenX() - stageRef.getX(); dragAnchorY = me.getScreenY() - stageRef.getY(); } }); // When screen is dragged, translate it accordingly rootGroup.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { stageRef.setX(me.getScreenX() - dragAnchorX); stageRef.setY(me.getScreenY() - dragAnchorY); } }); // bind textStageX.textProperty().bind(new SimpleStringProperty("x: ").concat(stageRef.xProperty().asString())); textStageY.textProperty().bind(new SimpleStringProperty("y: ").concat(stageRef.yProperty().asString())); textStageW.textProperty().bind(new SimpleStringProperty(" ").concat(stageRef.widthProperty().asString())); textStageH.textProperty().bind(new SimpleStringProperty("height: ").concat(stageRef.heightProperty().asString())); textStageF.textProperty().bind(new SimpleStringProperty("focused: ").concat(stageRef.focusedProperty().asString())); stage.setResizable(true); textStageR.textProperty().bind(new SimpleStringProperty("resizable: ").concat(stageRef.resizableProperty().asString())); // 下面这条语句不起作用... checkBoxResizable.selectedProperty().bindBidirectional(stage.resizableProperty()); checkBoxFullScreen.selectedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue ov, Object oldValue, Object newValue) { stageRef.setFullScreen(checkBoxFullScreen.selectedProperty().getValue()); } }); title.bind(titleTextField.textProperty()); stage.setScene(scene); stage.titleProperty().bind(title); stage.initStyle(stageStyle); stage.setOnCloseRequest(new EventHandler<WindowEvent>() { @Override public void handle(WindowEvent we) { System.out.println("Stage is closing"); } }); stage.show(); Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds(); stage.setX((primScreenBounds.getWidth() - stage.getWidth())/2); stage.setY((primScreenBounds.getHeight() - stage.getHeight())/2); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
《Pro JavaFX 2》第2章中的例子StageCoach,发现黄色背景那条语句不起作用,原因未知。可改用监听器
checkBoxResizable.selectedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue ov, Object oldValue, Object newValue) { stageRef.setResizable(checkBoxResizable.selectedProperty().getValue()); } });