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

时间:2019-06-10 19:27:03

标签: java swing events mouseevent jsplitpane

我有一个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);
  }
}

1 个答案:

答案 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负责将事件调度到组件。所以也许您可以:

  1. 将MouseListener添加到分隔线
  2. 在mousePressed上,您用自定义EventQueue替换了默认的EventQueue,它将忽略除分隔符以外的所有组件的MouseEvent。
  3. 在mouseRelease上还原原始的EventQueue。

查看Global Event Dispatching以获得入门的基本示例。