英文:
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("I'm editable");
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("Red");
RadioButton rbBlue = new RadioButton("Blue");
RadioButton rbBlack = new RadioButton("Black");
RadioButton rbOrange = new RadioButton("Orange");
RadioButton rbGreen = new RadioButton("Green");
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 -> 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(paneeli,400, 250);
stage.setTitle("Text");
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 )
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论