如何在JavaFX中将按钮和文本垂直显示。

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

How do I display buttons and text under eachother with JavaFX

问题

以下是您提供的代码的翻译部分:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage primaryStage) {

        // 按钮

        VBox vboxBtn = new VBox();
        vboxBtn.setPadding(new Insets(10, 10, 10, 10));
        vboxBtn.setSpacing(10);

        Button btnRoom = new Button("切换房间");
        Button btnMap = new Button("切换地图");

        vboxBtn.getChildren().addAll(btnRoom, btnMap);


        // 文本

        VBox vboxTxt = new VBox();
        vboxTxt.setPadding(new Insets(10, 10, 10, 10));
        vboxTxt.setSpacing(10);

        Text txtRoom = new Text("房间");
        Text txtMap = new Text("地图");

        vboxTxt.getChildren().addAll(btnRoom, btnMap);

        String currentRoom = "无";
        String currentMap = "无";

        // 切换地图的按钮
        btnMap.setText("切换地图");
        // 按钮点击事件
        btnMap.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("你好,世界");
            }
        });

        // 地图文本
        txtMap.setText("地图:" + currentMap);

        // 切换房间的按钮
        btnRoom.setText("切换房间");
        // 按钮点击事件
        btnRoom.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("你好,世界");
            }
        });

        // 房间文本
        txtRoom.setText("房间:" + currentRoom);

        StackPane root = new StackPane();
        root.getChildren().add(vboxBtn);
        root.getChildren().add(vboxTxt);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("你好,世界!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

请注意,为了更好地显示中文,我将一些文本进行了调整。如果您对翻译有任何疑问,请随时提问。

英文:

I have been trying to display buttons and some text with javafx but it whont work it just displays over eachother in the middle of the screen:

如何在JavaFX中将按钮和文本垂直显示。

I have been trying to use vbox and ive also tried to use hbox but both dont change how it looks when i run it

(when i run it i get no erors)
(also im using vs code if that has anything to say)

Here is my code so far:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
// buttons
VBox vboxBtn = new VBox();
vboxBtn.setPadding(new Insets(10, 10, 10, 10));
vboxBtn.setSpacing(10);
Button btnRoom = new Button(&quot;changeRoom&quot;);
Button btnMap = new Button(&quot;changeMap&quot;);
vboxBtn.getChildren().addAll(btnRoom, btnMap);
// text
VBox vboxTxt = new VBox();
vboxTxt.setPadding(new Insets(10, 10, 10, 10));
vboxTxt.setSpacing(10);
Text txtRoom = new Text(&quot;Room&quot;);
Text txtMap = new Text(&quot;Map&quot;);
vboxTxt.getChildren().addAll(btnRoom, btnMap);
String currentRoom = &quot;None&quot;;
String currentMap = &quot;None&quot;;
// button for switching maps
btnMap.setText(&quot;Change the map&quot;);
// button click
btnMap.setOnAction(new EventHandler&lt;ActionEvent&gt;(){
@Override
public void handle(ActionEvent event) {
System.out.println(&quot;Hello world&quot;);
}
});
// text for maps
txtMap.setText(&quot;Map: &quot; + currentMap);
// button for switching rooms
btnRoom.setText(&quot;Hello world&quot;);
// button click
btnRoom.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
@Override
public void handle(ActionEvent event) {
System.out.println(&quot;Hello world&quot;);
}
});
// text
txtRoom.setText(&quot;Room: &quot; + currentRoom);
StackPane root = new StackPane();
root.getChildren().add(btnRoom);
root.getChildren().add(btnMap);
root.getChildren().add(txtRoom);
root.getChildren().add(txtMap);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle(&quot;Hello World!&quot;);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

答案1

得分: 1

我认为问题出在你向面板添加了按钮和文本框,而不是垂直框和水平框。

我相信你想要它们在一个4x4的网格上,为此你需要将虚拟框添加到水平框中,然后将该水平框添加到一个面板中,或者你可以使用一个网格。

或者你可以设置它们的位置,我不知道这是否在代码中可行,但你可以在ScreenBuilder中做到这一点。

附注:检查一下JavaFX的ScreenBuilder。

英文:

I think the problem is you are adding butons and txts to the pane instead of Vboxes and Hboxes.

I believe you want them on a 4x4 grid, to do that you need to add your virtual boxes to a hbox then add that hbox to a pane, or u could use a grid.

Or you could set their locations, I dont know if this is possible with code, but you can do that in screenbuilder.

PS: check out screenbuilder for javafx.

huangapple
  • 本文由 发表于 2020年10月22日 03:11:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64470196.html
匿名

发表评论

匿名网友

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

确定