codice:import javax.swing.*; import java.awt.*; public class SommaMatrici { int [][]m1; int [][]m2; int [][]ris; JFrame f=new JFrame("Griglia Matrice 1"); JPanel p=new JPanel(); JFrame f1=new JFrame("Griglia Matrice 2"); JPanel p1=new JPanel(); JFrame f2=new JFrame("Griglia Somma Matrici"); JPanel p2=new JPanel(); public SommaMatrici (int r, int c) { m1= new int [r][c]; p.setLayout(new GridLayout(r,c,2,2)); m2= new int [r][c]; p1.setLayout(new GridLayout(r,c,2,2)); ris= new int [r][c]; p2.setLayout(new GridLayout(r,c,2,2)); } public void caricamento1(int r,int c) { String input; for (int i=0;i<r;i++) for (int j=0;j<c;j++) { input=JOptionPane.showInputDialog("Matrice 1","Inserisci l'elemento"+i+"-"+j); m1[i][j]=Integer.parseInt(input); JButton cella=new JButton(input); p.add(cella); f.setVisible(true); } } public void caricamento2(int r,int c) { String input; for (int i=0;i<r;i++) for (int j=0;j<c;j++) { input=JOptionPane.showInputDialog("Matrice 2","Inserisci l'elemento"+i+"-"+j); m2[i][j]=Integer.parseInt(input); JButton cella=new JButton(input); p1.add(cella); f1.setVisible(true); } } public void somma() { ris= new int [3][3]; for (int i=0;i<3;i++) for (int j=0;j<3;j++) { ris[i][j] = m1[i][j] + m2[i][j]; } } public static void main(String[] args) { // TODO Auto-generated method stub String input=JOptionPane.showInputDialog("Somma di due matrici","Quante righe?"); int r=Integer.parseInt(input); input=JOptionPane.showInputDialog("Somma di due matrici","Quante colonne?"); int c=Integer.parseInt(input); SommaMatrici matrice=new SommaMatrici(r,c); matrice.f.setSize(300, 300); matrice.f.setLocation(100, 100); matrice.f.getContentPane().add(matrice.p); matrice.caricamento1(r,c); matrice.f1.setSize(300, 300); matrice.f1.setLocation(400, 100); matrice.f1.getContentPane().add(matrice.p1); matrice.caricamento2(r,c); matrice.f2.setSize(300, 300); matrice.f2.setLocation(700, 100); matrice.f2.getContentPane().add(matrice.p2); matrice.somma(); } }