public static int print() {
int a =1;
int b =2;
int c= a+b;
return c;
}
public static void main(String[] args) {
System.out.println(print());
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Label l = new Label();
Pane p = new Pane(l);
p.getChildren().add(l);
Scene s = new Scene(p);
primaryStage.setScene(s);
primaryStage.show();
}
我想知道如何让print()
方法在javafx阶段打印,这只是一个简单的例子,所以情况是写3
不允许在标签中使用 new Label("3")
!
最佳答案
您可以定义一个整数,然后将其发送到 String.valueOf() 方法。
或者您可以在String.valueOf(print())中调用打印方法。 这两种解决方案是相同的。
下面的代码是潜在解决方案的示例。
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
int number = print();
Label l = new Label(String.valueOf(number));
StackPane p = new StackPane();
p.getChildren().add(l);
Scene scene = new Scene(p, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public int print() {
int a = 1;
int b = 2;
int c = a + b;
return c;
}
}
关于javafx:make stage 打印另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495807/