JavaFx - 自定义标题栏(附:窗口移动、关闭)
程序员文章站
2022-07-13 23:17:58
...
app.css
#pane {
-fx-background-color: #3c3c3c;
}
.button {
-fx-border-color: #c8c8c8;
-fx-border-radius: 4px;
-fx-font-weight: bold;
-fx-font-size: 18px;
-fx-alignment: center;
}
.widthButton {
-fx-font-size: 12px;
}
touch.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="pane" prefHeight="117.0" prefWidth="720.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.luxsun.touch.Controller">
<center>
<AnchorPane prefHeight="117.0" prefWidth="720.0" BorderPane.alignment="CENTER">
<children>
<Button layoutX="14.0" layoutY="19.0" mnemonicParsing="false" onAction="#cmdCtrl" prefHeight="80.0" prefWidth="80.0" text="Alt1" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="103.0" layoutY="19.0" mnemonicParsing="false" onMouseClicked="#cmdAlt" prefHeight="80.0" prefWidth="80.0" text="Alt2" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="190.0" layoutY="19.0" mnemonicParsing="false" onTouchPressed="#cmdShift" prefHeight="80.0" prefWidth="80.0" text="Shift" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="288.0" layoutY="19.0" mnemonicParsing="false" onTouchPressed="#cmdCtrlBlank" prefHeight="80.0" prefWidth="80.0" styleClass="widthButton" text="Ctrl & Space" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="389.0" layoutY="19.0" mnemonicParsing="false" onTouchPressed="#cmdCtrlShift" prefHeight="80.0" prefWidth="80.0" styleClass="widthButton" text="Ctrl & Shift" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="492.0" layoutY="19.0" mnemonicParsing="false" onTouchPressed="#cmdTouchA" prefHeight="80.0" prefWidth="80.0" text="Touch A" textAlignment="CENTER" textFill="#c8c8c8" />
<Button layoutX="597.0" layoutY="19.0" mnemonicParsing="false" onMouseClicked="#cmdClickA" prefHeight="80.0" prefWidth="80.0" text="Click A" textAlignment="CENTER" textFill="#c8c8c8" />
</children>
</AnchorPane>
</center>
</BorderPane>
窗口移动
package com.luxsun.touch;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class DragWindowHandler implements EventHandler<MouseEvent> {
private Stage primaryStage; // primaryStage为start方法头中的Stage
private double oldStageX;
private double oldStageY;
private double oldScreenX;
private double oldScreenY;
public DragWindowHandler(Stage primaryStage) { //构造器
this.primaryStage = primaryStage;
}
@Override
public void handle(MouseEvent e) {
if (e.getEventType() == MouseEvent.MOUSE_PRESSED) { // 鼠标按下的事件
this.oldStageX = this.primaryStage.getX();
this.oldStageY = this.primaryStage.getY();
this.oldScreenX = e.getScreenX();
this.oldScreenY = e.getScreenY();
} else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) { // 鼠标拖动的事件
this.primaryStage.setX(e.getScreenX() - this.oldScreenX + this.oldStageX);
this.primaryStage.setY(e.getScreenY() - this.oldScreenY + this.oldStageY);
}
}
}
主程序(关闭按钮)
package com.luxsun.touch;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.awt.*;
import java.awt.event.KeyEvent;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Application.setUserAgentStylesheet(getClass().getResource("/app.css").toExternalForm());
Parent root = FXMLLoader.load(getClass().getResource("/touch.fxml"));
BorderPane borderPane = (BorderPane)root;
ToolBar toolBar = new ToolBar();
int height = 25;
toolBar.setPrefHeight(height);
toolBar.setMinHeight(height);
toolBar.setMaxHeight(height);
toolBar.getItems().add(new WindowButtons());
toolBar.setStyle("-fx-background-color: #3c3c3c;");
DragWindowHandler handler = new DragWindowHandler(primaryStage);
toolBar.setOnMousePressed(handler);/* 鼠标按下 */
toolBar.setOnMouseDragged(handler);/* 鼠标拖动 */
borderPane.setTop(toolBar);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setOpacity(0.95D);
primaryStage.setTitle("Touch Panel");
Scene scene = new Scene(borderPane, 720, 117 + 25);
primaryStage.setScene(scene);
primaryStage.setAlwaysOnTop(true);
primaryStage.show();
}
// 关闭按钮
class WindowButtons extends HBox {
public WindowButtons() {
Button closeBtn = new Button("X");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Platform.exit();
}
});
this.getChildren().add(closeBtn);
}
}
public static void main(String[] args) {
launch(args);
}
}