Percorrendo Array, moleza essa!

4 respostas
S

Passei uns tempos parados e to enferrujado…

To querendo

private String nomes[] = {"=","0","1","2","3","4","5","6","7","8","9","+","-","*","/"};
private JButton butoes[];

for ( int i = 0; i < nomes.length; i++)
{
   butoes[i] = new JButton( nomes[i] );
   tecladoPanel.add( butoes[i] );
}

assim mostraria os botoes com os nomes na seguinte sequencia… de = até /… to querendo fazer o contrario mostrar da / ate igual .

como teria de colocar meu codigo ae ?

agredeço

4 Respostas

C

tente:

private String nomes[] = {"=","0","1","2","3","4","5","6","7","8","9","+","-","*","/"}; 
private JButton butoes[]; 

for ( int i = 0; i < nomes.length; i++) 
{ 
   butoes[i] = new JButton( nomes[nomes.length-i] ); 
   tecladoPanel.add( butoes[i] ); 
}
R

Fala Kra,

Eu faria assim…

private String nomes[] = {"=","0","1","2","3","4","5","6","7","8","9","+","-","*","/"};
private JButton butoes[];

for ( int i = (nomes.length - 1); i >= 0; i--)
{
   butoes[i] = new JButton( nomes[i] );
   tecladoPanel.add( butoes[i] );
}
S

hehe, ja tentei isso ae… mas ae vai mostrar so o ultimo ou seja so “/” em todos os locais.

EDIT: isso aqui eh valido para o post do amigo Carlos


o Outro deu certo…

Vlw ae galera…

S

tenta ae rato!!
aki funcionou!!!
[b]

import java.awt.event.<em>;

import java.awt.</em>;

import javax.swing.<em>;

public class d

{

static private String nomes[] = {"=",0,1,2,3,4,5,6,7,8,9,"+","-","</em>","/"};

static private JButton butoes[]=new JButton[nomes.length];

public static void main(String args[])
{

JFrame f=new JFrame();

Container c=f.getContentPane();

f.setLayout(new GridLayout(1,nomes.length));
for ( int i = 0; i < nomes.length; i++)

{

butoes[i] = new JButton( nomes[i] );

c.add( butoes[i] );

}

f.setSize(800,400);
f.show();

}
}

[/b]

Criado 10 de março de 2005
Ultima resposta 10 de mar. de 2005
Respostas 4
Participantes 4