没有事件发生

时间:2011-06-17 06:50:09

标签: java swing user-interface graphics event-handling

这是我的代码。我正在尝试使用箭头键移动球。 当我运行上面的程序球没有显示如果我将坐标改为0,30球显示)                            且事件未被触发,球既不移动也不跳跃

What is the problem

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

class ControlledBall extends JPanel{

int diameter = 30;
int height = 30;
int x_Pos = 0;
int y_Pos;

ControlledBall() {
  JFrame fr = new JFrame("Controlled Ball");
  this.setBackground(Color.black);
  fr.add(this);
  fr.setVisible(true);
  fr.setSize(600,400);
  y_Pos = this.getHeight() - diameter ;
  register();  
  repaint();
}

public void register() {
  this.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent ke) {
       if( ke.getKeyCode() == ke.VK_RIGHT ) {
           increment();
       }
   else  if( ke.getKeyCode() == ke.VK_LEFT ) {
           decrement();
       }
       else if( ke.getKeyCode() == ke.VK_UP) {
           jump();
       }
    }
 });
}

public void paintComponent(Graphics g) {
 super.paintComponent(g);
 g.setColor( Color.blue );
 g.fillOval( x_Pos , y_Pos , diameter , height );
 System.out.println("testing...");
}  

public void increment() {
 x_Pos++;
  if( x_Pos > (this.getWidth() - diameter) ) {
    x_Pos = this.getWidth() - diameter;
  }
 repaint();
}

public void decrement() {
 x_Pos--;
  if( x_Pos <= 0) {
    x_Pos = 0;
  }
 repaint();
}

public void jump() {
 y_Pos++;
  if( y_Pos <=0 ) {
   y_Pos = 0;
 }
repaint();
}

  public static void main( String args[] ) {
    new ControlledBall();
  }
}

3 个答案:

答案 0 :(得分:2)

请使用以下代码替换代码,您将获得主JFrame已使用KeyEvent的真实情况。

为确保获得所有这些活动,您不必在组件上注册,而是使用KeyboardFocusManager进行注册,这将会收到发生的重要事件。

ControlledBall() {
        JFrame fr = new JFrame("Controlled Ball");
        this.setBackground(Color.black);
        this.setBorder( BorderFactory.createLineBorder( Color.white ) );
        fr.add(this);
        fr.setSize(600,400);
        y_Pos = this.getHeight() - diameter ;
        register();  
        fr.setVisible(true);
        repaint();
        fr.addKeyListener( new KeyAdapter() 
        {   
            public void keyPressed(KeyEvent e) {
                System.out.println("I am here.....");
            }
        });
}

答案 1 :(得分:2)

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

class ControlledBall extends JPanel{

int diameter = 30;
int height = 30;
int x_Pos = 0;
int y_Pos;

ControlledBall() {
  JFrame fr = new JFrame("Controlled Ball");
  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setBackground(Color.red);    
  setPreferredSize(new Dimension(400,300));
  fr.add(this);
  fr.setVisible(true);
  // important!
  fr.pack();
  //fr.setSize(600,400);
  y_Pos = this.getHeight()/2;
  register();
  repaint();
}

public void register() {
    // very important!
    setFocusable(true);
  this.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent ke) {
       if( ke.getKeyCode() == ke.VK_RIGHT ) {
           increment();
       }
   else  if( ke.getKeyCode() == ke.VK_LEFT ) {
           decrement();
       }
       else if( ke.getKeyCode() == ke.VK_UP) {
           jump();
       }
    }
 });
}

public void paintComponent(Graphics g) {
 super.paintComponent(g);
 g.setColor( Color.orange );
 g.fillOval( x_Pos , y_Pos , diameter , height );
}

public void increment() {
 x_Pos++;
  if( x_Pos > (this.getWidth() - diameter) ) {
    x_Pos = this.getWidth() - diameter;
  }
 repaint();
}

public void decrement() {
 x_Pos--;
  if( x_Pos <= 0) {
    x_Pos = 0;
  }
 repaint();
}

public void jump() {
 y_Pos++;
  if( y_Pos <=0 ) {
   y_Pos = 0;
 }
repaint();
}

  public static void main( String args[] ) {
    new ControlledBall();
  }
}

答案 2 :(得分:0)

尝试使用此构造函数并相应地修改jump, incrementdecrement逻辑! 您需要将侦听器添加到框架而不是面板。

int x_Pos = 30; int y_Pos = 15;

ControlledBall() {
    JFrame fr = new JFrame("Controlled Ball");
    this.setBackground(Color.black);
    fr.add(this);
    fr.setVisible(true);
    fr.setSize(600, 400);
    fr.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent ke) {
            if (ke.getKeyCode() == ke.VK_RIGHT) {
                System.out.println("Right Key");
                increment();
            } else if (ke.getKeyCode() == ke.VK_LEFT) {
                System.out.println("Left Key");
                decrement();
            } else if (ke.getKeyCode() == ke.VK_UP) {
                System.out.println("Up Key");
                jump();
            }
        }
    });
    y_Pos = this.getHeight() - diameter;
    //register();
    repaint();
}