gpt4 book ai didi

java - Java Timing Framework 中的 PropertySetter 不适用于我自己的属性

转载 作者:行者123 更新时间:2023-12-01 15:39:04 25 4
gpt4 key购买 nike

我有一个使用时序框架的小类 http://java.net/projects/timingframework/ 。代码如下:

class Kula extends JPanel {
private Animator an;
private int xp;
private int yp;
private Color kolor;

public Kula(int x, int y) {
this.xp = x;
this.yp = y;
this.kolor = Color.RED;

TimingTarget tt = PropertySetter.getTarget(this, "kolor", Color.RED, Color.BLUE);
an = new Animator.Builder().setDuration(2, TimeUnit.SECONDS).addTarget(tt).build();
}

public Color getKolor() {
return kolor;
}

public void setKolor(Color kolor) {
this.kolor = kolor;
repaint();
}

public Animator getAnimator() {
return this.an;
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(kolor);
g.fillOval(xp, yp, 20, 20);
}
}

如您所见,它绘制了一个具有特定颜色和位置(xp 和 yp)的椭圆形。它还有一个动画师。在构造函数中,我创建了 Animator,但在创建 TimingTarget 之前,我使用线性插值器(默认)更改 kolor 变量。我有 getter 和 setter,但是当我启动我的动画师时,我得到一个异常:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: (Timing Framework #31) An unexpected exception occurred when reflectively invoking the method setKolor on swingtest.GUI$Kula[,0,0,798x533,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=].    at org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget.valueAtTimingEvent(PropertySetter.java:298)    at org.jdesktop.core.animation.timing.KeyFramesTimingTarget.timingEvent(KeyFramesTimingTarget.java:51)    at org.jdesktop.core.animation.timing.Animator.notifyListenersAboutATimingSourceTick(Animator.java:934)    at org.jdesktop.core.animation.timing.Animator.timingSourceTick(Animator.java:1124)    at org.jdesktop.core.animation.timing.TimingSource$1.run(TimingSource.java:183)    at org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource$1.actionPerformed(SwingTimerTimingSource.java:75)    at javax.swing.Timer.fireActionPerformed(Timer.java:312)    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)    at java.awt.EventQueue.access$000(EventQueue.java:101)    at java.awt.EventQueue$3.run(EventQueue.java:666)    at java.awt.EventQueue$3.run(EventQueue.java:664)    at java.security.AccessController.doPrivileged(Native Method)    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)Caused by: java.lang.IllegalAccessException: Class org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget can not access a member of class swingtest.GUI$Kula with modifiers "public"    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)    at java.lang.reflect.Method.invoke(Method.java:594)    at org.jdesktop.core.animation.timing.PropertySetter$PropertySetterTimingTarget.valueAtTimingEvent(PropertySetter.java:296)    ... 21 more

当我改变线路时,有趣的是:

TimingTarget tt = PropertySetter.getTarget(this, "kolor", Color.RED, Color.BLUE);

进入

TimingTarget tt = PropertySetter.getTarget(this, "background", Color.RED, Color.BLUE);

一切都好。我的面板背景更改正确。

为什么我的 PropertySetter 不能与 kolor 变量一起使用?

附注在波兰语中,“Kula”表示球体,“kolor”表示颜色

最佳答案

PropertySetter 通过反射访问目标的属性。仅当类和属性都具有公共(public)范围时才可能实现。

  • 不可访问:“kolor”是包范围类 Kula 中的公共(public)方法
  • 可访问:“background”是公共(public)类 Component 中的公共(public)方法

解决方案是公开您的类(class)。

关于java - Java Timing Framework 中的 PropertySetter 不适用于我自己的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375547/

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