gpt4 book ai didi

java - JScrollPane 不调度事件

转载 作者:行者123 更新时间:2023-11-30 08:29:03 35 4
gpt4 key购买 nike

我在 JPanel 内的 JScrollPane 内有一个 JTable。我在最后一个 JPanel 中添加了一个 MouseListener。问题是,当鼠标按下 JTable 之外的可用空间时,我无法监听鼠标事件。我有另一个带有 JTextFieldJPanel,它将事件分派(dispatch)给它的父级 JPanel。可能在 JScrollPane 中设置了一些属性?

SSCCE:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleTableDemo extends JPanel {

public SimpleTableDemo() {
super(new BorderLayout(0, 0));

String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) }, { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) }, { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) }, { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) }, { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setBackground(Color.GREEN);
table.setFillsViewportHeight(false);

JScrollPane scrollPane = new JScrollPane(table);

add(scrollPane, BorderLayout.CENTER);

JPanel textPanel = new JPanel();
JTextField textField = new JTextField(50);
textPanel.add(textField);
add(textPanel, BorderLayout.NORTH);

addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("PRESSED!!!");
}
});
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 400);

SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

例如(基于这里的评论)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class LostMouseEvent {

private JPanel panel1 = new JPanel() {
private static final long serialVersionUID = 1L;

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
private JPanel panel2 = new JPanel() {
private static final long serialVersionUID = 1L;

@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
};

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LostMouseEvent();
}
});
}

public LostMouseEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JScrollPane pane = new JScrollPane(panel2);
panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBorder(BorderFactory.createLineBorder(Color.green));
panel1.add(pane);
MouseListener rml = new RealMouseListener();
panel1.addMouseListener(rml);
MouseListener fml = new FakeMouseListener();
pane.addMouseListener(fml);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel1);
frame.pack();
frame.setVisible(true);

}
});
}

private class RealMouseListener extends MouseAdapter {

@Override
public void mousePressed(MouseEvent me) {
System.out.println(me);
Point point = me.getPoint();
//System.out.println(point);
System.out.println(me.getX());
System.out.println(me.getXOnScreen());
System.out.println(me.getY());
System.out.println(me.getYOnScreen());
}
}

private class FakeMouseListener extends MouseAdapter {

@Override
public void mousePressed(MouseEvent me) {
JScrollPane pane = (JScrollPane) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(panel2/*pane.getViewport()*/, me, panel1);
System.out.println(newMe.getX());
System.out.println(newMe.getXOnScreen());
System.out.println(newMe.getY());
System.out.println(newMe.getYOnScreen());
panel1.dispatchEvent(me);
}
}
}

关于java - JScrollPane 不调度事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770057/

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