KeyListener em inner class...?

2 respostas
B

O seguinte códifgo não apresenta nenhum erro, mas quando eu executo o resultado não é o esperado, o KeyListener não faz aparecerem linhas…
Gostaria que alguém me explicasse melhor como isso funciona.

Obrigado!!

:???:

Obs.: Desculpem a falta de organização do código…

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class desenho_besta extends JFrame implements ActionListener {
	public static desenho_besta besta;
	public static pega_botao botao;
	private static JPanel status;
	private static Container container;
	public desenho_besta(){
		 super("desenho_besta");

		addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
 		 int x, y, sx, sy;
  		x = 500;
  		y = 600;
  		x += 30;
  		y += 60;
  		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  		sx = d.width;
  		sy = d.height;
  		setSize(x, y);
 		setLocation((sx - x) / 2, (sy - y) / 2);
  		setResizable(false);
  		container = getContentPane();
  		container.setLayout(null);
	  status = new JPanel(null);
 	 status.setSize(d.width, d.height);
 	 status.setLocation(20 + d.width, 20);
	 setVisible(true);
	}
	public int z = 1;
	  public void paint(Graphics g) {
	 	g.setColor(Color.black);
		g.drawLine(0, 5, 8, 20);
		g.drawLine(z, 460, z, 460); // (Xo,Yo, X, Y) 
		z+=40;
                }
	public static void main(String args[]) {
		besta = new desenho_besta();
		botao = new pega_botao();
	}
	static class pega_botao implements KeyListener {
	 public int z = 460;
	 public int desenhando = 1;
	 public void keyPressed(KeyEvent e){
	 	if (e.getKeyCode() == KeyEvent.VK_ENTER){
			desenhando = 2;
		}
	 }
	public void pintar(Graphics g) {
	 if (desenhando == 2){	
		g.setColor(Color.black);
		g.drawLine(0, 5, 8, 20);
		g.drawLine(z, 460, z, 460); // (Xo,Yo, X, Y) 
		z+=40;
	}
               }
	public void keyTyped(KeyEvent e){}
	  public void keyReleased(KeyEvent e){
	  }
	}
	 public void actionPerformed(ActionEvent e) {

    }
}

2 Respostas

R

vc não chamou o método pintar no button click…

B

Inicialmente eu tinha que acrescentar isso:

pega_botao evento = new pega_botao();
addKeyListener(evento);

Isso já fez o KeyListener funcionar :wink: , mas ainda não executa a função pintar (eu não a chamo em lugar nenhum, pretendia chamar do

if (e.getKeyCode() == KeyEvent.VK_ENTER){

Mas obrigado!

Criado 16 de novembro de 2004
Ultima resposta 18 de nov. de 2004
Respostas 2
Participantes 2