feat: 行号宽度根据最后一个行号自动调整
This commit is contained in:
parent
871da1b9c2
commit
87ce23ce12
@ -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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user