javafx 中的控制台输出和 gui 文本字段?

console output & gui textfield in javafx?

我是 javafx 的新程序员。我正在构建一个名为模糊计算器的桌面应用程序。每当我 运行 这个程序时,它都会打开一个用户界面,但是当单击界面中的按钮(Max-Min)时,我发现我的输出在控制台而不是文本字段中。如何在我创建的文本字段中获得所需的输出???我已经浪费了太多时间来寻找解决方案,但 none 对我来说是可行的或清楚的。

我在下面附上我的代码以征求你的建议..

N.B:由于我的应用程序正在开发中,目前所有按钮都不起作用,只有 Max-min 按钮处于活动状态。

Main.java

 import javafx.scene.Parent;
 import javafx.scene.Scene;
 import javafx.stage.Stage;

 public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Fuzzy Calculator");
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
}


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

Controller.java

package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.util.Arrays;
import java.util.Scanner;
public class Controller {
@FXML
private Button add;

@FXML
private Button maxmin;

@FXML
private Button maxavg;

@FXML
private Button sub;

@FXML
private TextField display;

@FXML
private Button maxprod;

@FXML
private Button minmax;

@FXML
 void maxmin(ActionEvent event) {

   System.out.println("Array size:");
   Scanner input=new Scanner(System.in);
   int n=input.nextInt();
   System.out.println("1st input ");
   int a[][]=new int [n][n];
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<n;j++)
       {
           a[i][j]=input.nextInt();
       }
   }
   System.out.println("2nd input ");
   int b[][]=new int [n][n];

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<n;j++)
       {
           b[i][j]=input.nextInt();
       }
   }
   int [] c=new int [n];
   int [] k=new int [n];
   int t=0;
   for(int i=0;i<n;i++)
   {
       t=0;
       for(int j=0;j<n;j++ )
       {

           if(a[i][j]<b[j][i])
           {
               k[t]=a[i][j];
               t++;
           }
           else
           {
               k[t]=b[j][i];
               t++;
           }
       }
       Arrays.sort(k);
       c[i]=k[n-1];
   }
   System.out.println("the output is :");
   for(int i=0;i<n;i++)
   {
       System.out.print(" "+c[i]);
   }

}
}

sample.fxml

 <?xml version="1.0" encoding="UTF-8"?>
 <?import javafx.scene.text.*?>
 <?import javafx.scene.control.*?>
 <?import java.lang.*?>
 <?import javafx.scene.layout.*?>
 <?import javafx.geometry.Insets?>
 <?import javafx.scene.layout.GridPane?>
 <?import javafx.scene.control.Button?>
 <?import javafx.scene.control.Label?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
  <SplitPane dividerPositions="0.5" layoutX="102.0" layoutY="51.0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
    <items>
      <AnchorPane maxHeight="100.0" prefHeight="80.0">
           <children>
              <TextField fx:id="display" layoutX="150.0" layoutY="42.0" prefHeight="100.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                 <font>
                    <Font size="15.0" />
                 </font>
              </TextField>
           </children>
        </AnchorPane>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="230.0" prefWidth="598.0">
           <children>
              <GridPane layoutY="6.0" prefHeight="267.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                </columnConstraints>
                <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                </rowConstraints>
                 <children>
                    <Button fx:id="maxmin" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#maxmin" prefHeight="134.0" prefWidth="199.0" text="Max-Min">
                       <font>
                          <Font size="28.0" />
                       </font>
                    </Button>
                    <Button fx:id="maxprod" alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Max_Product" GridPane.columnIndex="1">
                       <font>
                          <Font size="28.0" />
                       </font>
                    </Button>
                    <Button fx:id="maxavg" alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Max_Average" GridPane.columnIndex="2">
                       <font>
                          <Font size="27.0" />
                       </font>
                    </Button>
                    <Button fx:id="add" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Addition" GridPane.rowIndex="1">
                       <font>
                          <Font size="28.0" />
                       </font>
                    </Button>
                    <Button fx:id="sub" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Subtraction" GridPane.columnIndex="1" GridPane.rowIndex="1">
                       <font>
                          <Font size="28.0" />
                       </font>
                    </Button>
                    <Button fx:id="minmax" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="134.0" prefWidth="199.0" text="Min-Max" GridPane.columnIndex="2" GridPane.rowIndex="1">
                       <font>
                          <Font name="System Bold" size="28.0" />
                       </font>
                    </Button>
                 </children>
              </GridPane>
           </children>
        </AnchorPane>
    </items>
  </SplitPane>
  </children>
 </AnchorPane>

你的代码看起来一切正常,它按照你写的做

System.out.println("the output is :");

将打印到控制台,因为这是标准输出

你说你想打印到文本字段中,所以就这样写吧:

display.setText(display.getText()+"new text, with its value : + 65);

setText()是决定哪个文字会出现在

getText()是取回已经输入的文本,允许做拼接

因为如果你只使用 setText(); 你会得到这样的东西:

display.setText("log n°1 ...");

log n°1

display.setText("log n°2 ...");

log n°2

并且您想添加文本而不是替换它:

display.setText("log n°1 ...\n");

log n°1

display.setText(display.getText() + "log n°2 ...");

log n°1

log n°2


但这是从另一个线程更新 UI。 UI 只能从 UI 线程更新。为此,您必须这样做

Platform.runLater(() -> {
         display.setText(display.getText() + "log n°2 ...");
});

它告诉 UI 线程执行任务 ;)

始终使用您用于输出的文本框或标签 class 的 settext 方法,而不是 System.out.printin()