我实现 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
....
}
答案 0 :(得分:1)
您的布局完全填充了拦截鼠标事件的按钮。用标签替换一个以查看差异。
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);
}
}
答案 1 :(得分:0)
您需要实现MouseMotionListener和MouseListener接口的方法。 Tere将是您需要覆盖的这两个接口的总共7种方法。在那些方法中编写要处理哪些事件的代码。但是你需要覆盖所有这些方法。
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
}
答案 2 :(得分:0)
如果你的框架只包含newContentPane(包含其他组件),你可以尝试将监听器添加到框架中:
...
add(newContentPane);
addMouseListener(this);
addMouseMotionListener(this);
}
...