如何在java中的3个类之间进行通信

时间:2015-11-08 15:24:47

标签: java swing keyboard

我做了一个简单的orogram,使用swing在屏幕上制作一个球,然后我有按钮让它移动:leftrightup,{{1 },down和一些显示坐标和速度的标签。

现在我想用键盘上的按键使它工作,但为此我需要再做一个类。

以下是3个班级:

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

change velocity

这是面板的代码:

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

这是键盘列表器

使用的keyAction
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类编辑P的变量,在面板类中Punto Decleared的一个实例,所以我可以用键移动点?

1 个答案:

答案 0 :(得分:1)

显然,你的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));