How do I show a table of multiply in java with JPanel and JFrame form?

0

I want that when I press a button I display a multiplication table but it does not throw me errors and I do not know if it is doing well.

import java.awt.Graphics;
import java.awt.event.ContainerListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author root
 */
public class Panel extends JPanel{
    private int x;
    private JTextArea mostrarMulti;
private int tablas=11;
private int multi = 0;
private JTextField num1;
private String resultado;

public Panel(JTextArea mostrarMulti, JTextField num1) {
    this.mostrarMulti=mostrarMulti;
    this.num1=num1;
}

public void mostrar(){
    x=Integer.parseInt(num1.getText());//convierte el texto ingresado en un valor numerico
    for (int i = 1; i < tablas; i++) {
          multi=x*i;
          //etiqueta.setText(resultado);
          mostrarMulti.append(x+"*"+i"="+multi);
    }
    mostrarMulti.setVisible(true);
}    

}

    
asked by Alex Reyes 02.04.2017 в 04:20
source

1 answer

1

you have your text field

private javax.swing.JTextField txtNumero;

you have your JTextArea where the multiplication table will be displayed

private javax.swing.JTextArea txtMostrarMulti;

we save the number in a variable

int numero = Integer.parseInt(txtNumero.getText());

in this variable we will save the resulting string when doing the calculations.  2x1 = 2  2x2 = 4

String salida = "";

we have the cycle that will allow iterating from 0 to 11 to obtain the multiplication table from 0 to 11

for (int i = 0; i <= 11; i++) {
        int resultado = numero * i;
        salida += numero + "x" + i + "=" + resultado + "\n";

    }

at the end and the output chain has the result so we do the following

txtMostrarMulti.append(salida);

I hope it serves you!

    
answered by 11.04.2017 / 19:54
source