gpt4 book ai didi

java - 拖动时 JPanel 子类 "jumps around"

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:07:59 30 4
gpt4 key购买 nike

我目前正在编写我的一个小型“Paint”程序;到目前为止,你可以用笔在上面画画,放大到 100 倍,然后选择一种颜色。接下来我想添加的是(或者是)拖动 JPanel 子类的可能性,在该子类上绘制选择要编辑的图像。基本上,通过按住鼠标右键,您可以更改位于 JInternalFrame 上的 JPanel 子类的位置。我有一个代码示例,它本身应该可以正常工作;至少它对我有用。要重现此问题,只需启动 DragPanelTest 类并在带有红色边框的组件上拖动鼠标 - 面板拖动不流畅,而是一直来回跳动。

代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class DragPanelTest extends JPanel implements MouseMotionListener, MouseListener {
Point oldDragPt;
boolean dragEnabled;
int newX;
int newY;

public static void main(String[] args) {
System.out.println("Launching Test.java ...");

JFrame frame = new JFrame("Title");
JInternalFrame intFrame = new JInternalFrame("title", true, true, true, true);
JDesktopPane deskPane = new JDesktopPane();
DragPanelTest dragPanel = new DragPanelTest();

frame.setSize(300, 300);;
frame.setLayout(null);
frame.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - frame.getWidth()) / 2;
int y = (d.height - frame.getHeight()) / 2;
frame.setLocation(x, y);

deskPane.setBounds(50, 50, 200, 200);
deskPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
frame.add(deskPane);


deskPane.add(intFrame);
intFrame.setSize(150, 150);
intFrame.add(dragPanel);
intFrame.setVisible(true);
}

public DragPanelTest() {
this.setSize(100,100);
this.setBorder(BorderFactory.createLineBorder(Color.RED));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e) {
if (e.isMetaDown()) {
dragEnabled = true;
oldDragPt = new Point((int) this.getMousePosition().getX(), (int) this.getMousePosition().getY());
}
repaint();
}

public void mouseReleased(MouseEvent e) {
dragEnabled = false;
oldDragPt = null;
}

public void mouseDragged(MouseEvent e) {
if (e.isMetaDown() && this.getMousePosition() != null) {
if (oldDragPt != null) {
if (dragEnabled) {
int mouseX = (int) this.getMousePosition().getX();
int mouseY = (int) this.getMousePosition().getY();
int x = this.getX();
int y = this.getY();
int diffX = (mouseX - (int) oldDragPt.getX());
int diffY = (mouseY - (int) oldDragPt.getY());
newX = getX() + diffX;
newY = getY() + diffY;
this.setLocation(newX, newY);
this.repaint();
oldDragPt = new Point(mouseX, mouseY);
}
} else {
oldDragPt = new Point((int) this.getMousePosition().getX(), (int) this.getMousePosition().getY());
}
}
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}

请注意,我省略了一些 System.out.println()@Override 命令以稍微缩短代码。

无论如何,如果有人知道我做错了什么/需要改进什么,我将非常感激!

最佳答案

the panel isn't dragged smoothly, but instead jumps back and forth all the time.

当你跳跃时,因为鼠标与组件和父面板之间的相对移动是关闭的。您的代码太复杂,无法指出导致问题的确切行。

在面板周围拖动组件的基本代码是:

public class DragListener extends MouseInputAdapter
{
Point location;
MouseEvent pressed;

public void mousePressed(MouseEvent me)
{
pressed = me;
}

public void mouseDragged(MouseEvent me)
{
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}

对于具有更多功能的更高级代码,您还可以查看 Moving Windows 中的 Component Mover 类.

关于java - 拖动时 JPanel 子类 "jumps around",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410551/

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