Problema com RadioButton e KeyListener

2 respostas
rodrigo.achilles

Olá pessoal,
Estou com um probleminha, quando eu indiro um RadioButton na minha aplicação o KeyListener não funciona, só funciona o SPACE para clicar no RadioButton.
Quando eu tiro, consigo ver tudo certo, a minhas teclas funcionam.
Estou colocando meu trabalho, se alguém puder me ajudar:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
/**
 * @author Rodrgo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Controle extends JFrame implements KeyListener, MouseInputListener
{
	private Container cont;
	private JPanel paineis, painelNorte, painelCentro, painelSul, painelNorte_1, painelNorte_2, painelNorte_3, painelJLabel_1, painelJLabel_2, painelJLabel_3;
	private JLabel JLabel_imgUp, JLabel_imgDown, JLabel_imgLeft, JLabel_imgRight, JLabel_imgCentro;
//	private JRadioButton JRadio_1, JRadio_2, JRadio_3;
//	private ButtonGroup graficosGroup;
	private JSeparator JSeparator_1;
	private ImageIcon imgUp = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaUp.png"));
	private ImageIcon imgDown = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaDown.png"));
	private ImageIcon imgLeft = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaLeft.png"));
	private ImageIcon imgRight = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaRight.png"));
	private ImageIcon imgCentro = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/centro.png"));
	private ImageIcon imgUpClick = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaUp_click.png"));
	private ImageIcon imgDownClick = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaDown_click.png"));
	private ImageIcon imgLeftClick = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaLeft_click.png"));
	private ImageIcon imgRightClick = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/setaRight_click.png"));
	private JLabel espaco1 = new JLabel("");
	private JLabel espaco2 = new JLabel("");
	private JLabel espaco3 = new JLabel("");
	private JLabel espaco4 = new JLabel("");
	private JLabel espaco5 = new JLabel("");
	
	public Controle()
    {
		super("Controle");
		cont = getContentPane();
		
		this.addKeyListener(this);
		
		// Criação do Layout
		paineis = new JPanel();
		paineis.setLayout(new GridLayout( 3, 1 ));
		painelNorte = new JPanel();
		painelNorte.setLayout(new GridLayout( 3, 1 ));
		painelNorte_1 = new JPanel();
		painelNorte_1.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelNorte_2 = new JPanel();
		painelNorte_2.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelNorte_3 = new JPanel();
		painelNorte_3.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelJLabel_1 = new JPanel();
		painelJLabel_1.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelJLabel_2 = new JPanel();
		painelJLabel_2.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelJLabel_3 = new JPanel();
		painelJLabel_3.setLayout(new FlowLayout(FlowLayout.CENTER));
		painelCentro = new JPanel();
		painelCentro.setLayout(new GridLayout( 2, 1 ));
		painelSul = new JPanel();
		painelSul.setLayout(new GridLayout( 3, 3 ));
		
		// Constroi o Painel Norte
/*		JRadio_1 = new JRadioButton("Gráfico Barra", false);
		JRadio_1.setActionCommand("linha");
		JRadio_1.addMouseListener(this);
		JRadio_2 = new JRadioButton("Gráfico Linha", false);
		JRadio_2.setActionCommand("barra");
		JRadio_2.addMouseListener(this);
		JRadio_3 = new JRadioButton("Gráfico Pizza", false);
		JRadio_3.setActionCommand("pizza");
		JRadio_2.addMouseListener(this);
		graficosGroup = new ButtonGroup();
		graficosGroup.add(JRadio_1);
		graficosGroup.add(JRadio_2);
		graficosGroup.add(JRadio_3);
		painelNorte_1.add(JRadio_1);
		painelNorte_2.add(JRadio_2);
		painelNorte_3.add(JRadio_3);
		painelNorte.add(painelNorte_1);
		painelNorte.add(painelNorte_2);
		painelNorte.add(painelNorte_3);
*/		
		// Constroi o Painel Centro	
		JSeparator_1 = new JSeparator();
		painelCentro.add(espaco1);
		painelCentro.add(JSeparator_1);
			
		// Constroi o Painel Sul
		JLabel_imgUp = new JLabel("", imgUp, JLabel.CENTER);
		JLabel_imgUp.addMouseListener(this);
		JLabel_imgLeft = new JLabel("", imgLeft, JLabel.CENTER);
		JLabel_imgLeft.addMouseListener(this);
		JLabel_imgCentro = new JLabel("", imgCentro, JLabel.CENTER);
		JLabel_imgRight = new JLabel("", imgRight, JLabel.CENTER);
		JLabel_imgRight.addMouseListener(this);
		JLabel_imgDown = new JLabel("", imgDown, JLabel.CENTER);
		JLabel_imgDown.addMouseListener(this);
		painelSul.add(espaco2);
		painelSul.add(JLabel_imgUp);
		painelSul.add(espaco3);
		painelSul.add(JLabel_imgLeft);
		painelSul.add(JLabel_imgCentro);
		painelSul.add(JLabel_imgRight);
		painelSul.add(espaco4);
		painelSul.add(JLabel_imgDown);
		painelSul.add(espaco5);
		
		// Adiciona todos os Paineis no Container
		paineis.add(painelNorte);
		paineis.add(painelCentro);
		paineis.add(painelSul);
		cont.add(paineis);
		
		this.setIconImage((new ImageIcon(ClassLoader.getSystemClassLoader().getResource("img/opengl.png"))).getImage());
		this.setContentPane(cont);
		this.pack();
		this.setSize(160, 350);
		this.centerWindow(this);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void centerWindow(Component frame)
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if ( frameSize.width > screenSize.width )
			frameSize.width = screenSize.width;
		if ( frameSize.height > screenSize.height )
			frameSize.height = screenSize.height;
		frame.setLocation ( ((screenSize.width - frameSize.width ) >> 1) + 400, (screenSize.height - frameSize.height) >> 1 );
	}
	public void mouseClicked(MouseEvent me)
	{
		
	}
	public void mousePressed(MouseEvent me)
	{
/*		if ( me.getSource() == JLabel_imgUp )
		{
			JLabel_imgUp.setIcon(imgUpClick);
			if ( JRadio_1.isSelected() )
			{
				// chama a função da barra
			}
			if ( JRadio_2.isSelected() )
			{
				// chama a função da linha
			}
			if ( JRadio_3.isSelected() )
			{
				// chama a função da pizza
			}
		}
		if ( me.getSource() == JLabel_imgLeft )
		{
			JLabel_imgLeft.setIcon(imgLeftClick);
			if ( JRadio_1.isSelected() )
			{
				// chama a função da barra
			}
			if ( JRadio_2.isSelected() )
			{
				// chama a função da linha
			}
			if ( JRadio_3.isSelected() )
			{
				// chama a função da pizza
			}
		}
		if ( me.getSource() == JLabel_imgRight )
		{
			JLabel_imgRight.setIcon(imgRightClick);
			if ( JRadio_1.isSelected() )
			{
				// chama a função da barra
			}
			if ( JRadio_2.isSelected() )
			{
				// chama a função da linha
			}
			if ( JRadio_3.isSelected() )
			{
				// chama a função da pizza
			}
		}
		if ( me.getSource() == JLabel_imgDown )
		{
			JLabel_imgDown.setIcon(imgDownClick);
			if ( JRadio_1.isSelected() )
			{
				// chama a função da barra
			}
			if ( JRadio_2.isSelected() )
			{
				// chama a função da linha
			}
			if ( JRadio_3.isSelected() )
			{
				// chama a função da pizza
			}
		}*/
	}
	public void mouseReleased(MouseEvent me)
	{
		if ( me.getSource() == JLabel_imgUp )
		{
			JLabel_imgUp.setIcon(imgUp);
		}
		if ( me.getSource() == JLabel_imgLeft )
		{
			JLabel_imgLeft.setIcon(imgLeft);
		}
		if ( me.getSource() == JLabel_imgRight )
		{
			JLabel_imgRight.setIcon(imgRight);
		}
		if ( me.getSource() == JLabel_imgDown )
		{
			JLabel_imgDown.setIcon(imgDown);
		}
	}
	public void mouseEntered(MouseEvent me)
	{

	}
	public void mouseExited(MouseEvent me)
	{
		
	}
	public void mouseDragged(MouseEvent me)
	{
		
	}
	public void mouseMoved(MouseEvent me)
	{
		
	}
	public void keyTyped(KeyEvent ke)
	{
		
	}
	public void keyPressed(KeyEvent ke)
	{
		if ( ke.getKeyCode() == KeyEvent.VK_UP )
		{
			JLabel_imgUp.setIcon(imgUpClick);
			System.out.println("Up");
		}
		if ( ke.getKeyCode() == KeyEvent.VK_LEFT )
		{
			JLabel_imgLeft.setIcon(imgLeftClick);
			System.out.println("Left");
		}
		if ( ke.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			JLabel_imgRight.setIcon(imgRightClick);
			System.out.println("Right");
		}
		if ( ke.getKeyCode() == KeyEvent.VK_DOWN )
		{
			JLabel_imgDown.setIcon(imgDownClick);
			System.out.println("Down");
		}
	}
	public void keyReleased(KeyEvent ke)
	{
		if ( ke.getKeyCode() == KeyEvent.VK_UP )
		{
			JLabel_imgUp.setIcon(imgUp);
		}
		if ( ke.getKeyCode() == KeyEvent.VK_LEFT )
		{
			JLabel_imgLeft.setIcon(imgLeft);
		}
		if ( ke.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			JLabel_imgRight.setIcon(imgRight);
		}
		if ( ke.getKeyCode() == KeyEvent.VK_DOWN )
		{
			JLabel_imgDown.setIcon(imgDown);
		}
	}
	public static void main(String[] args)
	{
		Controle controle = new Controle();
	}
}

Obrigado pessoal.
Abraçosss

2 Respostas

hmichel

Pelo q percebi, eh que o radiobutton é um componente o qual mantém foco, diferente dos panels e labels os quais vc tem. Então, qnd vc adiciona-o no form, ele fica dono do foco, e não o tranfere aos componentes os quais tem o listener de key publicados (JFrame). Tente adicionar o keyevent ao radio tambem.

[]'s

rodrigo.achilles

Caraca michel, show
Vc entendeu corretamente, a ainda me ajudou a colocar a porcaria pra funcionar, estava a dois dias martelando.

Valeu mesmo. :slight_smile:

Abraços

Criado 9 de abril de 2005
Ultima resposta 9 de abr. de 2005
Respostas 2
Participantes 2