java - java中3个类之间的通信

标签 java swing keyboard

我制作了一个简单的 orogram,它使用 swing 在屏幕上制作了一个球,然后我有按钮让它移动:left,right, up,down,change 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 的变量,面板类中 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));

关于java - java中3个类之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595582/

相关文章:

java - 如何从 (ddmmyyyy HH :mm:ss) to (yyyy-MM-dd HH:mm:ss. SSS) 转换日期?)

java - 没有输入时扫描仪卡住程序

java - JSF 中的 session 超时

java - 根据字符串的宽度绘制动态按钮

java - 我可以使用循环来同时水平和垂直移动吗?

android - TextView 中的 OnWrite

Android 自定义键盘更改背景颜色键按下

java - 可限时运行

java - removeAll() 后无法添加到 JFrame

java - 有没有办法在 Java Swing 中存储同时单击的每个标签的名称?