如何实现元素的水平拉伸

huangapple 未分类评论51阅读模式
标题翻译

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(&quot;Hello World&quot;);
        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 -&gt; {
            if (mouseEvent.getSceneX() &lt; pane1.getWidth() + 9 &amp;&amp; mouseEvent.getSceneX() &gt; (pane1.getWidth() - 6)) {
                if (getCursor() != Cursor.H_RESIZE)
                    setCursor(Cursor.H_RESIZE);
            } else if (getCursor() != Cursor.DEFAULT)
                setCursor(Cursor.DEFAULT);
        });
        setOnMouseReleased(mouseEvent -&gt; {
            if (getCursor() == Cursor.H_RESIZE &amp;&amp; !(mouseEvent.getSceneX() &lt; pane1.getWidth() + 9 &amp;&amp; mouseEvent.getSceneX() &gt; (pane1.getWidth() - 6)))
                setCursor(Cursor.DEFAULT);
        });
        setOnMousePressed(mouseEvent -&gt; {
            if (getCursor() == Cursor.H_RESIZE)
                mx = mouseEvent.getSceneX() - (pane1.getWidth());
        });
        setOnMouseDragged(mouseEvent -&gt; {
            if (getCursor() == Cursor.H_RESIZE) {
                double newPane1Width = mouseEvent.getSceneX() - mx;
                double newPane2Width = pane2.getWidth() - newPane1Width + pane1.getWidth();
                pane1.setPrefWidth(newPane1Width);
                pane2.setPrefWidth(newPane2Width);
            }
        });
    }
}

huangapple
  • 本文由 发表于 2020年5月31日 00:27:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/62105424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定