Erro na hora de rodar o programa

5 respostas
J
Esse é a primeira tela do meu programa

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.*;
public class abertura extends JFrame implements ActionListener

{

JTextField txtmat1 = new JTextField();

JTextField txtmat2 = new JTextField();

JLabel lb1 = new JLabel (Numero de Linhas);

JLabel lb2 = new JLabel (Numero de Colunas);

JButton btnok = new JButton(Ok);
public abertura()
{
	JPanel painelbotoes = new JPanel();
	JPanel paineldados = new JPanel();
	
	
	paineldados.setLayout(new GridLayout(2,2));
	
	paineldados.setBackground(new Color(54, 100, 139));
	painelbotoes.setBackground(new Color(54, 100, 139));
	
	lb1.setBackground(new Color(54, 100, 139));
	lb2.setBackground(new Color(54, 100, 139));
	
	getContentPane().setBackground(new Color(54, 100, 139));
	
	paineldados.add(lb1);
	paineldados.add(txtmat1);
	paineldados.add(lb2);
	paineldados.add(txtmat2);
	
	painelbotoes.add(btnok);
	
	getContentPane().add(paineldados, BorderLayout.NORTH);
	getContentPane().add(painelbotoes, BorderLayout.SOUTH);
	
	btnok.addActionListener(this);
	
	
	
	
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setTitle("Escolha o Tamanho da matriz");
	this.setSize(400,200);
	this.setResizable(false);

	setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
	if (event.getSource() == btnok)
	{
		calculocanto cal = new calculocanto(Integer.parseInt(txtmat1.getText()),Integer.parseInt(txtmat2.getText()));
	}
}
public static void main(String[] args) 
{
	new abertura();
}

}

E essa a a segunda tela q é chamada depois que dá Ok na primeira…

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.*;
public class calculocanto extends JFrame implements ActionListener

{

JButton btncalcular = new JButton(Calcular);

JLabel lbresposta = new JLabel (Inserir valores);

int linha,coluna;

int[][] matriz= new int[linha][coluna];

JTextField[][] txtmat = new JTextField[linha][coluna];
public calculocanto(int lin,int col)
{
	
	linha=lin;
	coluna=col;
	
    
		
	JPanel painelbotoes = new JPanel();
	JPanel paineldados = new JPanel();
	JPanel painelsul = new JPanel();
	
		
	painelbotoes.setLayout(new BoxLayout(painelbotoes, BoxLayout.X_AXIS));
	paineldados.setLayout(new GridLayout(lin,col));
	painelsul.setLayout(new BoxLayout(painelsul, BoxLayout.Y_AXIS));
	
	painelsul.setBackground(new Color(54, 100, 139));
	paineldados.setBackground(new Color(54, 100, 139));
	painelbotoes.setBackground(new Color(54, 100, 139));
	lbresposta.setBackground(new Color(54, 100, 139));
			
	painelbotoes.add(btncalcular);
	
	
	
	for (int w=0; w<lin; w++)
	{
  		for(int u=0; u<col; u++) 
  		{
      		txtmat[w][u] = new JTextField();
      		
      		paineldados.add(txtmat[w][u]);
      	}
  	}

	txtmat[0][0].setEnabled(false);
	txtmat[0][0].setText("Ori - Dest");
	txtmat[0][col-1].setEnabled(false);
	txtmat[0][col-1].setText("Oferta");
	txtmat[lin-1][0].setEnabled(false);
	txtmat[lin-1][0].setText("Procura");
	txtmat[lin-1][col-1].setEnabled(false);
	txtmat[0][0].setBackground(new Color(54, 100, 139));
	txtmat[0][col-1].setBackground(new Color(54, 100, 139));
	txtmat[lin-1][0].setBackground(new Color(54, 100, 139));
	txtmat[lin-1][col-1].setBackground(new Color(54, 100, 139));
	
	
			
		
	painelsul.add(paineldados);
	
	
	getContentPane().add(lbresposta, BorderLayout.NORTH);
	getContentPane().add(painelbotoes, BorderLayout.SOUTH);
	getContentPane().add(painelsul, BorderLayout.CENTER);
	
	btncalcular.addActionListener(this);
	
	lbresposta.setHorizontalAlignment(SwingConstants.CENTER);
	
	
	//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setTitle("Canto Noroeste");
	this.setSize(400,200);
	this.setResizable(false);

	setVisible(true);
	//pack();	
}
public void calcula()
{
	for(int i=0;i<linha;i++)
	{
		for(int j=0;j<coluna;j++)
		{
			matriz[i][j]=Integer.parseInt(txtmat[i][j].getText());
		}
	}
	int[][] mat= new int[linha][coluna];
	int lin=1;
	int col=1;
	int z=coluna-2;
	int aux=0;
	
	
	for(int i=0;i<linha;i++)
	{
		for(int j=0;j<coluna;j++)
		{
			mat[i][j]=0;
		}
	}
	
	
	
	
		while(z!=0)
		{
			aux=matriz[linha-1][col]-matriz[lin][coluna-1];
			if((aux<0))
			{
				matriz[lin][coluna-1]=(-1*aux);
				mat[lin][col]=matriz[linha-1][col];
				matriz[linha-1][col]=0;
				col++;
				z--;
			}
			if(aux>0)
			{
				matriz[linha-1][col]=aux;
				mat[lin][col]=matriz[lin][coluna-1];
				matriz[lin][coluna-1]=0;
				lin++;
			}
			if(aux==0)
			{
				mat[lin][col]=matriz[lin][coluna-1];
				matriz[lin][coluna-1]=0;
				matriz[linha-1][col]=0;
				z--;
				lin++;
				col++;
			}
			
			
		}
			for(int k=1;k<linha;k++)
			{
				System.out.println("");
				for(int l=1;l<coluna;l++)
				{
					System.out.print("|"+mat[k][l]+"|");
				}
			}
		
		
		
}

public void actionPerformed(ActionEvent event)
{
	if (event.getSource() == btncalcular)
	{
	calcula();
	}
}
/*public static void main(String[] args) 
{
	new calculocanto();
}*/

}

