Vous êtes sur la page 1sur 9

package frmmatrices;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.table.DefaultTableModel;
public class FrmMatrices extends javax.swing.JFrame {

// Variables declaration - do not modify
private javax.swing.JButton btnCaracol;
private javax.swing.JButton btnLimpiar;
private javax.swing.JButton btnAceptar;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblMatriz;
private javax.swing.JTextField txtNumero;
// End of variables declaration

//////////CONSTRUCTOR DE LA CLASE////////////////////////////
public FrmMatrices() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">

/////////METODO QUE LLAMA EL CONSTRUCTOR DE LA CLASE/////
private void initComponents() {

btnLimpiar = new javax.swing.JButton();
btnCaracol = new javax.swing.JButton();

btnAceptar = new javax.swing.JButton();


txtNumero = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tblMatriz = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

//////ActionListener DE EL CARACOL////////////
btnCaracol.setText("CARACOL");
btnCaracol.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCaracolActionPerformed(evt);
}
});

//////ActionListener de el boton LIMPIAR////////////
btnLimpiar.setText("LIMPIAR");
btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCaracolActionPerformed(evt);
}
});

//////ActionListener de el boton ACEPTAR////////////
btnAceptar.setText("ACEPTAR");
btnAceptar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAceptarActionPerformed(evt);
}

});
tblMatriz.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(tblMatriz);
jLabel1.setText("Tamao de la Matriz");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGro
up()
.addContainerGap(15, Short.MAX_VALUE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, java


wing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 86,
.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAceptar)
.addComponent(btnCaracol)

.addComponent(btnLimpiar)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELI
.addComponent(btnAceptar)
.addComponent(btnCaracol)
.addComponent(btnLimpiar)

.addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.sw


upLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, java


wing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnCaracolActionPerformed(java.awt.event.ActionEvent evt) {
int n = Integer.parseInt(txtNumero.getText());
int [][] matriz = new int[n][n];
matriz = this.matrizLimpiallena(n);
this.mostrarMatrizlimpia(matriz,n);

}


private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {
int n = Integer.parseInt(txtNumero.getText());
int [][] matriz = new int[n][n];
matriz = this.matrizLimpiallena(n);
this.mostrarMatrizlimpia(matriz,n);

}
private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int n = Integer.parseInt(txtNumero.getText());
int [][] matriz = new int[n][n];
matriz = this.matrizLlena(n);
this.mostrarMatriz(matriz,n);

}


//////////METODOS PARA EL BOTON ACEPTAR//////////////////////////////
public void mostrarMatriz(int matriz[][], int n){
DefaultTableModel model = (DefaultTableModel) tblMatriz.getModel();
model.setRowCount(n);
model.setColumnCount (n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tblMatriz.setValueAt(matriz[i][j],i,j);
}
}

}

private int[][] matrizLlena(int n) {
int[][] matriz = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

matriz[i][j]=1;
}
}
return matriz;
}


///////////////METODOS BOTON LIMPIAR//////////////////////////////////
public void mostrarMatrizlimpia(int matriz[][], int n){
DefaultTableModel model = (DefaultTableModel) tblMatriz.getModel();
model.setRowCount(n);
model.setColumnCount (n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tblMatriz.setValueAt(matriz[i][j],i,j);
}
}
}

public int[][] matrizLimpiallena(int n) {


int[][] matriz = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

matriz[i][j]=0;
}
}
return matriz;
}


///////////////METODOS BOTON LIMPIAR//////////////////////////////////
public void mostrarMatrizencaracol(int matriz[][], int n){
DefaultTableModel model = (DefaultTableModel) tblMatriz.getModel();
model.setRowCount(n);
model.setColumnCount (n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
tblMatriz.setValueAt(matriz[i][j],i,j);
}
}
}

public int[][] matrizcaracolllena(int n) {
int[][] matriz = new int[n][n];

for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

matriz[i][j]=0;
}
}
return matriz;
}

//////////////METODO PRINCIPAL///////////////////////////////////
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optio
nal) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default l
ook and feel.
* For details seehttp://download.oracle.com/javase/tutorial/uiswing/lookandfeel/pl
af.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInsta
lledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmMatrices.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {


java.util.logging.Logger.getLogger(FrmMatrices.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmMatrices.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmMatrices.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrmMatrices().setVisible(true);
}
});
}

}

Vous aimerez peut-être aussi