<small id='TT5pU'></small><noframes id='TT5pU'>

    <bdo id='TT5pU'></bdo><ul id='TT5pU'></ul>

    <legend id='TT5pU'><style id='TT5pU'><dir id='TT5pU'><q id='TT5pU'></q></dir></style></legend>

    1. <tfoot id='TT5pU'></tfoot>
      <i id='TT5pU'><tr id='TT5pU'><dt id='TT5pU'><q id='TT5pU'><span id='TT5pU'><b id='TT5pU'><form id='TT5pU'><ins id='TT5pU'></ins><ul id='TT5pU'></ul><sub id='TT5pU'></sub></form><legend id='TT5pU'></legend><bdo id='TT5pU'><pre id='TT5pU'><center id='TT5pU'></center></pre></bdo></b><th id='TT5pU'></th></span></q></dt></tr></i><div id='TT5pU'><tfoot id='TT5pU'></tfoot><dl id='TT5pU'><fieldset id='TT5pU'></fieldset></dl></div>

      没有任何事件发生

      时间:2023-09-30

          <tbody id='6CKdd'></tbody>
        <i id='6CKdd'><tr id='6CKdd'><dt id='6CKdd'><q id='6CKdd'><span id='6CKdd'><b id='6CKdd'><form id='6CKdd'><ins id='6CKdd'></ins><ul id='6CKdd'></ul><sub id='6CKdd'></sub></form><legend id='6CKdd'></legend><bdo id='6CKdd'><pre id='6CKdd'><center id='6CKdd'></center></pre></bdo></b><th id='6CKdd'></th></span></q></dt></tr></i><div id='6CKdd'><tfoot id='6CKdd'></tfoot><dl id='6CKdd'><fieldset id='6CKdd'></fieldset></dl></div>

          <bdo id='6CKdd'></bdo><ul id='6CKdd'></ul>

          <small id='6CKdd'></small><noframes id='6CKdd'>

          • <legend id='6CKdd'><style id='6CKdd'><dir id='6CKdd'><q id='6CKdd'></q></dir></style></legend><tfoot id='6CKdd'></tfoot>

              • 本文介绍了没有任何事件发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                This is my code.I am trying to make the ball move using arrow keys. As i run the above program ball is not displayed (if i change the coordinates to like 0,30 ball is displayed) and event is not fired ,ball neither moves nor jumps

                什么问题?

                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();
                  }
                }
                

                推荐答案

                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();
                  }
                }
                

                这篇关于没有任何事件发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将事件转发到所有组件 JAVA 下一篇:Java - 如何更改“本地"?事件侦听器中的变量

                相关文章

                最新文章

                    <bdo id='u2sxs'></bdo><ul id='u2sxs'></ul>
                  <legend id='u2sxs'><style id='u2sxs'><dir id='u2sxs'><q id='u2sxs'></q></dir></style></legend><tfoot id='u2sxs'></tfoot>
                1. <i id='u2sxs'><tr id='u2sxs'><dt id='u2sxs'><q id='u2sxs'><span id='u2sxs'><b id='u2sxs'><form id='u2sxs'><ins id='u2sxs'></ins><ul id='u2sxs'></ul><sub id='u2sxs'></sub></form><legend id='u2sxs'></legend><bdo id='u2sxs'><pre id='u2sxs'><center id='u2sxs'></center></pre></bdo></b><th id='u2sxs'></th></span></q></dt></tr></i><div id='u2sxs'><tfoot id='u2sxs'></tfoot><dl id='u2sxs'><fieldset id='u2sxs'></fieldset></dl></div>

                    <small id='u2sxs'></small><noframes id='u2sxs'>