`

JavaFX学习 自定义分页,工具栏,window

阅读更多
   这是很多都是借鉴别人的。
用Button实现关闭Icon,最小化Icon,最大化Icon

public class TestButton extends Application {

	double width = 600;
	double height = 300;
	Stage stage;
	double offsetX;
	double offsetY;

	@Override
	public void start(Stage stage) throws Exception {
		
		VBox root = new VBox();
		init(root);
		Scene scene = new Scene(root, width, height);
		scene.getStylesheets().add(getClass().getResource("dialogs.css").toExternalForm());
		Stage subStage = new Stage(StageStyle.TRANSPARENT);
		subStage.initOwner(stage);
		subStage.setScene(scene);
		subStage.show();
		this.stage = subStage;
	}

	public void init(VBox root) {
		HBox box = new HBox(10);
		ToolBar tb = new ToolBar();
		Button closeButton = createButton("close");
		closeButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				stage.close();
			}
		});
        Button minimizeButton = createButton("minimize");
        minimizeButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				stage.setIconified(!stage.isIconified());
			}
		});
        
        Button maximizeButton = createButton("maximize");
        maximizeButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				stage.setFullScreen(true);
			}
		});
        box.getChildren().addAll(minimizeButton,maximizeButton,closeButton);
        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        tb.getItems().addAll(region,box);
        tb.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				offsetX = event.getSceneX();
				offsetY = event.getSceneY();
			}
		});
        tb.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				stage.setX(event.getScreenX()-offsetX);
				stage.setY(event.getScreenY()-offsetY);
			}
		});
        HBox center = new HBox();
        center.setStyle("-fx-background-color: #949494");
        VBox.setVgrow(center, Priority.ALWAYS);
		root.getChildren().addAll(tb,center);
	}
	
	public Button createButton(String name){
		StackPane graphic = new StackPane();
		graphic.getStyleClass().setAll("graphic");
        Button button = new Button();
        button.getStyleClass().setAll("window-button");
        button.getStyleClass().add("window-"+name+"-button");
        button.setGraphic(graphic);
        button.setMinSize(17, 17);
        button.setPrefSize(17, 17);
        return button;
	}
	public static void main(String args[]) {
		launch();
	}
}

.window-button {
    -fx-skin: "com.sun.javafx.scene.control.skin.ButtonSkin";
    -fx-background-color: transparent transparent;
    -fx-background-insets: 0, 1;
    -fx-background-radius: 2;
    -fx-padding: 0 0 0 0;
    -fx-alignment: center;
}

.window-button:hover {
    -fx-background-color: linear-gradient(#505050,#2d2d2d),
                          linear-gradient(#a3a3a3, #8b8b8b 34%, #777777 36%, #777777 63%, #8b8b8b 65%, #adadad);
}

.window-button:pressed {
    -fx-background-color: linear-gradient(#515151,#202020),
                          linear-gradient(#a3a3a3, #8b8b8b 34%, #777777 36%, #777777 63%, #8b8b8b 65%, #adadad);
}

.window-button .graphic {
    -fx-background-color: #949494;
    -fx-scale-shape: false;
    -fx-padding: 4.5 4.5 4.5 4.5; /* Graphic is 9x9 px */
}

.window-button:hover .graphic {
    -fx-background-color: #fefeff;
}

.window-button:pressed .graphic {
    -fx-background-color: #cfcfcf;
}

.window-close-button .graphic {
    -fx-shape: "M395.992,296.758l1.794-1.794l7.292,7.292l-1.795,1.794 L395.992,296.758z M403.256,294.992l1.794,1.794l-7.292,7.292l-1.794-1.795 L403.256,294.992z";
}

.window-minimize-button .graphic {
    -fx-shape: "M420.012,299.248v2.537h-9.001v-2.537H420.012z";
}

.window-maximize-button .graphic {
    -fx-shape: "M406.283,294.985h2.537v9.031h-2.538L406.283,294.985z M412.012,298.248v2.537h-9.001v-2.537H412.012z";
}




  分页Button



  工具栏ToolBar


这里有个javafx学习的中文网站,我把内容都丢那上面去了。
http://www.jfxee.com/
  • 大小: 1.9 KB
  • 大小: 1.4 KB
  • 大小: 3.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics