下面列出了java.awt.event.MouseEvent#getXOnScreen() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private MouseEvent convertMouseEvent(MouseEvent e) {
// JList where mouse click acts like ctrl-mouse click
// https://community.oracle.com/thread/1351452
return new MouseEvent(
e.getComponent(),
e.getID(), e.getWhen(),
// e.getModifiers() | InputEvent.CTRL_DOWN_MASK,
// select multiple objects in OS X: Command + click
// pointed out by nsby
e.getModifiersEx() | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(),
// Java 10: e.getModifiersEx() | Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx(),
e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
e.getButton());
}
@Override protected TrackListener createTrackListener() {
return new TrackListener() {
@Override public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
super.mousePressed(new MouseEvent(
e.getComponent(), e.getID(), e.getWhen(),
InputEvent.BUTTON2_DOWN_MASK ^ InputEvent.BUTTON2_MASK,
e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
MouseEvent.BUTTON2));
} else {
super.mousePressed(e);
}
}
};
}
private void redispatchEvent(MouseEvent event) {
JList list = (JList) event.getSource();
int index = list.locationToIndex(event.getPoint());
Rectangle cellBounds = list.getCellBounds(index, index);
if (cellBounds == null) {
return;
}
ListModel model = list.getModel();
Object state = model.getElementAt(index);
ListCellRenderer renderer = list.getCellRenderer();
Component rendererComponent =
renderer.getListCellRendererComponent(list, state, index, true, true);
rendererComponent.setBounds(cellBounds);
Point p = event.getPoint();
p.translate(-cellBounds.x, -cellBounds.y);
MouseEvent newEvent =
new MouseEvent(rendererComponent, event.getID(), event.getWhen(), event.getModifiers(),
p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(),
event.isPopupTrigger(), event.getButton());
rendererComponent.dispatchEvent(newEvent);
list.repaint();
}
@Override
public void mousePressed(MouseEvent me) {
if (me.getButton() == MouseEvent.BUTTON1) {
prevX = me.getXOnScreen();
prevY = me.getYOnScreen();
if ((outcode = getOutcode(me.getPoint(), new Rectangle(me.getComponent().getSize()))) != 0) {
resizing = true;
prevR = me.getComponent().getBounds();
} else if (jPanel_TitleBar.contains(me.getPoint()) && ((getExtendedState() & JFrame.MAXIMIZED_BOTH) != JFrame.MAXIMIZED_BOTH)) {
dragging = true;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
dragging = true;
if (type == Tree.H) {
int w2 = getX() + getWidth() / 2;
vd = e.getXOnScreen() - w2;
}
else {
int h2 = getY() + getHeight() / 2;
vd = e.getYOnScreen() - h2;
}
repaint();
}
@Override
public void mousePressed (MouseEvent e)
{
glassPane.setVisible(true);
ScreenPoint screenPoint = new ScreenPoint(
e.getXOnScreen(),
e.getYOnScreen());
glassPane.setImage(image);
glassPane.setPoint(screenPoint);
}
@Override
public void mouseReleased (MouseEvent e)
{
ScreenPoint screenPoint = new ScreenPoint(e.getXOnScreen(), e.getYOnScreen());
glassPane.setVisible(false);
glassPane.setImage(null);
fireDropEvent(new GhostDropEvent<>(action, screenPoint));
}
/**
* 显示鼠标跟随的信息提示,配合{@link UI#hiddenMessageForMouse()} 使用
* @param mouseEvent 添加鼠标监听后,传入鼠标的监听对象 java.awt.event.MouseEvent
* @param width 显示的提示框宽度
* @param height 显示的提示框高度
* @param html 显示的文字,支持html格式
* @return 显示文字的组件JLabel,可对此组件进行调整
* @see UI#showMessageForMouse(int, int, int, int, String)
*/
public static JLabel showMessageForMouse(MouseEvent mouseEvent,int width,int height,String html){
int x=0;
int y=0;
if(mouseEvent!=null){
x=mouseEvent.getXOnScreen();
y=mouseEvent.getYOnScreen();
}
return showMessageForMouse(x, y, width, height, html);
}
@Override protected void processMouseMotionEvent(MouseEvent e) {
if (pointOutsidePrefSize(e.getPoint())) {
MouseEvent ev = new MouseEvent(
e.getComponent(), MouseEvent.MOUSE_EXITED, e.getWhen(),
e.getModifiersEx(), e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(), e.isPopupTrigger(), MouseEvent.NOBUTTON);
super.processMouseEvent(ev);
} else {
super.processMouseMotionEvent(e);
}
}
@Override
public void mouseReleased (MouseEvent e)
{
ScreenPoint screenPoint = new ScreenPoint(
e.getXOnScreen(),
e.getYOnScreen());
glassPane.setVisible(false);
glassPane.setImage(null);
fireDropEvent(new GhostDropEvent<>(action, screenPoint));
}
/**
* Returns a point that describes the upper left corner of the dialog
* to show. The function makes sure that the point is chosen so that
* the whole dialog is visible.
*
* @param event The mouse event that contains the click information.
*
* @return The normalized coordinates of the dialog.
*/
private Point getDialogLocation(final MouseEvent event)
{
// Get the default toolkit
final Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
final Dimension scrnsize = toolkit.getScreenSize();
final int x = event.getXOnScreen();
final int y = event.getYOnScreen();
return new Point(normalize(x, m_helpDialog.getWidth(), scrnsize.width), normalize(y, m_helpDialog.getHeight(), scrnsize.height));
}
@Override
public void mouseClicked(MouseEvent evt) {
JComponent source = (JComponent) evt.getSource();
int index;
if (source instanceof JList) {
index = list.locationToIndex(evt.getPoint());
} else if (source instanceof JTable) {
JTable table = (JTable) source;
Point p = evt.getPoint();
index = table.rowAtPoint(p);
boolean pointOutsidePrefSize = SwingUtilities2.pointOutsidePrefSize(table, index,
table.columnAtPoint(p), p);
if (pointOutsidePrefSize && !fullRowSelection) {
return;
}
// Translate point from table to list
if (index >= 0 && list != null && listSelectionModel.isSelectedIndex(index)) {
// Make a new event with the list as source, placing the
// click in the corresponding list cell.
Rectangle r = list.getCellBounds(index, index);
MouseEvent newEvent = new MouseEvent(list, evt.getID(),
evt.getWhen(), evt.getModifiersEx(),
r.x + 1, r.y + r.height / 2,
evt.getXOnScreen(),
evt.getYOnScreen(),
evt.getClickCount(), evt.isPopupTrigger(),
evt.getButton());
SwingUtilities.convertMouseEvent(list, newEvent, list);
evt = newEvent;
}
} else {
return;
}
if (index >= 0 && SwingUtilities.isLeftMouseButton(evt)) {
JFileChooser fc = getFileChooser();
// For single click, we handle editing file name
if (evt.getClickCount() == 1 && source instanceof JList) {
if ((!fc.isMultiSelectionEnabled() || fc.getSelectedFiles().length <= 1)
&& listSelectionModel.isSelectedIndex(index)
&& getEditIndex() == index && editFile == null
&& DarkUIUtil.isOverText(evt, index, list)) {
editFileName(index);
} else {
setEditIndex(index);
}
} else if (evt.getClickCount() == 2) {
// on double click (open or drill down one directory) be
// sure to clear the edit index
resetEditIndex();
}
}
// Forward event to Basic
if (getDoubleClickListener() != null) {
list.putClientProperty("List.isFileList", false);
getDoubleClickListener().mouseClicked(evt);
list.putClientProperty("List.isFileList", true);
}
}
@Override
public void mousePressed( MouseEvent e ) {
lastXOnScreen = e.getXOnScreen();
lastYOnScreen = e.getYOnScreen();
}
/**
* Unlike {@link JTree#getToolTipText()} this implementation takes row selection style into account.
* That means that tooltips for {@link TreeSelectionStyle#line} will be displayed at any point in the row, not just on the node.
*
* @param event {@link MouseEvent}
* @return tooltip text
*/
@Nullable
@Override
public String getToolTipText ( @Nullable final MouseEvent event )
{
String tip = null;
if ( event != null )
{
final Point point = event.getPoint ();
final WTreeUI ui = getUI ();
final int row = ui.getExactRowForLocation ( point );
final TreeCellRenderer cellRenderer = getCellRenderer ();
if ( row != -1 && cellRenderer != null )
{
final TreePath path = getPathForRow ( row );
final Object value = path.getLastPathComponent ();
final boolean selected = isRowSelected ( row );
final boolean expanded = isExpanded ( row );
final boolean leaf = isLeaf ( ( N ) value );
final Component renderer = cellRenderer.getTreeCellRendererComponent ( this, value, selected, expanded, leaf, row, true );
if ( renderer instanceof JComponent )
{
final Rectangle pathBounds = getPathBounds ( path );
if ( pathBounds != null )
{
final MouseEvent newEvent = new MouseEvent ( renderer, event.getID (),
event.getWhen (),
event.getModifiers (),
point.x - pathBounds.x,
point.y - pathBounds.y,
event.getXOnScreen (),
event.getYOnScreen (),
event.getClickCount (),
event.isPopupTrigger (),
MouseEvent.NOBUTTON );
final JComponent jComponent = ( JComponent ) renderer;
tip = jComponent.getToolTipText ( newEvent );
}
}
}
}
if ( tip == null )
{
tip = getToolTipText ();
}
return tip;
}
@Override
public void mouseDragged(MouseEvent me) {
// dragging
if (dragging) {
int x = me.getXOnScreen();
int y = me.getYOnScreen();
// Move frame by the mouse delta
setLocation(getLocationOnScreen().x + x - prevX, getLocationOnScreen().y + y - prevY);
prevX = x;
prevY = y;
}
// resizing
if (resizing) {
Component component = me.getComponent();
Rectangle rect = prevR;
int xInc = me.getXOnScreen() - prevX;
int yInc = me.getYOnScreen() - prevY;
// Resizing the West or North border affects the size and location
switch (outcode) {
case Rectangle.OUT_TOP:
rect.y += yInc;
rect.height -= yInc;
break;
case Rectangle.OUT_TOP + Rectangle.OUT_LEFT:
rect.y += yInc;
rect.height -= yInc;
rect.x += xInc;
rect.width -= xInc;
break;
case Rectangle.OUT_LEFT:
rect.x += xInc;
rect.width -= xInc;
break;
case Rectangle.OUT_LEFT + Rectangle.OUT_BOTTOM:
rect.height += yInc;
rect.x += xInc;
rect.width -= xInc;
break;
case Rectangle.OUT_BOTTOM:
rect.height += yInc;
break;
case Rectangle.OUT_BOTTOM + Rectangle.OUT_RIGHT:
rect.height += yInc;
rect.width += xInc;
break;
case Rectangle.OUT_RIGHT:
rect.width += xInc;
break;
case Rectangle.OUT_RIGHT + Rectangle.OUT_TOP:
rect.y += yInc;
rect.height -= yInc;
rect.width += xInc;
break;
default:
break;
}
prevX = me.getXOnScreen();
prevY = me.getYOnScreen();
component.setBounds(rect);
component.validate();
component.repaint();
}
}
private void formMouseDragged(MouseEvent evt) {
int x = evt.getXOnScreen();
int y = evt.getYOnScreen();
this.setLocation(x - xMouse, y - yMouse);
}
@Override
public String getToolTipText(MouseEvent event) {
try {
// Required to really get the tooltip text:
putClientProperty("ComputingTooltip", Boolean.TRUE);
toolTip = null;
String tipText = null;
Point p = event.getPoint();
// Locate the renderer under the event location
int hitColumnIndex = columnAtPoint(p);
int hitRowIndex = rowAtPoint(p);
if ((hitColumnIndex != -1) && (hitRowIndex != -1)) {
//Outline tbl = (Outline) table;
if (convertColumnIndexToModel(hitColumnIndex) == 0) { // tree column index
// For tree column get the tooltip directly from the renderer data provider
RenderDataProvider rendata = getRenderDataProvider();
if (rendata != null) {
Object value = getValueAt(hitRowIndex, hitColumnIndex);
if (value != null) {
String toolT = rendata.getTooltipText(value);
if (toolT != null && (toolT = toolT.trim ()).length () > 0) {
tipText = toolT;
}
}
}
}
TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex);
Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
// Now have to see if the component is a JComponent before
// getting the tip
if (component instanceof JComponent) {
// Convert the event to the renderer's coordinate system
Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
p.translate(-cellRect.x, -cellRect.y);
MouseEvent newEvent = new MouseEvent(component, event.getID(),
event.getWhen(), event.getModifiers(),
p.x, p.y,
event.getXOnScreen(),
event.getYOnScreen(),
event.getClickCount(),
event.isPopupTrigger(),
MouseEvent.NOBUTTON);
if (tipText == null) {
tipText = ((JComponent)component).getToolTipText(newEvent);
}
toolTip = ((JComponent)component).createToolTip();
}
}
// No tip from the renderer get our own tip
if (tipText == null)
tipText = getToolTipText();
if (tipText != null) {
tipText = tipText.trim();
if (tipText.length() > MAX_TOOLTIP_LENGTH &&
!tipText.regionMatches(false, 0, "<html>", 0, 6)) { // Do not cut HTML tooltips
tipText = tipText.substring(0, MAX_TOOLTIP_LENGTH) + "...";
}
}
return tipText;
} finally {
putClientProperty("ComputingTooltip", Boolean.FALSE);
}
//return super.getToolTipText(event);
}
public void mousePressed(MouseEvent e) {
dragging = true;
dragX = e.getXOnScreen();
dragY = e.getYOnScreen();
}
/**
* In this specific implementation, we update the size of the
* shape image according to the interline scale and to the
* display zoom of the droppable target underneath.
*
* @param e the mouse event
*/
@Override
public void mouseDragged (MouseEvent e)
{
final ShapeButton button = (ShapeButton) e.getSource();
final Shape shape = button.shape;
final ScreenPoint screenPoint = new ScreenPoint(e.getXOnScreen(), e.getYOnScreen());
final OmrGlassPane glass = (OmrGlassPane) glassPane;
// The (zoomed) sheet view
ScrollView scrollView = sheet.getStub().getAssembly().getSelectedView();
Component component = scrollView.getComponent().getViewport();
if (screenPoint.isInComponent(component)) {
final RubberPanel view = scrollView.getView();
final Zoom zoom = view.getZoom();
final Point localPt = zoom.unscaled(screenPoint.getLocalPoint(view));
glass.setOverTarget(true);
// Moving into this component?
if (component != prevComponent.get()) {
if (shape.isDraggable()) {
if (dndOperation == null) {
// Set payload
dndOperation = new DndOperation(
sheet,
zoom,
InterFactory.createManual(shape, sheet));
}
dndOperation.enteringTarget();
} else {
glass.setImage(getNonDraggableImage(zoom));
glass.setReference(null);
}
prevComponent = new WeakReference<>(component);
}
if (shape.isDraggable()) {
// Update reference point
Point localRef = dndOperation.getReference(localPt);
glass.setReference(
(localRef != null) ? new ScreenPoint(view, zoom.scaled(localRef))
: null);
}
} else if (prevComponent.get() != null) {
// No longer on a droppable target, reuse initial image & size
glass.setOverTarget(false);
glass.setImage(dropAdapter.getImage());
glass.setReference(null);
reset();
}
glass.setPoint(screenPoint); // This triggers a repaint of glassPane
}
public void mouseDragged(MouseEvent e) {
if (dragX >= 0 && dragY >= 0) {
int x = e.getXOnScreen();
int y = e.getYOnScreen();
int dx = x - dragX;
int dy = y - dragY;
int newX = window.getX();
int newY = window.getY();
int newW = window.getWidth();
int newH = window.getHeight();
int xx = 0;
int yy = 0;
Dimension min = window.getMinimumSize();
if (isResizeLeft(currentResizing)) {
newX += dx;
newW -= dx;
if (newW < min.width) {
xx = newW - min.width;
newX += xx;
newW = min.width;
}
} else if (isResizeRight(currentResizing)) {
newW += dx;
if (newW < min.width) {
xx = min.width - newW;
newW = min.width;
}
}
if (isResizeTop(currentResizing)) {
newY += dy;
newH -= dy;
if (newH < min.height) {
yy = newH - min.height;
newY += yy;
newH = min.height;
}
} else if (isResizeBottom(currentResizing)) {
newH += dy;
if (newH < min.height) {
yy = min.height - newH;
newH = min.height;
}
}
window.setBounds(newX, newY, newW, newH);
content.setSize(newW, newH);
dragX = x + xx;
dragY = y + yy;
}
}