Ele compila o codigo e não acha nenhum erro… mas na hora de rodar no momento que clico no ok da primeira tela ele o erro que ta na msg do arquivo em anexo… agradeço desde de já…


5 Respostas

P

è cara pelo que eu vi no seu codígo não tem erro de sintaxe mas sim erro de lógico daí não tem, por isso o compilador não acha erro, e o erro que está dando é que você está tentando acessar uma posição de um vetor que não existe na linha 57. Dá uma revisada no seu código e qualuqer coisa posta aí de novo

J

Até se eu colocar o
JTextField[][] txtmat = new JTextField[linha][coluna];
dentro du metedo contrutor ele funciona só que dai eu não consigo pegar os dados que estão nu field porque ele foi declarado só dentro do metodo construtor…

J

jah resolvi o problema obrigadooo :smiley:

P

Seria legal você postar como resolveu o problema também…

J
Desculpe a demora mas eh q tava meio offline hauahauahau

assim resolvi o problema

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.*;
public class calculocanto extends JFrame implements ActionListener

{

JButton btncalcular = new JButton(Calcular);

JLabel lbresposta = new JLabel (Inserir valores);

int linha,coluna;

JTextField[][] txtmat;

int[][] matriz= new int[100][100];
public calculocanto(int lin,int col)
{
	
	linha=lin;
	coluna=col;
	
	 txtmat= new JTextField[lin][col];
    
	JPanel painelbotoes = new JPanel();
	JPanel paineldados = new JPanel();
	JPanel painelsul = new JPanel();
	
		
	painelbotoes.setLayout(new BoxLayout(painelbotoes, BoxLayout.X_AXIS));
	paineldados.setLayout(new GridLayout(lin,col));
	painelsul.setLayout(new BoxLayout(painelsul, BoxLayout.Y_AXIS));
	
	painelsul.setBackground(new Color(54, 100, 139));
	paineldados.setBackground(new Color(54, 100, 139));
	painelbotoes.setBackground(new Color(54, 100, 139));
	lbresposta.setBackground(new Color(54, 100, 139));
			
	painelbotoes.add(btncalcular);
	
	
	
	for (int w=0; w<lin; w++)
	{
  		for(int u=0; u<col; u++) 
  		{
      		txtmat[w][u] = new JTextField();
      		
      		paineldados.add(txtmat[w][u]);
      	}
  	}

	txtmat[0][0].setEnabled(false);
	txtmat[0][0].setText("Ori - Dest");
	txtmat[0][col-1].setEnabled(false);
	txtmat[0][col-1].setText("Oferta");
	txtmat[lin-1][0].setEnabled(false);
	txtmat[lin-1][0].setText("Procura");
	txtmat[lin-1][col-1].setEnabled(false);
	txtmat[0][0].setBackground(new Color(54, 100, 139));
	txtmat[0][col-1].setBackground(new Color(54, 100, 139));
	txtmat[lin-1][0].setBackground(new Color(54, 100, 139));
	txtmat[lin-1][col-1].setBackground(new Color(54, 100, 139));
	
	
			
		
	painelsul.add(paineldados);
	
	
	getContentPane().add(lbresposta, BorderLayout.NORTH);
	getContentPane().add(painelbotoes, BorderLayout.SOUTH);
	getContentPane().add(painelsul, BorderLayout.CENTER);
	getContentPane().setBackground(new Color(54, 100, 139));
	
	btncalcular.addActionListener(this);
	
	lbresposta.setHorizontalAlignment(SwingConstants.CENTER);
	
	
	//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setTitle("Canto Noroeste");
	this.setSize(400,200);
	this.setResizable(false);

	setVisible(true);
	//pack();	
}
public void calcula()
{
	int[][] mat= new int[linha][coluna];
	int lin=1;
	int col=1;
	int z=coluna-2;
	int aux=0;
	String resp="(";
	for(int i=0;i<linha;i++)
	{
		for(int j=0;j<coluna;j++)
		{
			
			//if(((i==0)&&(j==0))||((i==0)&&(j==coluna-1))||((i==linha-1)&&(j==0))||((i==linha-1)&&(j==coluna-1)))
			if((i==0)||(j==0)||(i==linha-1&&j==coluna-1))
			{
				System.out.println("teste");
				matriz[i][j]=0;
			}
			else
			{
			matriz[i][j]=Integer.parseInt(txtmat[i][j].getText());
			}
		}
	}
			
	System.out.println("teste2");
	for(int i=0;i<linha;i++)
	{
		for(int j=0;j<coluna;j++)
		{
			mat[i][j]=0;
		}
	}
	
	
	
	
		while(z!=0)
		{
			aux=matriz[linha-1][col]-matriz[lin][coluna-1];
			if((aux<0))
			{
				
				matriz[lin][coluna-1]=(-1*aux);
				mat[lin][col]=matriz[linha-1][col];
				matriz[linha-1][col]=0;
				col++;
				z--;
			}
			if(aux>0)
			{
				matriz[linha-1][col]=aux;
				mat[lin][col]=matriz[lin][coluna-1];
				matriz[lin][coluna-1]=0;
				lin++;
			}
			if(aux==0)
			{
				mat[lin][col]=matriz[lin][coluna-1];
				matriz[lin][coluna-1]=0;
				matriz[linha-1][col]=0;
				z--;
				lin++;
				col++;
			}
			
			
		}
			for(int k=1;k<linha;k++)
			{
				System.out.println("");
				for(int l=1;l<coluna;l++)
				{
					if(k!=linha-1)
					{
						System.out.print("|"+mat[k][l]+"|");
						resp=resp+mat[k][l]+",";
					}
					
				}
			}
		
		lbresposta.setText("Matriz Resposta "+resp+")");
		
}

public void actionPerformed(ActionEvent event)
{
	if (event.getSource() == btncalcular)
	{
	calcula();
	}
}
/*public static void main(String[] args) 
{
	new calculocanto();
}*/

}

ele jah setando zero nu vetor de inicio por isso dava erro na matriz :slight_smile:

Criado 4 de novembro de 2006
Ultima resposta 7 de nov. de 2006
Respostas 5
Participantes 2