I'm trying to show a 2D matrix when fill two text fields (rows and columns) but I don't know how to print the 2D matrix when this two text field are filled and the "Draw" button is pushed.
This is my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class InputOutput extends JFrame{
JTextField mtf;
JTextField ntf;
JButton button;
public InputOutput(){
setLayout (new FlowLayout ());
mtf = new JTextField(10);
ntf = new JTextField(10);
add(mtf);
add(ntf);
button = new JButton("Dibujar");
add(button);
event e = new event();
button.addActionListener(e);
}
public class Cuadricula extends JComponent{
final int height = 30;
final int width = 30;
int posicionx=0;
int posiciony=0;
public Cuadricula(int x,int y){
setBounds(x,y,width+1, height+1);
}
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
g.drawRect(0,0,width,height);
}
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
String Sm = mtf.getText();
String Sn = ntf.getText();
int m = Integer.parseInt(Sm);
int n = Integer.parseInt(Sn);
Cuadricula casillas[] = new Cuadricula[m*n];
int i,j;
int k = 0;
while (k < m*n){
for(i=0; i < m; i++){
for(j=0; j < n; j++){
casillas[k] = new Cuadricula(i*30,j*30);
k++;
}
}
}
//HERE IS WHAT I DON'T KNOW HOW TO DO
for(int i = 0; i < 16; i++){
gui.getContentPane().add(casillas[i]);
}
}
}
public static void main(String[] args){
InputOutput gui = new InputOutput();
gui.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
gui.setSize(300,150);
gui.setTitle("Writer");
gui.setVisible(true);
gui.getContentPane().setLayout(null);
}
}