gpt4 book ai didi

java - 是否可以监听 ActionListener 以达到完成状态并切换到新的 ActionListener?

转载 作者:行者123 更新时间:2023-11-30 09:17:18 27 4
gpt4 key购买 nike

我的程序的先前版本旨在显示多个系列的魔术图像,所有这些图像都使用相同类型的 ActionListener。例如,在显示每幅图像之间设置时间的所有技巧时,我通过使用扩展 ActionListener 的名为 TimeListener 的类来实现。我创建了该类的一个新实例,然后使用 Timer 告诉它在每个持续时间触发一个事件,其中持续时间是可变的毫秒数。

TimeListener timeListener = new TimeListener();
Timer displayTimer = new javax.swing.Timer(duration, timeListener);
displayTimer.start();

系列按用户输入的顺序显示,TimeListener 中的控制结构确定下一张显示的图像。

我现在需要对其进行设置,以便不同的技巧具有不同的 ActionListener。我想按如下方式设置它们:

ActionListener[] actionListeners = new ActionListener[trickSettings.length];
for(int i = 0; i < trickSettings.length; i++) {
String pace = trickSettings[i].getPace();
String trick = trickSettings[i].getTrick();
if(pace.equals("Set Pace") || pace.equals("Set Pace Free Pause") || pace.equals("Set Pace Timed Pause") || pace.equals("Set Pace Subgoal Pause")) {
actionListeners[i] = new TimeListener(trick, pace);
}
else if(pace.equals("Yoked Pace")) {
actionListeners[i] = new YokedListener(trick, pace);
}
else if(pace.equals("Self Pace")) {
actionListeners[i] = new SelfListener(trick, pace);
}
}

但是,以前的方法只是从类的构造函数中运行所有内容。如果我采用这种新方法,虽然我需要能够切换哪个 ActionListener 处于 Activity 状态。我很确定这不能从构造函数中切换。我是否需要制作一个 ActionListener 来告知这些 ActionListeners 何时完成,以便它可以切换到下一个?有没有更简单的方法来做到这一点?


编辑我的解决方案

private class MasterListener {
public void newListener() {
for(int i = 0; i < trickSettings.length; i++) {
if(trickSettings[i].getIsDone() == false) {
start(trickSettings[i], 0, true);
inBegin = true;
mouse = new Mouse(trickSettings[i]);
addMouseListener(mouse);
timeListener = new TimeListener(trickSettings[i]);
displayTimer = new Timer(trickSettings[i].getDuration(), timeListener);
displayTimer.start();
break;
}
}
}
}

我只是通过在构造函数中创建一个 MasterListener 实例并在其上调用 newListener() 来启动它。每次我完成技巧时,我只是在我的 MasterListener 对象上调用 newListener()。

注意:start 方法执行一个新的 SwingWorker,用于在后台预加载图像。

最佳答案

我会避免将数据与代码混合,换句话说,避免“硬编码”您的数据,而这正是您想要做的。相反,我会...

  • 创建一个新类,包含一个 Trick,它的 Pace,无论它是否被束缚。
  • 您的程序模型中可能有此类对象的集合
  • 使用此类的当前对象的状态来确定单个 ActionListener 的行为方式。请注意,ActionListener 可以根据对象的状态调用不同的方法。

编辑你的评论/我的回复

I actually already have a Class that contains all of that information called TrickSettings. Should I make this class also contain information on how many images it has shown/if it is done? In the above code trickSettings is an array of TrickSettings.

是的。它需要做的就是返回该技巧的图像集合的 size()

My concern with your third point, is that I don't want my ActionListener firing with the same timing for all of the tricks.

所以让计时器使用它从当前正在迭代的 TrickSettings 对象获得的时间。没什么大不了的。

For example, tricks that are paced as "Yoked Pace" read the timing from a file, or even more different are tricks that are pace "Self Pace" where the user clicks to advance them. I previously had these using a MouseAdapter class instead...

您的程序逻辑可以很容易地适应这一点。


编辑
很高兴你有工作。既然你有解决方案,我现在将发布我的解决方案,这是我昨天创建的用于测试我的想法的解决方案。也许它可以给你一些想法:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.swing.*;

