我编写了MouseListener
如下定义的,以便可以四处移动JButton
以重新排序内的组件JPanel
。的JPanel
是一个内JScrollPane
,使得当添加多种组分,他们可以滚动。
我的问题是,当拖动组件并且鼠标移出滚动窗格/视口时,组件将重新吸附到其位置,JPanel
然后将其绘制在正确的位置。我认为此行为是由于视口在我调用时调用了其子代的重绘scrollRectToVisible()
有什么方法可以防止这种情况发生吗?
请注意,我仅限于Java 5
听众
import java.awt.Component;
import java.awt.Container;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class DragListener extends MouseInputAdapter
{
private Point location;
private MouseEvent pressed;
private MouseEvent dragged;
private MouseEvent dropped;
@Override
public void mousePressed(MouseEvent me)
{
pressed = me;
}
@Override
public void mouseDragged(MouseEvent me)
{
dragged = me;
Component component = dragged.getComponent();
Container parent = component.getParent();
Container superParent = parent.getParent();
if(superParent instanceof JViewport)
{
JViewport vp = (JViewport)superParent;
Rectangle vpb = vp.getBounds();
Point pt = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(pt, vp);
if(!vpb.contains(pt))
{
int yDiff = (pt.y < vpb.y ) ? pt.y : pt.y - vpb.height;
vpb.translate(0, yDiff);
vp.scrollRectToVisible(vpb);
}
}
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
// Mouse release omitted
}
Gui (在NetBeans中创建)
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class DragginTest extends javax.swing.JFrame
{
public DragginTest()
{
initComponents();
addListeners(jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9);
}
private void addListeners(JButton... buttons)
{
DragListener drag = new DragListener();
for(JButton b : buttons)
{
b.addMouseListener(drag);
b.addMouseMotionListener(drag);
}
}
@SuppressWarnings("unchecked")
private void initComponents()
{
jLayeredPane1 = new javax.swing.JLayeredPane();
jScrollPane1 = new javax.swing.JScrollPane();
mainPanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(450, 450));
mainPanel.setLayout(new java.awt.GridLayout(5, 2, 2, 2));
// Below Repeated for buttons 1-9 (left out for conciseness)
jButton1.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
jButton1.setForeground(new java.awt.Color(255, 0, 0));
jButton1.setText("1");
mainPanel.add(jButton1);
// End Repeat
jScrollPane1.setViewportView(mainPanel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(53, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[])
{
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new DragginTest().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
}
我在您的DragListener代码中添加了一个hack。基本上,它会在您拖动时删除布局管理器,因此重新验证无效,并且在释放鼠标时会恢复布局管理器:
import java.awt.*;
import java.awt.Container;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class DragListener extends MouseInputAdapter
{
private Point location;
private MouseEvent pressed;
private MouseEvent dragged;
private MouseEvent dropped;
private LayoutManager layout;
@Override
public void mousePressed(MouseEvent me)
{
pressed = me;
Component component = me.getComponent();
Container parent = component.getParent();
parent.setPreferredSize(parent.getPreferredSize());
layout = parent.getLayout();
parent.setLayout(null);
}
@Override
public void mouseDragged(MouseEvent me)
{
dragged = me;
Component component = dragged.getComponent();
Container parent = component.getParent();
Container superParent = parent.getParent();
if(superParent instanceof JViewport)
{
JViewport vp = (JViewport)superParent;
Rectangle vpb = vp.getBounds();
Point pt = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(pt, vp);
if(!vpb.contains(pt))
{
int yDiff = (pt.y < vpb.y ) ? pt.y : pt.y - vpb.height;
vpb.translate(0, yDiff);
vp.scrollRectToVisible(vpb);
}
}
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
// Mouse release omitted
@Override
public void mouseReleased(MouseEvent me)
{
Component component = me.getComponent();
Container parent = component.getParent();
parent.setPreferredSize( null );
parent.setLayout(layout);
parent.validate();
parent.repaint();
}
}
当然,我假设您的真正的mouseReleased代码将具有将按钮插入Container中适当位置的逻辑,因此其实际位置可以由GridLayout维护,否则组件将回到其原始位置。
编辑:
这是释放鼠标按钮时将按钮移到新位置的版本。有点复杂,因为您需要担心ZOrder。那就是将组件向下拖动是可以的。但是,如果尝试向上拖动组件,则该组件会在其他按钮下方绘制。临时重置ZOrder可解决此问题。
伙计,代码开始成为一个大问题:)临时的null布局和临时的ZOrder。
无论如何,这里是代码:
import java.awt.*;
import java.awt.Container;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class DragListener extends MouseInputAdapter
{
private Point location;
private MouseEvent pressed;
private MouseEvent dragged;
private MouseEvent dropped;
private LayoutManager layout;
private Rectangle originalBounds;
private int originalZOrder;
@Override
public void mousePressed(MouseEvent me)
{
pressed = me;
Component component = me.getComponent();
Container parent = component.getParent();
originalBounds = component.getBounds();
originalZOrder = parent.getComponentZOrder(component);
parent.setPreferredSize(parent.getPreferredSize());
layout = parent.getLayout();
parent.setLayout(null);
parent.setComponentZOrder(component, 0);
}
@Override
public void mouseDragged(MouseEvent me)
{
JComponent source = (JComponent) me.getComponent();
JComponent parent = (JComponent) source.getParent();
Point p = me.getPoint();
p = SwingUtilities.convertPoint(source, p, parent);
Rectangle bounds = source.getBounds();
bounds.setLocation(p);
bounds.x -= pressed.getX();
bounds.y -= pressed.getY();
source.setLocation(0, bounds.y);
parent.scrollRectToVisible(bounds);
}
@Override
public void mouseReleased(MouseEvent me)
{
boolean moved = false;
Component component = me.getComponent();
Container parent = component.getParent();
Point location = component.getLocation();
if (location.y < 0)
{
parent.add(component, 0);
moved = true;
}
else
{
for (int i = 0; i < parent.getComponentCount(); i++)
{
Component c = parent.getComponent(i);
Rectangle bounds = c.getBounds();
if (c == component)
bounds = originalBounds;
// Component is released in the space originally occupied
// by the component or over an existing component
if (bounds.contains(0, location.y))
{
if (c == component)
{
parent.setComponentZOrder(component, originalZOrder);
}
else
{
parent.add(component, i);
}
moved = true;
break;
}
}
}
// Component is positioned below all components in the container
if (!moved)
{
parent.add(component, parent.getComponentCount() - 1);
}
// Restore layout manager
parent.setPreferredSize( null );
parent.setLayout(layout);
parent.validate();
parent.repaint();
component.requestFocusInWindow();
}
private static void createAndShowGUI()
{
JPanel panel = new JPanel( new GridLayout(0, 1) );
DragListener drag = new DragListener();
for (int i = 0; i <10; i++)
{
JButton button = new JButton("" + i);
button.setFont(new java.awt.Font("Tahoma", 1, 48));
button.setForeground(new java.awt.Color(255, 0, 0));
button.addMouseListener(drag);
button.addMouseMotionListener(drag);
panel.add( button );
}
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(panel) );
frame.setLocationByPlatform( true );
frame.setSize(200, 400);
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
我需要给我的客户提供临时访问密钥来连接到物联网服务(发布、接收等)。)。为了提供这种访问,我创建了一个Lambda函数,该函数调用来创建临时STS密钥。那些钥匙正在被创建,看起来很好。 我在具有以下内联策略的角色中使用带有Lambda的assumeRole: 注意:我已经添加了ec2权限来尝试辅助(简化)测试。 此角色具有开放的信任关系: 但是,在我的客户端代码(浏览器)中,我无法连接到IoT,并
有时候,你希望在不会干扰其他工作的情况下临时禁用一个资源。例如, 你可能想要调整服务器上的一个配置文件,经过 Puppet 的不断测试,直到获得你想要的确切设置。 在此期间你不想让 Puppet 覆盖旧版本的配置,为此你可以在资源上设置如下的 noop 元参数: noop => true, 操作步骤 添加如下代码到你的配置清单: file { "/tmp/test.cfg": conte
我知道这个问题之前有人问过,在这里Android禁用最近的任务按钮,就像在SureLock一样,但是既然那里的答案不起作用,也许一些人可以分享一些关于这个被遗忘的事情的光。 我也试过: 但没有运气
本文向大家介绍使用Swing绘制动态时钟,包括了使用Swing绘制动态时钟的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了利用Swing绘制一个动态时钟的具体代码,供大家参考,具体内容如下 效果 代码在下面,可跳过解析。 前言 编程实现一个时钟 利用Swing绘制一个时钟,只能是静态的。利用Calendar类获取当前的时分秒,然后根据数学公式绘制相应的时钟就可以了。 如果静态的时钟已
问题内容: 我使用的是scrollTo jQuery插件,想知道是否可以通过Java临时禁用window元素上的滚动吗?我要禁用滚动的原因是,当您在scrollTo设置动画时滚动时,它确实很难看;) 当然,我可以做一个,然后在动画停止时将其放回自动状态,但是如果滚动条仍然可见但处于非活动状态会更好。 问题答案: 该事件无法取消。但是您可以通过 取消 以下交互事件来实现: 鼠标 和 触摸滚动 以及与
我已经被这件事困扰了几天,希望你能帮助我解决这个问题。我创建了一个类似流程图的应用程序来绘制不同类型的铅锤项目,并将不同的endpoint连接到这些项目上。将其创建为元素后,将根据铅锤项目的类型动态添加endpoint。是这样的吗。 这就是我创建铅锤项目的方式。 然后根据endpoint的类型添加endpoint。 这就是它看起来的样子 屏幕一 我设法从方法“GetConnections”中获取数