java 手动触发事件,java-在JTabbedPane上触发stateChanged事件

我有带有淡入淡出动画的JTabbedPane,当用户单击选项卡时执行.为了处理动画,我重写了stateChanged方法.

public class AnimatedJTabbedPane extends JTabbedPane implements ChangeListener, TimingTarget{

/**

*

*/

private static final long serialVersionUID = 1L;

protected BufferedImage previousTabImage;

protected BufferedImage bufferImage;

protected BufferedImage nextTabImage;

protected boolean animationStarted = false;

protected boolean paintPreviousImage = false;

protected boolean leftToRightIndex = false;

protected float fraction = 0.0f;

protected Animator animator;

protected int previousTabIndex = -1;

public AnimatedJTabbedPane(int tabPlacement) {

super(tabPlacement);

this.animator = new Animator(300, this);

this.animator.setAcceleration(0.1f);

this.animator.setDeceleration(0.8f);

this.addChangeListener(this);

}

public void stateChanged(ChangeEvent e) {

if(this.previousTabIndex != this.getSelectedIndex()){

if(this.previousTabIndex == -1){

this.previousTabIndex = this.getSelectedIndex();

}

else{

this.paintPreviousImage = true;

boolean interrupted = false;

if(this.animator.isRunning()){

this.animator.stop();

interrupted= true;

}

Component previousComponent = this.getComponentAt(this.previousTabIndex);

this.previousTabImage = this.getGraphicsConfiguration().createCompatibleImage(

previousComponent.getWidth(), previousComponent.getHeight(), Transparency.TRANSLUCENT);

previousComponent.paint(this.previousTabImage.getGraphics());

Component nextComponent = this.getComponentAt(this.getSelectedIndex());

this.nextTabImage = this.getGraphicsConfiguration().createCompatibleImage(

previousComponent.getWidth(), previousComponent.getHeight(), Transparency.TRANSLUCENT);

nextComponent.paint(this.nextTabImage.getGraphics());

if(this.previousTabIndex < this.getSelectedIndex()){

this.leftToRightIndex = true;

}

else{

this.leftToRightIndex = false;

}

this.previousTabIndex = this.getSelectedIndex();

if(interrupted){

this.animator.setStartFraction(1-this.fraction);

}

else{

this.animator.setStartFraction(0);

}

this.animationStarted = true;

this.animator.start();

}

}

}

@Override

public void paintChildren(Graphics g){

if(this.getComponentCount() != 0){

Rectangle childrenPosition = this.getComponentAt(0).getBounds();

if(this.bufferImage == null ||

this.bufferImage.getWidth() != this.getWidth() ||

this.bufferImage.getHeight() != this.getHeight()){

this.bufferImage = new BufferedImage(

this.getWidth(),

this.getHeight(),

BufferedImage.TYPE_INT_ARGB);

}

if(this.animationStarted){

if(this.paintPreviousImage){

g.drawImage(this.bufferImage, 0, 0, null);

this.paintPreviousImage = false;

}

else{

Graphics2D g2d = (Graphics2D)this.bufferImage.createGraphics();

int x = (int)childrenPosition.getX();

int y = (int)childrenPosition.getY();

this.performFadeAnimation(g2d, x, y);

g.drawImage(this.bufferImage, 0, 0, null);

g2d.dispose();

}

g.dispose();

}

else{

Graphics2D g2d = (Graphics2D)this.bufferImage.createGraphics();

super.paintChildren(g2d);

g.drawImage(this.bufferImage, 0, 0, null);

g2d.dispose();

g.dispose();

}

}

else{

super.paintChildren(g);

}

}

protected void performFadeAnimation(Graphics2D g2d, final double x, final double y){

g2d.drawImage(this.previousTabImage, (int)x, (int)y, null);

AlphaComposite composite =

AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.fraction);

g2d.setComposite(composite);

g2d.drawImage(this.nextTabImage, (int)x, (int)y, null);

}

@Override

public void begin() {

// TODO Auto-generated method stub

}

@Override

public void end() {

this.animationStarted = false;

this.repaint();

this.nextTabImage.flush();

this.nextTabImage = null;

this.previousTabImage.flush();

this.previousTabImage = null;

}

@Override

public void repeat() {

// TODO Auto-generated method stub

}

@Override

public void timingEvent(float fraction) {

this.fraction = fraction;

this.repaint();

}

}

当用户手动单击选项卡时,这非常有用.调用stateChanged事件,并且选项卡随动画而变化.

我需要从代码中更改选定的选项卡,所以我正在使用setSelectedIndex(int)方法.

public void setSelectedTab(int tab){

animatedTabbedPane.setSelectedIndex(tab);

}

但是,现在setSelectedIndex结束后,选项卡立即更改-没有动画,然后调用stateChanged方法,因此选项卡切换回先前选择的选项卡,并(正确地)执行我的动画.

因此选项卡更改了两次,第一次是在setSelectedIndex之后没有动画,第二次是在AnimatedJTabbedPane中的stateChanged之后.

我需要类似的东西:

animatedTabbedPane.firePropertyChange("stateChanged", old, new);

我只想使用stateChanged方法来更改选项卡,所以我可以不使用默认的setSelectedIndex方法.我怎样才能做到这一点?

编辑

我的问题的一点图:

红线代表可见性.因此,当用户更改选项卡时,仅调用stateChanged时,选项卡0平滑地更改为选项卡1.当我使用setSelectedIndex时,选项卡0立即替换为选项卡1,只有这样,我才能从stateChanged中看到所需的动画,因此用户看到了闪光.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值