我有一个JSplitPane,它的连续布局已打开。拖动分隔线时,如何防止其他组件接收鼠标事件?
public class Test {
public static void main(String[] args) throws Exception {
JButton top = new JButton("top");
top.setRolloverEnabled(true);
top.setMinimumSize(new Dimension(100, 100));
top.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
top.setBackground(Color.green);
}
@Override
public void mouseExited(MouseEvent e) {
top.setBackground(null);
}
});
JButton bottom = new JButton("bottom");
bottom.setRolloverEnabled(true);
bottom.setMinimumSize(new Dimension(100, 100));
bottom.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
bottom.setBackground(Color.green);
}
@Override
public void mouseExited(MouseEvent e) {
bottom.setBackground(null);
}
});
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setResizeWeight(0.5);
split.setContinuousLayout(true);
split.setTopComponent(top);
split.setBottomComponent(bottom);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(split, BorderLayout.CENTER);
f.setVisible(true);
}
}
答案 0 :(得分:1)
在拖动分隔线时,如何防止其他组件接收鼠标事件?
不知道一种关闭所有事件的方法。
但是我猜您真正担心的是您不希望背景发生变化。
如果是这样,则可以向MouseListener添加异常逻辑,以在按下鼠标按钮时忽略mouseEntered事件:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
JButton top = new JButton("top");
top.setRolloverEnabled(true);
top.setMinimumSize(new Dimension(100, 100));
top.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) return;
top.setBackground(Color.green);
}
@Override
public void mouseExited(MouseEvent e) {
top.setBackground(null);
}
});
JButton bottom = new JButton("bottom");
bottom.setRolloverEnabled(true);
bottom.setMinimumSize(new Dimension(100, 100));
bottom.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) return;
bottom.setBackground(Color.green);
}
@Override
public void mouseExited(MouseEvent e) {
bottom.setBackground(null);
}
});
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setResizeWeight(0.5);
split.setContinuousLayout(true);
split.setTopComponent(top);
split.setBottomComponent(bottom);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(split, BorderLayout.CENTER);
f.setVisible(true);
}
}
编辑:
也许您可以使用自己的EventQueue。 EventQueue负责将事件调度到组件。所以也许您可以:
查看Global Event Dispatching以获得入门的基本示例。