gpt4 book ai didi

java - 在拖动 JSplitPane 分隔线时如何避免(或消耗)鼠标事件?

转载 作者:行者123 更新时间:2023-11-30 10:02:51 25 4
gpt4 key购买 nike

我有一个打开了连续布局的 JSplitPane。如何在拖动分隔线时阻止其他组件接收鼠标事件?

enter image description here

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

最佳答案

How do I prevent other components from receiving mouse events while the divider is being dragged?

不知道有什么方法可以关闭所有事件。

但我猜您真正担心的是您不希望背景发生变化。

如果是这样,那么您可以向 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 负责将事件分派(dispatch)给组件。所以也许你可以:

  1. 将 MouseListener 添加到分隔线
  2. 在 mousePressed 上,您将默认的 EventQueue 替换为您的自定义 EventQueue,它将忽略除分隔线之外的所有组件的 MouseEvents。
  3. 在 mouseRelease 上恢复原始的 EventQueue。

查看 Global Event Dispatching一个帮助您入门的基本示例。

关于java - 在拖动 JSplitPane 分隔线时如何避免(或消耗)鼠标事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56532425/

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