gpt4 book ai didi

java - 为什么 MouseMotionListener 不起作用?

转载 作者:行者123 更新时间:2023-12-01 14:06:23 25 4
gpt4 key购买 nike

我实现了MouseMotionListener,也实现了MouseListener。我在构造函数 newContentPane.addMouseListener(this)

中添加
newContentPane.addMouseMotionListener(this);

这还不够吗?不会调用 mouseDragged(MouseEvent e)、 mouseMoved(MouseEvent e) 等方法。

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

最佳答案

您的布局完全充满了拦截鼠标事件的按钮。用标签替换一个以查看差异。

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

还可以考虑使用MouseAdapter并仅覆盖您想要使用的方法。

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

关于java - 为什么 MouseMotionListener 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com