如何在JavaFX中将可移动文本保持在窗口内?

huangapple go评论60阅读模式
英文:

How to keep a movable text inside the window with JavaFX?

问题

以下是代码的翻译部分:

我有一个带有文本和两个按钮的代码bt1按钮将文本向左移动bt2按钮将文本向右移动问题是我不知道如何防止文本离开窗口换句话说如何将文本保持在窗口的边界内另外如何将文本定位在窗口的中心

以下是相关的代码

```java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import java.io.IOException;

public class Text extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Pane paneText = new Pane();
        Text text = new Text("我是可编辑的");
        paneText.getChildren().addAll(text);

        HBox paneButton = new HBox(10);
        paneButton.setAlignment(Pos.TOP_CENTER);
        Button bt1 = new Button("<");
        Button bt2 = new Button(">");
        paneButton.getChildren().addAll(bt1, bt2);

        HBox paneRadioButton = new HBox(10);
        paneRadioButton.setAlignment(Pos.BASELINE_LEFT);
        RadioButton rbRed = new RadioButton("红色");
        RadioButton rbBlue = new RadioButton("蓝色");
        RadioButton rbBlack = new RadioButton("黑色");
        RadioButton rbOrange = new RadioButton("橙色");
        RadioButton rbGreen = new RadioButton("绿色");

        ToggleGroup gr = new ToggleGroup();
        rbRed.setToggleGroup(gr);
        rbBlue.setToggleGroup(gr);
        rbBlack.setToggleGroup(gr);
        rbOrange.setToggleGroup(gr);
        rbGreen.setToggleGroup(gr);

        paneRadioButton.getChildren().addAll(rbRed, rbBlue, rbBlack, rbOrange, rbGreen);

        BorderPane pane = new BorderPane();
        pane.setCenter(paneText);
        pane.setTop(paneButton);
        pane.setBottom(paneRadioButton);

        bt1.setOnAction(e -> text.setX(text.getX() - 10));
        bt2.setOnAction(e -> text.setX(text.getX() + 10));

        rbRed.setOnAction(e -> text.setFill(Color.RED));
        rbBlue.setOnAction(e -> text.setFill(Color.BLUE));
        rbBlack.setOnAction(e -> text.setFill(Color.BLACK));
        rbOrange.setOnAction(e -> text.setFill(Color.ORANGE));
        rbGreen.setOnAction(e -> text.setFill(Color.GREEN));

        Scene scene = new Scene(pane, 400, 250);
        stage.setTitle("文本");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

希望这有助于您理解代码的功能和结构。

英文:

I have a code with text and 2 buttons; bt1 that moves the text left and bt2 that moves the text right. The problem is that I can't figure out how I can keep the text from leaving the window. So in other words how can I keep the text inside the borders of the window? Also, how can I position the text in the center of the window?

Here is the code in question:

import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Scene;import javafx.stage.Stage;import javafx.geometry.Pos;import javafx.scene.control.Button;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.scene.layout.Pane;import javafx.scene.text.Text;import javafx.scene.paint.Color;import javafx.scene.control.RadioButton;import javafx.scene.control.ToggleGroup;import java.io.IOException;
public class Text extends Application {@Overridepublic void start(Stage stage) throws IOException {
    Pane paneText = new Pane();
Text text = new Text(&quot;I&#39;m editable&quot;);
paneText.getChildren().addAll(text);
HBox paneButton = new HBox(10);
paneButton.setAlignment(Pos.TOP_CENTER);
Button bt1 = new Button(&quot;&lt;&quot;);
Button bt2 = new Button(&quot;&gt;&quot;);
paneButton.getChildren().addAll(bt1,bt2);
HBox paneRadioButton = new HBox(10);
paneRadioButton.setAlignment(Pos.BASELINE_LEFT);
RadioButton rbRed = new RadioButton(&quot;Red&quot;);
RadioButton rbBlue = new RadioButton(&quot;Blue&quot;);
RadioButton rbBlack = new RadioButton(&quot;Black&quot;);
RadioButton rbOrange = new RadioButton(&quot;Orange&quot;);
RadioButton rbGreen = new RadioButton(&quot;Green&quot;);
ToggleGroup gr = new ToggleGroup();
rbRed.setToggleGroup(gr);
rbBlue.setToggleGroup(gr);
rbBlack.setToggleGroup(gr);
rbOrange.setToggleGroup(gr);
rbGreen.setToggleGroup(gr);
paneRadioButton.getChildren().addAll(rbRed,rbBlue,rbBlack,rbOrange,rbGreen);
    BorderPane pane = new BorderPane();
pane.setCenter(paneText);
pane.setTop(paneButtons);
pane.setBottom(paneRadioButtons);
bt1.setOnAction(e -&gt; text.setX(text.getX() - 10));
bt2.setOnAction(e -&gt; text.setX(text.getX() + 10));
rbRed.setOnAction(e -&gt; text.setFill(Color.RED));
rbBlue.setOnAction(e -&gt; text.setFill(Color.BLUE));
rbBlack.setOnAction(e -&gt; text.setFill(Color.BLACK));
rbOrange.setOnAction(e -&gt; text.setFill(Color.ORANGE));
rbGreen.setOnAction(e -&gt; text.setFill(Color.GREEN));
Scene scene = new Scene(paneeli,400, 250);
stage.setTitle(&quot;Text&quot;);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

答案1

得分: 1

有条件的限制翻译

对于左侧很容易。只要是 if(x>0){....} 就可以了。但右侧有点棘手。您需要知道已翻译节点的当前宽度以及其父节点的宽度。在这种方法中,即使父节点被拉伸,条件也能正常工作,但不适用于相反情况(父节点宽度减小)

App.java

public class App extends Application {

    @Override
    public void start(Stage stage) {

        var text = new Text("hola");
        var boundWidth = text.getBoundsInLocal().getWidth();
        var left = new Button("left");
        var right = new Button("right");
        var anchorPane = new AnchorPane(left, right, text);

        left.setOnAction((t) -> {
            if (text.getTranslateX() > 0) {
                text.setTranslateX(text.getTranslateX() - 10);
            }

        });
        right.setOnAction((t) -> {
            if (text.getTranslateX() + 10 <= anchorPane.getWidth() - boundWidth) {
                text.setTranslateX(text.getTranslateX() + 10);

            }
        });


        AnchorPane.setLeftAnchor(left, 10d);
        AnchorPane.setRightAnchor(right, 10d);
        AnchorPane.setTopAnchor(text, 30d);

        var scene = new Scene(anchorPane, 120, 50);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}
英文:

restrinct translation with conditions

For the left side is easy . It's just if(x>0){....} . But the right side is a little bit tricky . You need to know the current width of the translated node and the width of its parent . In this approach the condition works even if the parent is stretched , but is not implemented for the opposite ( parent width is reduced )

如何在JavaFX中将可移动文本保持在窗口内?

App.java

public class App extends Application {
@Override
public void start(Stage stage) {
var text = new Text(&quot;hola&quot;);
var boundWidth = text.getBoundsInLocal().getWidth();
var left = new Button(&quot;left&quot;);
var right = new Button(&quot;right&quot;);
var anchorPane = new AnchorPane(left, right, text);
left.setOnAction((t) -&gt; {
if (text.getTranslateX() &gt; 0) {
text.setTranslateX(text.getTranslateX() - 10);
}
});
right.setOnAction((t) -&gt; {
if (text.getTranslateX() + 10 &lt;= anchorPane.getWidth() - boundWidth) {
text.setTranslateX(text.getTranslateX() + 10);
}
});
AnchorPane.setLeftAnchor(left, 10d);
AnchorPane.setRightAnchor(right, 10d);
AnchorPane.setTopAnchor(text, 30d);
var scene = new Scene(anchorPane, 120, 50);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

huangapple
  • 本文由 发表于 2023年2月26日 21:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572276.html
匿名

发表评论

匿名网友

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

确定