将MouseMotionListener添加到JButton

时间:2017-12-20 13:54:20

标签: java swing mouselistener mousemotionlistener

我希望在一个面板中创建4个名为FBButton的JButtons,可以单击该按钮来更改预设图像并在面板上拖动。截至目前,按钮将从mouseListener单击,但拖动时会出现错误。我已经尝试将布局设置为null,但是当我这样做时,即使我初始化它们的大小和位置,按钮也不会出现在屏幕上。

为实现此目的,我将代码分为三类:FBPlayerFrameFBButtonFBMouseListener。我会尽可能地让每个人都成为最小的屁股,但如果你认为我添加了太多,我会道歉。

以下是FBPlayerFrame

package swing;

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

public class FBPlayerFrame extends JFrame {

    JPanel p = new JPanel();
    FBButton buttons[] = new FBButton[22];
    Dimension dim = new Dimension(52, 52);

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

    public FBPlayerFrame() {
        super("Football Start");
        setSize(400, 400);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        p.setLayout(new DragLayout());

        for (int i = 0; i < 4; i++) {
            buttons[i] = new FBButton();
            buttons[i].setPreferredSize(new Dimension(52, 52));
            buttons[i].setLocation(50, 40 + 60 * i);
            p.add(buttons[i]);
            buttons[i].addMouseMotionListener(new FBMouseListener());
            buttons[i].addMouseListener(new FBMouseListener());

        }   

        add(p);    
        setVisible(true);
    }    
}

以下是FBButton

package swing;

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

@SuppressWarnings("serial")
public class FBButton extends JButton implements ActionListener {
    ImageIcon SN, SL,IN;
    byte value = 0;
    FBMouseListener listener;

    public FBButton() {

        listener = new FBMouseListener();

        SN = new ImageIcon(this.getClass().getResource("square_null.png"));
        SL = new ImageIcon(this.getClass().getResource("square_left.png"));
        IN = new ImageIcon(this.getClass().getResource("invisible.png"));

        addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        value++;
        value %= 3; 

        if (value == 1) {
            setIcon(SN);
        } else if (value == 2) {
            setIcon(SL);
        } else {
            setIcon(IN);
        }    

    }    
}

最后一堂课是FBMouseListener

package swing;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;

public class FBMouseListener extends MouseInputAdapter {
    Point location;
    MouseEvent pressed;

    public void mousePressed(MouseEvent me) {
        pressed = me;
        System.out.println("Found mousePressed() in FBMouseListener()");
    }

    public void mouseDragged(MouseEvent me) {
        System.out.println("Found mouseDragged() in FBMouseListener()");

        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX(); //ERROR HERE
        int y = location.y - pressed.getY() + me.getY();
        component.setLocation(x, y);
    }
}

当我点击并拖动相应的println()时,会出现。拖动带有一个我无法识别的错误。以下是最后一次println()和完整错误。当我点击哪一行时,说它是FBMouseListener中的这一行(在代码中也标有注释)int x = location.x - pressed.getX() + me.getX(); //ERROR HERE

Found mouseDragged() in FBMouseListener()Exception in thread "AWT-EventQueue-0" 
java.lang.NullPointerException
    at swing.FBMouseListener.mouseDragged(FBMouseListener.java:22)
    at java.awt.AWTEventMulticaster.mouseDragged(AWTEventMulticaster.java:320)
    at java.awt.Component.processMouseMotionEvent(Component.java:6581)
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3342)
    at java.awt.Component.processEvent(Component.java:6302)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

提前谢谢你。我一直试图解决这个问题,我很感激能得到的所有帮助。

1 个答案:

答案 0 :(得分:0)

您不应该保持对“已按下”的MouseEvent的引用,而是存储该位置本身。