refactor: 重构了布局
This commit is contained in:
@@ -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<String> fontFamilyCombo;
|
||||
@FXML
|
||||
private Spinner<Double> fontSizeSpinner;
|
||||
@FXML
|
||||
private ComboBox<String> fontStyleCombo;
|
||||
@FXML
|
||||
private ColorPicker textColorPicker;
|
||||
@FXML
|
||||
private ColorPicker fillColorPicker;
|
||||
@FXML
|
||||
private ColorPicker strokeColorPicker;
|
||||
@FXML
|
||||
private Spinner<Double> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user