我已经制作了一个简单的Orogram,可以使用挥杆在屏幕上使球变成球形,然后我有一些按钮可以使其移动:leftrightupdownchange velocity和一些显示坐标的标签和速度。

现在,我想使其也可以与键盘上的键一起使用,但是为此,我需要进行另一堂课。

这是3类:

Punto.java(屏幕上显示的点):

public class Punto {
    int x , y, v;

    public Punto(int a, int b) {
        this.x=a;
        this.y=b;
        this.v=1;
    }

    public void moveLeft(){
        this.x-=v;
    }

    public void  moveRight(){
        this.x+=v;
    }

    public void  moveUp(){
        this.y-=v;
    }

    public void  moveDown(){
        this.y+=v;
    }

    public void cambiaVelocita(){
        switch(v){
            case 1: v = 2;
                    break;
            case 2: v = 4;
                    break;
            case 4: v = 8;
                    break;
            case 8: v = 1;
                    break;
        }
    }

    public int getX(){
        return this.x;
    }

    public int getY(){
        return this.y;
    }

    public int getV(){
        return this.v;
    }
}


这是面板的代码:

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

public class MyPanel extends JPanel implements ActionListener {

    private Punto p;

    ActionEvent Event;

    String vkLeft = "VK_LEFT";
    String vkRight = "VK_RIGHT";
    String vkup = "VK_UP";
    String vkdown = "VK_DOWN";

    private JLabel l;
    private JButton b;
    private JButton bb;
    private JButton bbb;
    private JButton bbbb;
    private JButton bbbbb;

    public MyPanel(){
        p = new Punto(50,50);
        l = new JLabel("Coordinate");

        setKeyBindings();

        b = new JButton("Left");
        b.addActionListener(this);
        bb = new JButton("Right");
        bb.addActionListener(this);
        bbb = new JButton("Up");
        bbb.addActionListener(this);
        bbbb = new JButton("Down");
        bbbb.addActionListener(this);
        bbbbb = new JButton("Velocita");
        bbbbb.addActionListener(this);

        this.add(l);
        this.add(b);
        this.add(bb);
        this.add(bbb);
        this.add(bbbb);
        this.add(bbbbb);

   }

   private void setKeyBindings() {
      ActionMap actionMap = getActionMap();
      int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = getInputMap(condition );


      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), vkLeft);
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), vkRight);
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkup);
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkdown);

      actionMap.put("VK_LEFT", new KeyAction(vkLeft));
      actionMap.put("VK_RIGHT", new KeyAction(vkRight));
      actionMap.put("VK_UP", new KeyAction(vkup));
      actionMap.put("VK_DOWN", new KeyAction(vkdown));

   }



    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.l.setText( " Coordinate x:" + Integer.toString(p.getX()) +  " y:"  + Integer.toString(p.getY())+  "   Velocita:"  + Integer.toString(p.getV())) ;
        g.setColor(Color.red);
        g.fillOval(p.getX(),p.getY(),10,10);
//        g.drawString(Integer.toString(p.getX()) +  " "  + Integer.toString(p.getY()),50,60);
    }





    public void actionPerformed(ActionEvent e){
        Object action = e.getSource();
        if(action == b)
            p.moveLeft();
        if(action == bb)
            p.moveRight();
        if(action == bbb)
            p.moveUp();
        if(action == bbbb)
            p.moveDown();
        if(action == bbbbb)
            p.cambiaVelocita();
        this.repaint();

    }

}


这是键盘列表器使用的keyAction

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

public class KeyAction extends AbstractAction {
      public KeyAction(String actionCommand) {
         putValue(ACTION_COMMAND_KEY, actionCommand);
      }

      @Override
      public void actionPerformed(ActionEvent actionEvt) {
         if(actionEvt.getActionCommand().equals("VK_LEFT"))
            System.out.println("Left");
         if(actionEvt.getActionCommand().equals("VK_RIGHT"))
            System.out.println("right");
         if(actionEvt.getActionCommand().equals("VK_UP"))
            System.out.println("up");
         if(actionEvt.getActionCommand().equals("VK_DOWN"))
            System.out.println("down");
      }
   }


现在我的问题是:如何从KeyAction类中编辑面板类中P的变量P(Punto Decleared的实例),以便可以使用键移动该点?

最佳答案

显然,您的KeyAction需要了解周围的知识。考虑一下:

public class KeyAction extends AbstractAction {
    Punto p;
    Component c;
    public KeyAction(Punto p, Component c, String actionCommand) {
       this.p = p;
       this.c = c;
       putValue(ACTION_COMMAND_KEY, actionCommand);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvt) {
       if(actionEvt.getActionCommand().equals("VK_LEFT"))
           p.moveLeft();
       if(actionEvt.getActionCommand().equals("VK_RIGHT"))
           p.moveRight();
       if(actionEvt.getActionCommand().equals("VK_UP"))
          p.moveUp();
       if(actionEvt.getActionCommand().equals("VK_DOWN"))
          p.moveDown();

       c.repaint();
    }
 }


然后更改代码以创建您的操作,如下所示:

  actionMap.put("VK_LEFT", new KeyAction(p, this, vkLeft));
  actionMap.put("VK_RIGHT", new KeyAction(p, this, vkRight));
  actionMap.put("VK_UP", new KeyAction(p, this, vkup));
  actionMap.put("VK_DOWN", new KeyAction(p, this, vkdown));

08-04 01:31