从我的一个项目修改
这就是它的样子
当最大化时
package Project
Main.class
public class Main extends Application {
double dragX,dragY;
@Override
public void start(Stage primaryStage) {
try {
primaryStage.initStyle(StageStyle.TRANSPARENT);
HBox hbox = new HBox(10.0);
hbox.setPrefHeight(70);
hbox.setFillHeight(true);
BackgroundFill bf = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null);
hbox.setBackground(new Background(bf));
AnchorPane ap = new AnchorPane();
ResponsiveButton min = new ResponsiveButton("Minimise");
min.noDefaultFill = true;
min.setBorder(Border.EMPTY);
min.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
//min.setPrefSize(32.0,32.0);
min.setAlignment(Pos.CENTER);
min.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
primaryStage.setIconified(true);
}
});
ResponsiveButton max = new ResponsiveButton("Maximise");
max.setBorder(Border.EMPTY);
max.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
//max.setPrefSize(32.0,32.0);
max.noDefaultFill = true;
max.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
if(primaryStage.isMaximized()){
primaryStage.setMaximized(false);
}else{
primaryStage.setMaximized(true);
}
}
});
ResponsiveButton logout = new ResponsiveButton("Close me");
logout.noDefaultFill = true;
logout.setBorder(Border.EMPTY);
logout.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
//logout.setPrefSize(32.0,32.0);
logout.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
primaryStage.close();
}
});
Label label = new Label();
label.setText("Custom Stage");
label.setTextFill(Color.WHITESMOKE);
//label.setAlignment(Pos.CENTER);
label.setPadding(new Insets(0.0,0.0,0.0,18.0));
label.setFont(Font.font("Tahoma", FontWeight.BOLD, 25));
HBox.setHgrow(label, Priority.ALWAYS);
hbox.setAlignment(Pos.CENTER);
hbox.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if(!primaryStage.isMaximized()){
dragX = primaryStage.getX() - mouseEvent.getScreenX();
dragY = primaryStage.getY() - mouseEvent.getScreenY();
hbox.setCursor(Cursor.MOVE);
}
}
});
hbox.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if(!primaryStage.isMaximized()){
hbox.setCursor(Cursor.HAND);
BackgroundFill bf = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null);
hbox.setBackground(new Background(bf));
}
}
});
hbox.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if(!primaryStage.isMaximized()){
primaryStage.setX(mouseEvent.getScreenX() + dragX);
primaryStage.setY(mouseEvent.getScreenY() + dragY);
BackgroundFill bf = new BackgroundFill(Color.BLANCHEDALMOND, new CornerRadii(1), null);
hbox.setBackground(new Background(bf));
}
}
});
hbox.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if(!primaryStage.isMaximized()){
if (!mouseEvent.isPrimaryButtonDown()) {
hbox.setCursor(Cursor.HAND);
}
}
}
});
hbox.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
hbox.setCursor(Cursor.DEFAULT);
}
}
});
HBox hbox2 = new HBox(10.0);
HBox.setHgrow(hbox2, Priority.ALWAYS);
hbox2.getChildren().addAll(min,max,logout);
hbox.getChildren().addAll(label,hbox2);
hbox2.setAlignment(Pos.CENTER_RIGHT);
AnchorPane.setTopAnchor(hbox, 0.0);AnchorPane.setLeftAnchor(hbox, 5.0);AnchorPane.setRightAnchor(hbox, 0.0);
AnchorPane.setBottomAnchor(ap , 0.0);AnchorPane.setTopAnchor(ap , 40.0);
AnchorPane.setLeftAnchor(ap , 5.0); AnchorPane.setRightAnchor(ap , 0.0);
ap .setPrefSize(500, 560);
AnchorPane main = new AnchorPane();
main.getChildren().addAll(hbox,ap);
Scene sc = new Scene(main,500,500);
sc.setRoot(main);
Pane p = new Pane();
AnchorPane.setTopAnchor(p, 80.0);
AnchorPane.setLeftAnchor(p, 60.0);
Group root = new Group();
p.setPrefSize(400, 400);
p.setBackground(new Background(new BackgroundFill(Color.GOLD,
null, null)));
root.getChildren().add(p);
ap.getChildren().add(p);
primaryStage.setScene(sc);
primaryStage.setTitle("Custom window");
primaryStage.show();
BubbledLabel bl1 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
bl1.relocate(10, 50);
bl1.setText("I love this new Custom stage");
bl1.setBackground(new Background(new BackgroundFill(Color.VIOLET,
null, null)));
BubbledLabel bl2 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
bl2.relocate(310, 100);
bl2.setText("We all love it");
bl2.setBackground(new Background(new BackgroundFill(Color.LIGHTPINK,
null, null)));
BubbledLabel bl3 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
bl3.relocate(10, 150);
bl3.setText("Nikitoslav is going love it");
bl3.setBackground(new Background(new BackgroundFill(Color.YELLOWGREEN,
null, null)));
BubbledLabel bl4 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
bl4.relocate(165, 200);
bl4.setText("hmmm.. you have you asked him???");
bl4.setBackground(new Background(new BackgroundFill(Color.GREENYELLOW,
null, null)));
BubbledLabel bl5 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
bl5.relocate(10, 250);
bl5.setText("yea! yea! ");
bl5.setBackground(new Background(new BackgroundFill(Color.YELLOWGREEN,
null, null)));
BubbledLabel bl6 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
bl6.relocate(250, 300);
bl6.setText("Oh really!!? oh stop it!!");
bl6.setBackground(new Background(new BackgroundFill(Color.GREENYELLOW,
null, null)));
p.getChildren().addAll(bl1,bl2,bl3,bl4,bl5,bl6);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
如果你只是复制这里的代码,把ResponsiveButton改成Button,去掉Anchorpaneap`和它的子类,它的类不会被添加,但是如果你下载包工程就没有问题了。