简体   繁体   English

Javafx:如何从一个 class 调用另一个方法?

[英]Javafx: How do I call an method from one class to another?

I am new to programming.我是编程新手。 My problem right now is that I want to ask the user to give his name in a TextField, and use that value(name) in another BoardGame class.我现在的问题是我想要求用户在 TextField 中提供他的名字,并在另一个 BoardGame class 中使用该值(名称)。 for example, first window to ask his name, and second window to use it in the game.例如,第一个 window 问他的名字,第二个 window 在游戏中使用它。

My View class (first window)我的观点class(第一个窗口)

public class View extends BorderPane {

public TextArea player1;
public Label name1;

public View(){
    super();
    initializeNodes();
    layoutNodes();
}

public void initializeNodes() {
    player1 = new TextArea();
    name1 = new Label ("Player 1: ");

}
public void layoutNodes() {
    player1.setPrefSize(100,20);
    naam1.setPrefSize(60,40);

    HBox hBox = new HBox(name1,player1);
    TextField nameField;
    nameField = new TextField();
    nameField.setPrefHeight(70);
    player1.getText();
}
EventHandler<ActionEvent> e = new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
        player1.setText(name1.getText());
    }
};
public TextArea getPlayer1() {
    return player1;
}

My BoardGame class(second window):我的BoardGame类(第二个窗口):

public class Boardgame extends BorderPane{
private Label lblPlayer1;
private View view;


public Boardgame(){
    super();
    initializeNodes();
    layoutNodes();
}

private void initializeNodes() {
    super.setPrefSize(900,700);
    lblPlayer1 = new Label("Player 1");
    

}

private void layoutNodes() {
    VBox vBox = new VBox(lblPlayer1);
}

Only thing that works is where I get "Player 1" instead of the name, because I put it hardcoded in there, but how can I make it their input(name)?唯一可行的是我得到“玩家 1”而不是名字,因为我把它硬编码在那里,但我怎样才能让它成为他们的输入(名字)?

I recommend you use the SceneBuilder, it will show you all available FX Components, and make it easier for you to design the UI.我推荐你使用 SceneBuilder,它会显示所有可用的 FX 组件,让你更容易设计 UI。

It needs some research since you will need a controller etc (Example here: http://tutorials.jenkov.com/javafx/fxml.html )它需要一些研究,因为您将需要 controller 等(此处示例: http://tutorials.jenkov.com/javafx/fxml.ZFC35FDC70D5FC69D2698Z83A822C7A53E

However, if you want a way of your user being able to input, Java FX offers the Alert Dialog.但是,如果您希望用户能够输入,Java FX 提供了警报对话框。

You can see a good example of its usage here: https://code.makery.ch/blog/javafx-dialogs-official/你可以在这里看到一个很好的例子: https://code.makery.ch/blog/javafx-dialogs-official/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM