Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么MouseMotionListener不工作?_Java_Swing_Awt_Mouselistener - Fatal编程技术网

Java 为什么MouseMotionListener不工作?

Java 为什么MouseMotionListener不工作?,java,swing,awt,mouselistener,Java,Swing,Awt,Mouselistener,我实现了MouseMotionListener,也实现了MouseListener。我在constructornewContentPane.addMouseListener(this)中添加了 newContentPane.addMouseMotionListener(this); 这还不够吗? 像mouseDragged(MouseEvent e)mouseMoved(MouseEvent e)这样的方法不会被调用 newContentPane.addMouseMotionListener(

我实现了MouseMotionListener,也实现了MouseListener。我在constructor
newContentPane.addMouseListener(this)
中添加了

newContentPane.addMouseMotionListener(this);
这还不够吗? 像
mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e)
这样的方法不会被调用

newContentPane.addMouseMotionListener(this);
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

public class BorderFrame extends JFrame implements MouseListener,MouseMotionListener{
    private static final long serialVersionUID = 1L;
    private JButton northButton;
    private JButton southButton;
    private JButton westButton;
    private JButton eastButton;
    private JButton centerButton1;
    private JButton centerButton2;
    private JPanel newContentPane;
    private JSplitPane splitPane1;
    private JSplitPane splitPane2;

    public BorderFrame(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newContentPane = new JPanel(new BorderLayout());
        northButton = new JButton("North");
        southButton = new JButton("South");
        westButton = new JButton("West");
        eastButton = new JButton("East");
        centerButton1 = new JButton("Center1");
        centerButton2 = new JButton("Center2");
        newContentPane.add(northButton, "North");
        newContentPane.add(southButton, BorderLayout.SOUTH);
        newContentPane.add(westButton, BorderLayout.WEST);
        newContentPane.add(eastButton, BorderLayout.EAST);
        // newContentPane.add(centerButton, BorderLayout.CENTER);
        splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerButton1,
                centerButton2);
        splitPane1.setResizeWeight(0.5);
        splitPane1.setOneTouchExpandable(true);
        splitPane1.setContinuousLayout(true);
        newContentPane.add(splitPane1, BorderLayout.CENTER);

        add(newContentPane);
        newContentPane.addMouseListener(this);
        newContentPane.addMouseMotionListener(this);
    }

    protected static void createAndShowGUI() {
        final BorderFrame borderFrame = new BorderFrame("Test BorderLayout");
        borderFrame.pack();
        borderFrame.setLocationRelativeTo(null);
        borderFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }


//AUTOGENERATED methods
....
}

您需要实现MouseMotionListener和MouseListener接口的方法。Tere将有来自这两个接口的总共7个方法需要重写。用你想要处理的事件的方法编写代码。但您需要覆盖所有这些方法

newContentPane.addMouseMotionListener(this);
public void mouseMoved(MouseEvent e) {
   //Code what you want to do
}

public void mouseDragged(MouseEvent e) {
   //Code what you want to do
}

 public void mousePressed(MouseEvent e) {
   //Code what you want to do
}

public void mouseReleased(MouseEvent e) {
   //Code what you want to do
}

public void mouseEntered(MouseEvent e) {
   //Code what you want to do
}

public void mouseExited(MouseEvent e) {
   //Code what you want to do
}

public void mouseClicked(MouseEvent e) {
   //Code what you want to do
}

如果您的框架仅包含newContentPane(其中包含其他组件),则可以尝试将侦听器添加到框架:

newContentPane.addMouseMotionListener(this);
...
add(newContentPane);
addMouseListener(this);
addMouseMotionListener(this);
}
...

您的布局中完全充满了截取鼠标事件的按钮。将其中一个替换为标签以查看差异

newContentPane.addMouseMotionListener(this);
newContentPane.add(new JLabel("North"), BorderLayout.NORTH);

也考虑使用和覆盖您要使用的方法。

newContentPane.addMouseMotionListener(this);
MouseHandler handler = new MouseHandler();
newContentPane.addMouseListener(handler);
newContentPane.addMouseMotionListener(handler);
…
private static class MouseHandler extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println(e);
    }
}

BorderFrame.java:12:error:BorderFrame不是抽象的,并且不会覆盖MouseListener中的抽象方法mouseExited(MouseEvent)
Post一个编译过的东西,用于一个运行时问题。我实现了它,但只是没有将它附加到此示例以节省空间,例如@override public void mouseMoved(MouseEvent e){System.out.printf(“鼠标%d%d\n”,e.getX(),e.getY());}您已为此事件注册了newCOntentPane。在此面板中使用鼠标进行操作应执行相应的方法。请确保您仅在
newCOntentPane
中移动鼠标。您正在将
splitPane1
添加到
newCOntentPane
。我怀疑您正在该面板中移动鼠标。请尝试注册
splitPane1
用于这些事件。我添加splitPane1.addMouseMotionListener(此),但仅当splitPanel最大程度地位于左侧或右侧时才起作用,而当我调整按钮大小时则不起作用基本上,布局组织的控件不是您期望的方式。您的
newContentPane
也不会显示。它隐藏在某些控件下。组织您的UI并确保所有内容都显示出来。