From 87ce23ce122860dcb3d046697403c5d0bcda054c Mon Sep 17 00:00:00 2001 From: songdragon Date: Sat, 19 Aug 2023 16:04:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A1=8C=E5=8F=B7=E5=AE=BD=E5=BA=A6?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E8=A1=8C?= =?UTF-8?q?=E5=8F=B7=E8=87=AA=E5=8A=A8=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jcnc/jnotepad/ui/LineNumberTextArea.java | 24 ++++++++++++++++--- src/main/resources/styles.css | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jcnc/jnotepad/ui/LineNumberTextArea.java b/src/main/java/org/jcnc/jnotepad/ui/LineNumberTextArea.java index 60bc198..bdaaa9e 100644 --- a/src/main/java/org/jcnc/jnotepad/ui/LineNumberTextArea.java +++ b/src/main/java/org/jcnc/jnotepad/ui/LineNumberTextArea.java @@ -13,16 +13,18 @@ public class LineNumberTextArea extends BorderPane { private final TextArea mainTextArea; private final TextArea lineNumberArea; + static final int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, + 99999999, 999999999, Integer.MAX_VALUE}; + public LineNumberTextArea() { mainTextArea = new TextArea(); lineNumberArea = new TextArea(); lineNumberArea.setEditable(false); - lineNumberArea.setMinWidth(30); - lineNumberArea.setMaxWidth(30); + lineNumberArea.setPrefWidth(30); // 设置显示滚动条样式类 lineNumberArea.getStyleClass().add("text-line-number"); /*mainTextArea.getStyleClass().add("show-scrollbars"); */ - + lineNumberArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberWidth()); mainTextArea.textProperty().addListener((observable, oldValue, newValue) -> updateLineNumberArea()); // 当主要文本区域的垂直滚动位置发生变化时,使行号文本区域的滚动位置保持一致 @@ -39,6 +41,22 @@ public class LineNumberTextArea extends BorderPane { setLeft(lineNumberArea); } + private void updateLineNumberWidth() { + int numOfLines = mainTextArea.getParagraphs().size(); + int count = 1; + for (int i = 0; i < sizeTable.length; i++) { + if (numOfLines <= sizeTable[i]) { + count = i + 1; + break; + } + } + //单数字宽度10像素,4为padding=左3+右1 + int actualWidth = count * 10 + 4; + if (actualWidth > lineNumberArea.getWidth()) { + lineNumberArea.setPrefWidth(actualWidth); + } + } + public StringProperty textProperty() { return mainTextArea.textProperty(); } diff --git a/src/main/resources/styles.css b/src/main/resources/styles.css index c69dd13..42b923b 100644 --- a/src/main/resources/styles.css +++ b/src/main/resources/styles.css @@ -7,7 +7,7 @@ /* 不显示滚动条 */ .text-line-number .content{ -fx-cursor: text; - -fx-padding: 8px 1px 8px 1px; + -fx-padding: 8px 1px 8px 5px; } .text-line-number .scroll-bar:vertical { -fx-pref-width: 1;