我有一个JScrollPane
可以将其viewportView设置为一系列不同面板的。每当单击其视口中的任何其他组件时,我都想获取 JScrollPane 组件。如果我将 a 添加MouseListener
到 JScrollPane,它会在我直接单击窗格边框时接收我的鼠标事件,但在我单击组件时不会接收。
添加侦听器并最终找到封闭的滚动窗格的正确方法是什么?我不一定会提前知道我在视口中显示的面板上的所有组件——只是它们将位于 JPanel 的某个子类上。
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;
import net.miginfocom.swing.MigLayout;
public class TestScrollPane extends MouseInputAdapter{
public void mouseEntered(MouseEvent arg0) {System.out.println("Entered " + arg0.getComponent());}
public void mouseExited(MouseEvent arg0) {System.out.println("Exited " + arg0.getComponent());}
public void mousePressed(MouseEvent arg0) {System.out.println("Pressed " + arg0.getComponent());}
public void mouseReleased(MouseEvent arg0) {System.out.println("Released " + arg0.getComponent());}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setLayout(new MigLayout());
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestPane pane1 = new TestPane("Scroll Pane 1");
TestPane pane2 = new TestPane("Scroll Pane 2");
frame.add(pane1, "push,grow");
frame.add(pane2, "push, grow");
TestMouseListener listener = new TestMouseListener();
pane1.addMouseListener(listener);
pane1.addMouseMotionListener(listener);
pane2.addMouseListener(listener);
pane2.addMouseMotionListener(listener);
frame.setVisible(true);
}
}
class TestPanel2 extends JPanel {
String name;
TestPanel2(String name){
this.name = name;
setLayout(new MigLayout());
JTextArea area = new JTextArea();
area.append(name);
add(area, "push, grow");
}
public String toString(){ return name; }
}
class TestPane extends JScrollPane {
String name;
TestPane(String name){
this.name = name;
TestPanel2 panel = new TestPanel2(name + " panel");
setViewportView(panel);
}
public String toString(){ return name; }
}
在这个例子中,我得到了鼠标进入和退出事件,但我只能通过单击文本区域周围的边框来获取鼠标单击事件。即使我更改 TestPane 类以将侦听器添加到它的 viewportView 面板,我也无法判断 textArea 中发生了什么。
class TestPane extends JScrollPane {
String name;
TestPane(String name){
this.name = name;
TestPanel2 panel = new TestPanel2(name + " panel");
TestMouseListener listener = new TestMouseListener();
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
setViewportView(panel);
}
public String toString(){ return name; }
}
不过,我无法知道 JPanel 上的内容,因此我无法更深入地手动添加侦听器。