From 43e1f76f8cd14c3ecf5902743f114896d19e2c32 Mon Sep 17 00:00:00 2001 From: Gary Gan Date: Mon, 24 Nov 2025 16:09:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BA=86?= =?UTF-8?q?=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/bytevibe/hyperpoint/Controller.java | 6 +- .../java/dev/bytevibe/hyperpoint/Main.java | 2 +- .../bytevibe/hyperpoint/PropertyPanel.java | 168 ++++++++---------- .../dev/bytevibe/hyperpoint/main.fxml | 21 +-- .../bytevibe/hyperpoint/propertyPanel.fxml | 64 +++++++ 5 files changed, 158 insertions(+), 103 deletions(-) create mode 100644 src/main/resources/dev/bytevibe/hyperpoint/propertyPanel.fxml diff --git a/src/main/java/dev/bytevibe/hyperpoint/Controller.java b/src/main/java/dev/bytevibe/hyperpoint/Controller.java index 541bc10..4e8233b 100644 --- a/src/main/java/dev/bytevibe/hyperpoint/Controller.java +++ b/src/main/java/dev/bytevibe/hyperpoint/Controller.java @@ -43,7 +43,7 @@ public class Controller implements Initializable { @FXML private AnchorPane drawingCanvasContainer; @FXML - private VBox propertyPanel; + private VBox propertyPanelContainer; @FXML private AnchorPane scenePane; @@ -134,7 +134,7 @@ public class Controller implements Initializable { // 移除旧的Canvas drawingCanvasContainer.getChildren().clear(); - propertyPanel.getChildren().clear(); + propertyPanelContainer.getChildren().clear(); // 创建新的Canvas PageContent pageContent = page.getPageContent(); @@ -149,7 +149,7 @@ public class Controller implements Initializable { // 创建属性面板 propertyPanelComponent = new PropertyPanel(drawingCanvas); - propertyPanel.getChildren().add(propertyPanelComponent); + propertyPanelContainer.getChildren().add(propertyPanelComponent); } } diff --git a/src/main/java/dev/bytevibe/hyperpoint/Main.java b/src/main/java/dev/bytevibe/hyperpoint/Main.java index 38e53a8..fcd3351 100644 --- a/src/main/java/dev/bytevibe/hyperpoint/Main.java +++ b/src/main/java/dev/bytevibe/hyperpoint/Main.java @@ -18,7 +18,7 @@ public class Main extends Application { public void start(Stage stage) throws Exception { try { Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); - Scene scene = new Scene(root, 1200, 600); + Scene scene = new Scene(root, 1300, 700); stage.setScene(scene); Image icon = new Image(getClass().getResourceAsStream("icon.png")); diff --git a/src/main/java/dev/bytevibe/hyperpoint/PropertyPanel.java b/src/main/java/dev/bytevibe/hyperpoint/PropertyPanel.java index 248a962..a7054a7 100644 --- a/src/main/java/dev/bytevibe/hyperpoint/PropertyPanel.java +++ b/src/main/java/dev/bytevibe/hyperpoint/PropertyPanel.java @@ -1,119 +1,103 @@ package dev.bytevibe.hyperpoint; -import javafx.geometry.Insets; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; import javafx.scene.control.*; -import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; +import java.io.IOException; +import java.net.URL; +import java.util.ResourceBundle; + /** * 属性编辑面板,用于编辑选中对象的属性 + * 使用FXML加载UI布局 */ -public class PropertyPanel extends VBox { - private DrawingCanvas canvas; +public class PropertyPanel extends VBox implements Initializable { + @FXML private Label objectTypeLabel; + @FXML private Label positionLabel; + @FXML private TextField textContentField; + @FXML private ComboBox fontFamilyCombo; + @FXML private Spinner fontSizeSpinner; + @FXML private ComboBox fontStyleCombo; + @FXML private ColorPicker textColorPicker; + @FXML private ColorPicker fillColorPicker; + @FXML private ColorPicker strokeColorPicker; + @FXML private Spinner strokeWidthSpinner; + @FXML private Button deleteButton; + private DrawingCanvas canvas; + public PropertyPanel(DrawingCanvas canvas) { this.canvas = canvas; - setPrefWidth(200); - setPadding(new Insets(10)); - setSpacing(8); - setStyle("-fx-border-color: #e0e0e0; -fx-border-width: 1 0 0 0;"); + loadFXML(); + } - // 对象类型标签 - objectTypeLabel = new Label("未选中对象"); - objectTypeLabel.setStyle("-fx-font-size: 14; -fx-font-weight: bold;"); + /** + * 加载FXML文件 + */ + private void loadFXML() { + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("propertyPanel.fxml")); + loader.setRoot(this); + loader.setController(this); + loader.load(); + } catch (IOException e) { + System.err.println("无法加载propertyPanel.fxml文件"); + e.printStackTrace(); + } + } - // 位置标签 - positionLabel = new Label("X: 0, Y: 0"); - - // 文本内容 - Label textLabel = new Label("文本内容:"); - textContentField = new TextField(); - textContentField.setOnKeyReleased(e -> updateTextContent()); - - // 字体选择 - Label fontLabel = new Label("字体:"); - fontFamilyCombo = new ComboBox<>(); - fontFamilyCombo.getItems().addAll("Arial", "Times New Roman", "Courier New", "Verdana", "Georgia"); - fontFamilyCombo.setValue("Arial"); - fontFamilyCombo.setOnAction(e -> updateTextStyle()); - - // 字体大小 - Label sizeLabel = new Label("大小:"); - fontSizeSpinner = new Spinner<>(8.0, 72.0, 16.0, 2.0); - fontSizeSpinner.setEditable(true); - fontSizeSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateTextStyle()); - - // 字体风格 - Label styleLabel = new Label("风格:"); - fontStyleCombo = new ComboBox<>(); - fontStyleCombo.getItems().addAll("NORMAL", "BOLD", "ITALIC", "BOLD_ITALIC"); - fontStyleCombo.setValue("NORMAL"); - fontStyleCombo.setOnAction(e -> updateTextStyle()); - - // 文本颜色 - Label textColorLabel = new Label("文本颜色:"); - textColorPicker = new ColorPicker(Color.BLACK); - textColorPicker.setOnAction(e -> updateTextColor()); - - // 填充颜色 - Label fillColorLabel = new Label("填充颜色:"); - fillColorPicker = new ColorPicker(Color.WHITE); - fillColorPicker.setOnAction(e -> updateFillColor()); - - // 边框颜色 - Label strokeColorLabel = new Label("边框颜色:"); - strokeColorPicker = new ColorPicker(Color.BLACK); - strokeColorPicker.setOnAction(e -> updateStrokeColor()); - - // 边框宽度 - Label strokeWidthLabel = new Label("边框宽度:"); - strokeWidthSpinner = new Spinner<>(1.0, 10.0, 2.0, 1.0); - strokeWidthSpinner.setEditable(true); - strokeWidthSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateStrokeWidth()); - - // 删除按钮 - deleteButton = new Button("删除对象"); - deleteButton.setStyle("-fx-font-size: 12;"); - deleteButton.setOnAction(e -> deleteSelectedObject()); - - // 添加所有控件到面板 - getChildren().addAll( - objectTypeLabel, - new Separator(), - positionLabel, - new Separator(), - textLabel, - textContentField, - fontLabel, - fontFamilyCombo, - sizeLabel, - fontSizeSpinner, - styleLabel, - fontStyleCombo, - textColorLabel, - textColorPicker, - fillColorLabel, - fillColorPicker, - strokeColorLabel, - strokeColorPicker, - strokeWidthLabel, - strokeWidthSpinner, - new Separator(), - deleteButton + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + // 初始化字体列表 + fontFamilyCombo.getItems().addAll( + "Arial", + "Times New Roman", + "Courier New", + "Verdana", + "Georgia" ); + // 初始化风格列表 + fontStyleCombo.getItems().addAll( + "NORMAL", + "BOLD", + "ITALIC", + "BOLD_ITALIC" + ); + + // 设置默认值 + fontFamilyCombo.setValue("Arial"); + fontStyleCombo.setValue("NORMAL"); + textColorPicker.setValue(Color.BLACK); + fillColorPicker.setValue(Color.WHITE); + strokeColorPicker.setValue(Color.BLACK); + + // 添加事件监听 + textContentField.setOnKeyReleased(e -> updateTextContent()); + fontFamilyCombo.setOnAction(e -> updateTextStyle()); + fontSizeSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateTextStyle()); + fontStyleCombo.setOnAction(e -> updateTextStyle()); + textColorPicker.setOnAction(e -> updateTextColor()); + fillColorPicker.setOnAction(e -> updateFillColor()); + strokeColorPicker.setOnAction(e -> updateStrokeColor()); + strokeWidthSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateStrokeWidth()); + deleteButton.setOnAction(e -> deleteSelectedObject()); + // 默认隐藏所有编辑控件 setEditingVisible(false); @@ -174,6 +158,8 @@ public class PropertyPanel extends VBox { fillColorPicker.setDisable(true); strokeColorPicker.setDisable(true); strokeWidthSpinner.setDisable(true); + + deleteButton.setDisable(false); } /** @@ -188,6 +174,8 @@ public class PropertyPanel extends VBox { fillColorPicker.setDisable(false); strokeColorPicker.setDisable(false); strokeWidthSpinner.setDisable(false); + + deleteButton.setDisable(false); } /** @@ -202,6 +190,8 @@ public class PropertyPanel extends VBox { fillColorPicker.setDisable(true); strokeColorPicker.setDisable(true); strokeWidthSpinner.setDisable(true); + + deleteButton.setDisable(false); } /** diff --git a/src/main/resources/dev/bytevibe/hyperpoint/main.fxml b/src/main/resources/dev/bytevibe/hyperpoint/main.fxml index 505b505..88913df 100644 --- a/src/main/resources/dev/bytevibe/hyperpoint/main.fxml +++ b/src/main/resources/dev/bytevibe/hyperpoint/main.fxml @@ -7,16 +7,18 @@ - + - +