控制台输出& javafx中的gui textfield?

时间:2017-06-28 08:44:30

标签: java javafx textfield scenebuilder

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

我在下面附上我的代码以供您提出建议..

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>

2 个答案:

答案 0 :(得分:1)

你的代码看起来都很正常,它做你写的

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线程执行任务;)

答案 1 :(得分:0)

始终使用您要用于输出的Textbox或Label类的settext方法代替System.out.printin()