jeudi 7 mai 2020

How to make a randomized 2D Array depending on multiple variables?

I have code right now that takes user input to customize the the amount of rows and columns of a JTextArea, I want to use the same variables to make a 2d array with random values filling it, ranging from 0 to 2, I tried creating a nested for loop but ultimately failed. I can't find anything online that answers how to do this. Any help or advice appreciated. Below is all I have right now without anything to do this.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main {

    int tf1r = 0;
    int tf2c = 0;
    String space = ", ";
    JPanel panel;
    JLabel label;
    JTextField tf1, tf2;
    JButton set, reset;
    JTextArea ta;


    public static void main(String[] args) {

        // Frame
        JFrame frame = new JFrame("Mapped Array");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setResizable(false);

        // Menu Bar
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("EXPORT");
        JMenu m2 = new JMenu("HELP");
        mb.add(m1);
        mb.add(m2);
        JMenuItem m11 = new JMenuItem("TXT");
        JMenuItem m22 = new JMenuItem("BAT");
        m1.add(m11);
        m1.add(m22);

        // Panel Bottom and Components
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Enter Rows and Columns");
        JTextField tf1 = new JTextField(10);
        JTextField tf2 = new JTextField(10);
        JButton set = new JButton("SET");
        JButton reset = new JButton("RESET");
        panel.add(label);
        panel.add(tf1);
        panel.add(tf2);
        panel.add(set);
        panel.add(reset);

        // Center TextArea
        JTextArea ta = new JTextArea();

        // Components to Frame
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.setVisible(true);

        //Sets Text
        set.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String space = ", ";
                String rows = tf1.getText();
                String columns = tf2.getText();
                int tf1r = Integer.parseInt(rows);
                int tf2c = Integer.parseInt(columns);

                ta.setRows(tf1r);
                ta.setColumns(tf2c);



                System.out.println("rows: "+tf1r+" columns: "+tf2c+" TaR: "+ta.getRows()+" TaC: "+ta.getColumns());

                ta.setText(rows+space+columns+"\n100, 100\n");
            }
        }); 

        //Resets Text
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ta.setText("");
            }
        }); 
    }
}



Aucun commentaire:

Enregistrer un commentaire