Problemas com o JRadioButton

1 resposta
P

Olá pessoal, esotu tendo dificuldade em fazer aparecer as minhas imagens quando seleciono uma opção do JRadioButton. Alguém pode me ajudar??? Abaixo posto o código. Obrigada :).

public class estudando extends JFrame implements ActionListener{

public JRadioButton b1;

public JRadioButton b2;

public JRadioButton b3;

public JRadioButton b4;

public JRadioButton b5;

public JRadioButton b6;

public JRadioButton b7;

public JRadioButton b8;

public JRadioButton b9;

public JLabel la;
public estudando() {
super("estudando");
JPanel p = new JPanel(new GridLayout(3,3));

JRadioButton r1 = new JRadioButton ("cachorro");
JRadioButton r2 = new JRadioButton ("Gato" );
JRadioButton r3 = new JRadioButton ("Papagaio");
JRadioButton r4 = new JRadioButton ("Tucano");
JRadioButton r5 = new JRadioButton ("Baleia");
JRadioButton r6 = new JRadioButton ("Pato");
JRadioButton r7 = new JRadioButton ("Ganso");
JRadioButton r8 = new JRadioButton ("Arara");
JRadioButton r9 = new JRadioButton ("Cabra");
//ActionListener a = new tratadorevento();

r1.addActionListener(this);

r2.addActionListener(this);

r3.addActionListener(this);

r4.addActionListener(this);

r5.addActionListener(this);

r6.addActionListener(this);

r7.addActionListener(this);

r8.addActionListener(this);

r9.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
bg.add(r5);
bg.add(r6);
bg.add(r7);
bg.add(r8);
bg.add(r9);


p.add(r1);
p.add(r2);
p.add(r3);
p.add(r4);
p.add(r5);
p.add(r6);
p.add(r7);
p.add(r8);
p.add(r9);

Container c = getContentPane();
c.add(p,BorderLayout.SOUTH);

JLabel imagem = new JLabel();
p.add(imagem);
c.add(imagem,BorderLayout.CENTER);
imagem.setPreferredSize(new Dimension(100,100));//espaço onde a imagem será visualizada
imagem.setHorizontalAlignment(SwingConstants.CENTER);//determinam a locação do ícone em relação ao texto.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,400);
setVisible(true);

}
//class tratadorevento implements ActionListener{

public void actionPerformed (ActionEvent e) {
		JRadioButton rb = (JRadioButton)e.getSource();
	    ImageIcon ícone = new ImageIcon(rb.getActionCommand()+ "bicho.jpg");
		imagem.setIcon(ícone);

	}
//}

public static void main (String args []) {

new estudando();

} }

1 Resposta

kaial
Se este codigo nao estiver dando nenhuma excecao do tipo filenotfoundException, entao provavelmente o problema pode ser a atualizacao da janela, entao basta colocar o seguinte metodo :

public void paint(Graphics g){

super.paintComponents(g);

}

e a cada acao que houver nos radioButtons colocar o seguinte comando : estudando.this.repaint();

Espero ter ajudado!!

Criado 24 de setembro de 2012
Ultima resposta 24 de set. de 2012
Respostas 1
Participantes 2