gpt4 book ai didi

java - jtextarea鼠标事件覆盖容器鼠标事件

转载 作者:行者123 更新时间:2023-12-01 14:42:46 24 4
gpt4 key购买 nike

这是我的问题:我有一个 jpanel,其中包含三个面板。一个垂直分隔面板、一个聊天输出面板和一个聊天输入面板。主要的jpanel,chatpanel,实现了mousemotionlistener,并且有以下代码:

 public void mouseMoved(MouseEvent e) {
int y = e.getY();

//change cursor look if hovering over vertical spacer
if(y >= (int)(verticalPanelSeperator.getLocation().getY()) && y <= (int)(verticalPanelSeperator.getLocation().getY() + verticalPanelSeperator.getHeight())) {
setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
}
else {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
}
}

public void mouseDragged(MouseEvent e) {
//vertical spacer can be draged up/down to change height of input/output areas
if(getCursor().getType() == Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR).getType()) {
int edge = (int)this.getLocation().getY();
int cur = e.getY();
int height = this.getHeight();
output.setHeightPercent((((double)(cur-edge))/height));
output.componentResized(new ComponentEvent(this, 1));
input.setHeightPercent((((double)(height-cur-edge))/height));
input.componentResized(new ComponentEvent(this, 2));
e.consume();
}
}

问题是,由于某种原因,当您将鼠标移动到verticalpanelseperator上时,它会更改为调整大小光标,但是当您向上移动它而不拖动时,它会保持为调整大小光标。此外,从 print 语句来看,当鼠标进入聊天输出时,e.getY() 的值不会增加,它保持在 Verticalseperatorpanel 的范围内。就像聊天输出默认鼠标监听器覆盖了聊天面板上的鼠标监听器并且不执行任何操作。有人可以告诉我这是怎么回事以及如何解决这个问题吗?

最佳答案

MouseListener 只会响应其容器的鼠标事件,只要没有其他容器正在监视其上方的鼠标事件。将鼠标事件视为雨滴。如果你在你和雨滴之间撑起一把伞,雨滴就不会到达你身上。

我建议您应该使用 MouseListener 接口(interface)中的 mouseEntermouseExit 并将其直接注册到 verticalPanelSeperator 上

关于java - jtextarea鼠标事件覆盖容器鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777247/

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