标题翻译
How to make horizontal stretching of elements
问题
不知道怎么称呼我的问题。我需要使得我可以像这样更改水平“列表”中元素的长度:
[![введите сюда описание изображения][1]][1]
[1]: https://i.stack.imgur.com/85UlC.png
我已经实现了,但是方法非常笨拙。请告诉我如何更好地做到这一点,例如,以便可以轻松地为多个(3个或更多)元素编写代码。也许有一些内置的 JavaFX 方法可以做到这一点。
我的代码:
*Main*:
```java
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane1 = new Pane();
pane1.setPrefWidth(100);
pane1.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
Pane pane2 = new Pane();
HBox.setHgrow(pane2, Priority.ALWAYS);
pane2.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
MainPane root = new MainPane(pane1, pane2);
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainPane:
public final class MainPane extends HBox {
private double mx;
public MainPane(Pane pane1, Pane pane2) {
getChildren().addAll(pane1, pane2);
setOnMouseMoved(mouseEvent -> {
if (mouseEvent.getSceneX() < pane1.getWidth() + 9 && mouseEvent.getSceneX() > (pane1.getWidth() - 6)) {
if (getCursor() != Cursor.H_RESIZE)
setCursor(Cursor.H_RESIZE);
} else if (getCursor() != Cursor.DEFAULT)
setCursor(Cursor.DEFAULT);
});
setOnMouseReleased(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE && !(mouseEvent.getSceneX() < pane1.getWidth() + 9 && mouseEvent.getSceneX() > (pane1.getWidth() - 6)))
setCursor(Cursor.DEFAULT);
});
setOnMousePressed(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE)
mx = mouseEvent.getSceneX() - (pane1.getWidth());
});
setOnMouseDragged(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE) {
double newPane1Width = mouseEvent.getSceneX() - mx;
double newPane2Width = pane2.getWidth() - newPane1Width + pane1.getWidth();
pane1.setPrefWidth(newPane1Width);
pane2.setPrefWidth(newPane2Width);
}
});
}
}
英文翻译
I don't know what to call my problem. I need to make it so that I can change the length of the elements in the horizontal "list" like this:
I did it, but in a very clumsy way. Please tell me how to do this better, for example, so that you can easily write code for several (3 or more) elements. Maybe there is some built-in JavaFX way to do this.
My code:
Main:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane1 = new Pane();
pane1.setPrefWidth(100);
pane1.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
Pane pane2 = new Pane();
HBox.setHgrow(pane2, Priority.ALWAYS);
pane2.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
MainPane root = new MainPane(pane1, pane2);
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainPane:
public final class MainPane extends HBox {
private double mx;
public MainPane(Pane pane1, Pane pane2) {
getChildren().addAll(pane1, pane2);
setOnMouseMoved(mouseEvent -> {
if (mouseEvent.getSceneX() < pane1.getWidth() + 9 && mouseEvent.getSceneX() > (pane1.getWidth() - 6)) {
if (getCursor() != Cursor.H_RESIZE)
setCursor(Cursor.H_RESIZE);
} else if (getCursor() != Cursor.DEFAULT)
setCursor(Cursor.DEFAULT);
});
setOnMouseReleased(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE && !(mouseEvent.getSceneX() < pane1.getWidth() + 9 && mouseEvent.getSceneX() > (pane1.getWidth() - 6)))
setCursor(Cursor.DEFAULT);
});
setOnMousePressed(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE)
mx = mouseEvent.getSceneX() - (pane1.getWidth());
});
setOnMouseDragged(mouseEvent -> {
if (getCursor() == Cursor.H_RESIZE) {
double newPane1Width = mouseEvent.getSceneX() - mx;
double newPane2Width = pane2.getWidth() - newPane1Width + pane1.getWidth();
pane1.setPrefWidth(newPane1Width);
pane2.setPrefWidth(newPane2Width);
}
});
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论