gpt4 book ai didi

java - 如何获取对 JFrame 内发生 MouseEvent 的元素的引用

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:25 25 4
gpt4 key购买 nike

我编写了 mouseinputlistener 方法的实现

public void mouseDragged(MouseEvent e){
Pion c = (Pion)e.getSource();
c.setBounds(e.getX() + c.getX(), e.getY() + c.getY(), 30, 30);

}

我在类中实现了它,它是JFrame的扩展,它包含一个具有不同组件的gridLayout:JPanel并且一些面板包含JButtons

我需要的是一种方法,它可以为我提供对发生事件的组件的引用,无论它是 JPanel 还是 JButton

我尝试使用 e.getSource,但显然它不起作用,代码可以编译,但每次我在组件上拖动鼠标时,都会收到一个异常,提示“Plateau(即扩展 JFrame 的当前类)无法转换为 Pion(即扩展 JButton 的类)”

我尝试为扩展组件(Pion 等)的每个类实现 MouseInputListener,但我需要在 JFrame 规模上执行操作,而这不允许这样做。

这是我得到的异常:

java.lang.ClassCastException: PlateauColorLine cannot be cast to javax.swing.JPanel
at PlateauColorLine.mouseDragged(PlateauColorLine.java:299)
at java.awt.Component.processMouseMotionEvent(Component.java:6564)
at java.awt.Component.processEvent(Component.java:6285)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Window.processEvent(Window.java:2022)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

`

最佳答案

希望这会有所帮助

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Pion extends JButton
{
public Pion (String title)
{
super(title);
addMouseListener(new MouseListnerAdapter());
}
}

class MouseListnerAdapter extends MouseAdapter
{
@Override
public void mousePressed(MouseEvent e) {
Object o = e.getSource();

if(o instanceof JButton)
{
JButton b = (JButton) o;
b.setBounds(e.getX() + b.getX(), e.getY() + b.getY(), 30, 30);
}
}
};


public class Test extends JFrame {
public Test()
{
super("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,1));


JPanel p1 = new JPanel();
Pion pb1 = new Pion("button1");
p1.add(pb1);
add(p1);

JPanel p2 = new JPanel();
Pion pb2 = new Pion("button2");
p2.add(pb2);
add(p2);

pack();
setSize(240, 240);
setLocationByPlatform(true);
setVisible(true);
addMouseListener(new MouseListnerAdapter());

}

public static void main(String[] args)throws Exception
{
new Test();
}
}

关于java - 如何获取对 JFrame 内发生 MouseEvent 的元素的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360181/

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