gpt4 book ai didi

java - 为什么视口(viewport)更改监听器被多次调用

转载 作者:行者123 更新时间:2023-12-01 18:37:28 24 4
gpt4 key购买 nike

我有一个视口(viewport),并且我已将一个更改监听器附加到它。每当我滚动视口(viewport)时,我的更改监听器就会被调用大约四次。我不知道如何避免这种情况;我只想调用一次?

最佳答案

没有办法解决这个问题,JViewport 将触发多个 stateChanged 事件,因为它提供有关许多属性更改的通知...

来自JavaDocs ...

Adds a ChangeListener to the list that is notified each time the view's size, position, or the viewport's extent size has changed.

此时,很难知道该建议什么,因为我们不知道您想要实现什么目标,但是,如果您必须使用 ChangeListener,您可以建立联合机制。也就是说,您基本上要等到事件之间发生足够长的延迟才响应它,而不是响应每个事件......

例如...

public class DelayedChangeHandler implements ChangeListener {

private Timer timer;
private ChangeEvent last;

public DelayedChangeHandler() {
timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stableStateChanged();
}
});
timer.setRepeats(false);
}

@Override
public void stateChanged(ChangeEvent e) {
last = e;
timer.restart();
}

protected void stableStateChanged() {
System.out.println("Finally...");
}

}

基本上,这是一个 ChangeListener 实现,它使用具有短延迟的非重复 javax.swing.Timer 。每次调用stateChanged时,计时器都会重新启动。最后,当允许计时器“滴答”时,它会调用 stableStateChanged 指示自上次事件引发以来已经过去了足够的时间。

这假设您不太关心导致事件的原因,只关心事件发生了......

一个可运行的示例...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestViewport {

public static void main(String[] args) {
new TestViewport();
}

public TestViewport() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JPanel pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
};

JScrollPane sp = new JScrollPane(pane);
sp.getViewport().addChangeListener(new DelayedChangeHandler());
sp.getViewport().addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getPropertyName());
}
});

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(sp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class DelayedChangeHandler implements ChangeListener {

private Timer timer;
private ChangeEvent last;

public DelayedChangeHandler() {
timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stableStateChanged();
}
});
timer.setRepeats(false);
}

@Override
public void stateChanged(ChangeEvent e) {
last = e;
timer.restart();
}

protected void stableStateChanged() {
System.out.println("Finally...");
}

}

}

关于java - 为什么视口(viewport)更改监听器被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301143/

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