public class TimerSpeedEg extends JPanel {
private static final int BI_W = 150;
private static final int BI_H = BI_W;
private static final int SHOW_TRICKS_COUNT = 3;
private List<Icon> icons;
private Random random = new Random();
private List<ShowTricks> showTricksList = new ArrayList<>();

public TimerSpeedEg() {
icons = createIcons();


setLayout(new GridLayout(1, 0));
add(createShowTricksPanel(BI_W, BI_H, "Fast", 100, false));
add(createShowTricksPanel(BI_W, BI_H, "Slow", 1000, false));
add(createShowTricksPanel(BI_W, BI_H, "Self-Paced", 0, true));

}

private JPanel createShowTricksPanel(int w, int h, String title,
int speed, boolean selfPaced) {
final ShowTricks showTricks = new ShowTricks(w, h);
TrickSettings trickSettings = new TrickSettings(speed, selfPaced);
for (Icon icon : icons) {
trickSettings.addIcon(icon);
}
showTricks.setTrickSettings(trickSettings );
JButton showButton = new JButton(new AbstractAction("Show") {

@Override
public void actionPerformed(ActionEvent arg0) {
showTricks.show();
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(showButton);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder(title));
panel.add(showTricks.getTrickLabel(), BorderLayout.CENTER);
panel.add(btnPanel, BorderLayout.SOUTH);
return panel;
}

private List<Icon> createIcons() {
List<Icon> iconList = new ArrayList<Icon>();
String[] strings = { "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten" };
for (int i = 0; i < strings.length; i++) {
JLabel label = new JLabel(strings[i], SwingConstants.CENTER);
label.setSize(BI_W, BI_H);
label.setOpaque(true);
Color color = new Color(random.nextInt(127) + 127,
random.nextInt(127) + 127, random.nextInt(127) + 127);
Color borderColor = new Color(color.getRed() - 100,
color.getGreen() - 100, color.getBlue() - 100);
label.setBackground(color);
label.setBorder(BorderFactory.createLineBorder(borderColor, 20));
BufferedImage bImg = new BufferedImage(BI_W, BI_H,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bImg.getGraphics();
label.paint(g);
g.dispose();
iconList.add(new ImageIcon(bImg));
}
return iconList;
}

private static void createAndShowGui() {
TimerSpeedEg mainPanel = new TimerSpeedEg();

JFrame frame = new JFrame("TimerSpeedEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class ShowTricks {
int prefW;
int prefH;
private JLabel trickLabel = new JLabel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
};

public ShowTricks(int prefW, int prefH) {
this.prefW = prefW;
this.prefH = prefH;
}

private TrickSettings trickSettings;

public JLabel getTrickLabel() {
return trickLabel;
}

public void setTrickSettings(TrickSettings trickSettings) {
this.trickSettings = trickSettings;
}

public void show() {
if (trickSettings == null) {
return;
}
if (trickSettings.isSelfPaced()) {
showSelfPaced();
} else {
showWithTimer();
}
}

private void showWithTimer() {
Timer timer = new Timer(trickSettings.getMiliseconds(), new TimerListener());
timer.setInitialDelay(0);
timer.start();
}

private void showSelfPaced() {
trickLabel.setIcon(trickSettings.getIcon(0));
trickLabel.addMouseListener(new MouseAdapter() {
private int count = 1;

@Override
public void mousePressed(MouseEvent e) {
if (count < trickSettings.getSize()) {
trickLabel.setIcon(trickSettings.getIcon(count));
count++;
} else {
trickLabel.removeMouseListener(this);
}
}
});
}

private class TimerListener implements ActionListener {
private int count = 0;

@Override
public void actionPerformed(ActionEvent evt) {
if (count < trickSettings.getSize()) {
trickLabel.setIcon(trickSettings.getIcon(count));
count++;
} else {
((Timer) evt.getSource()).stop();
}
}
}
}

class TrickSettings implements Iterable<Icon> {
private List<Icon> icons = new ArrayList<Icon>();
private int miliseconds;
private boolean selfPaced;

public TrickSettings(int miliseconds, boolean selfPaced) {
this.miliseconds = miliseconds;
this.selfPaced = selfPaced;
}

public int getSize() {
return icons.size();
}

public int getMiliseconds() {
return miliseconds;
}

public boolean isSelfPaced() {
return selfPaced;
}

@Override
public Iterator<Icon> iterator() {
return icons.iterator();
}

public Icon getIcon(int index) {
return icons.get(index);
}

public void addIcon(Icon icon) {
icons.add(icon);
}
}

关于java - 是否可以监听 ActionListener 以达到完成状态并切换到新的 ActionListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038765/

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