在javafx中创建随机二进制矩阵

时间:2018-04-25 02:05:56

标签: matrix javafx binary

我必须创建一个JavaFX程序,它将创建一个1和0的随机矩阵。 我必须创建不同的方法,例如主方法,在程序开始时问候用户的方法,以及创建和显示矩阵的方法。这是我第一次创建JavaFX程序...请帮忙

这是我的代码,但我需要帮助打印出二进制数字......

    import java.awt.GridLayout;
    import java.util.Random;

    import javax.swing.JTextArea;

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import java.awt.TextArea;

    public class MatrixJavaFX extends Application
    {    
       Button[][] matrix; 

       public void start(Stage primaryStage) throws Exception 
       {
           int size = 10;
           int length = size;
           int width = size;

           GridPane root = new GridPane();

           matrix = new Button[width][length];
           for (int y = 0; y < length; y++)
           {
              for (int x = 0; x < width; x++)
              {
                   Random random = new Random();
                   int random1 = random.nextInt(2);
                   TextField text = new TextField();
                   text.setPrefHeight(50);
                   text.setPrefWidth(50);
                   text.setAlignment(Pos.CENTER);

                   root.setRowIndex(text, y);
                   root.setColumnIndex(text, x);
                   root.getChildren().add(text);
              }
           }
           Scene scene = new Scene(root, 500, 500);
           primaryStage.setTitle("Welcome to the Matrix!");
           primaryStage.setScene(scene);
           primaryStage.show();
       }
       private void add(JTextArea area) 
       {
        JTextArea area1 = new JTextArea(); 
        area1.setText(Integer.toString((int)Math.round(Math.random())));
        area1.setEditable(false);
        this.add(area1);    
       }
      public void MatrixSwing() 
      {
      this.setLayout(new GridLayout(10,10)); // makes the 10 x 10 grid 
      for (int i = 0; i < 100; i++)
      {
          this.add(new JTextArea()); // puts all 100 numbers in place
      }
    }
    private void setLayout(GridLayout gridLayout) {

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

1 个答案:

答案 0 :(得分:0)

问题(数字未显示的原因)非常简单 - 您忘了将它们添加到&#39;文字&#39;:

int random1 = random.nextInt(2);
TextField text = new TextField(); // text is not initialized - Blank!

解决方案:

int random1 = random.nextInt(2);
TextField text = new TextField(Integer.toString(random1)); // pass a string with random1

结果如下:

Solution