---
title: "JavaFX学习 自定义分页，工具栏，window"
author: "lujin55"
date: "2012-12-14T14:32:37Z"
tags: []
categories: ["编程语言"]
slug: "javafx-window"
draft: false
lastmod: "2013-03-04T15:56:45Z"
id: 1747929
url: "https://lujin55.iteye.com/blog/1747929"
---

这是很多都是借鉴别人的。
用Button实现关闭Icon，最小化Icon，最大化Icon
![](http://dl.iteye.com/upload/attachment/0078/5669/e0d09ab0-e8e2-30f5-98aa-405529ed4a5c.png)

```java

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();
	}
}

```


```java

.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
![](http://dl.iteye.com/upload/attachment/0078/5673/209e2327-85b8-3e20-8178-7de3f8af0c92.png)


  工具栏ToolBar
![](http://dl.iteye.com/upload/attachment/0078/6417/94c2e5d9-bf0a-35d8-abfc-daa7d4d96842.png)

这里有个javafx学习的中文网站，我把内容都丢那上面去了。
http://www.jfxee.com/