【问题标题】:ActionListener class keeps repeating when swing Timer is started摆动计时器启动时,ActionListener 类不断重复
【发布时间】:2018-07-16 02:38:27
【问题描述】:

我正在尝试使用 Java GUI 开发一个非常基本的“西蒙说”模拟器。我有一个方法可以生成并返回一个int[] 数组;对于数组中的每个元素,定时器computer 应该启动,为指定的JButton 调用doClick() 方法,并等待1/2 秒。每个JButton 都连接到一个ActionListener(),该ActionListener() 将特定按钮的颜色更改为白色,激活另一个计时器timer,并将按钮更改回原来的颜色。

每次我在 for 循环中调用 computer.start(); 时,它都会在 ComputerListener() 中运行代码,但它会无休止地重复。我添加了打印语句,以便我可以通过 Netbeans 上的输出查看发生了什么。我在论坛上查看过类似的问题,但没有提供可行的解决方案。

我的问题:为什么在 for 循环中调用 computer.start(); 时我的 ComputerListener 类会重复?

package simon;

// @jagged_prospect

import java.util.Random;
import java.util.Arrays;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;

public class SIMONPanel extends JPanel{

private static final int PANEL_W=300,PANEL_H=300;
private static final int PREF_W=500,PREF_H=500;
private static final String[] CARD_LABELS={"main","info","game"};

private final JPanel gameCard,infoCard,splashCard;
private final JButton rButton,yButton,gButton,bButton;
private final int lives=3;

private CardLayout cardlayout=new CardLayout();
private JPanel cards=new JPanel(cardlayout);
private Action[] actions={new ShowMainAction(),new ShowInfoAction(),
    new ShowGameAction()};
private Object source;
private Timer timer,computer;

public SIMONPanel(){
    setBackground(Color.BLACK);
    setLayout(new BorderLayout());
    gameCard=new JPanel();
    infoCard=new JPanel();
    splashCard=new JPanel();

    // game card panel
    gameCard.setLayout(new BorderLayout());
    gameCard.setPreferredSize(new Dimension(PANEL_W,PANEL_H));

    JPanel gameButtonPanel=new JPanel();
    gameButtonPanel.setLayout(new GridLayout(2,2));
    JButton startButton=new JButton("Start");
    startButton.addActionListener(new StartListener());

    rButton=new JButton("red");
    rButton.addActionListener(new ColorButtonListener());
    rButton.setSize(50,50);
    rButton.setUI((ButtonUI)BasicButtonUI.createUI(rButton));
    rButton.setBackground(Color.RED);
    rButton.setForeground(Color.WHITE);

    yButton=new JButton("yellow");
    yButton.addActionListener(new ColorButtonListener());
    yButton.setSize(50,50);
    yButton.setUI((ButtonUI)BasicButtonUI.createUI(yButton));
    yButton.setBackground(Color.YELLOW);

    gButton=new JButton("green");
    gButton.addActionListener(new ColorButtonListener());
    gButton.setSize(50,50);
    gButton.setUI((ButtonUI)BasicButtonUI.createUI(gButton));
    gButton.setBackground(Color.GREEN);

    bButton=new JButton("blue");
    bButton.addActionListener(new ColorButtonListener());
    bButton.setSize(50,50);
    bButton.setUI((ButtonUI)BasicButtonUI.createUI(bButton));
    bButton.setBackground(Color.BLUE);
    bButton.setForeground(Color.WHITE);

    gameButtonPanel.add(gButton);
    gameButtonPanel.add(rButton);
    gameButtonPanel.add(yButton);
    gameButtonPanel.add(bButton); 
    gameCard.add(gameButtonPanel,BorderLayout.CENTER);
    gameCard.add(startButton,BorderLayout.SOUTH);

    // splash card panel
    splashCard.setLayout(new BorderLayout());
    splashCard.setPreferredSize(new Dimension(PANEL_W,PANEL_H));
    splashCard.setBackground(Color.BLACK);
    JLabel titleLabel=new JLabel("S I M O N",SwingConstants.CENTER);
    titleLabel.setFont(new Font("Niagara Solid",Font.BOLD,84));
    titleLabel.setForeground(Color.WHITE);
    splashCard.add(titleLabel,BorderLayout.CENTER);

    // info card panel
    // nothing here yet

    JPanel buttonPanel=new JPanel(new GridLayout(1,0,5,0));
    for(Action action : actions){
        buttonPanel.add(new JButton(action));
        buttonPanel.setBackground(Color.BLACK);
    }

    cards.add(splashCard,CARD_LABELS[0]);
    cards.add(infoCard,CARD_LABELS[1]);
    cards.add(gameCard,CARD_LABELS[2]);

    add(cards,BorderLayout.CENTER);
    add(buttonPanel,BorderLayout.SOUTH);
}
// sets uniform panel size
    @Override
public Dimension getPreferredSize() {
    return new Dimension(PREF_W, PREF_H);
}
// shows the Main Menu card
private class ShowMainAction extends AbstractAction {
    public ShowMainAction() {
        super("Main");
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        cardlayout.show(cards,CARD_LABELS[0]);
    } 
}
// shows the Info card
private class ShowInfoAction extends AbstractAction {
    public ShowInfoAction() {
        super("Info");
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        cardlayout.show(cards,CARD_LABELS[1]);
    }
}
// show the Game card
private class ShowGameAction extends AbstractAction {
    public ShowGameAction() {
        super("Game");
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        cardlayout.show(cards,CARD_LABELS[2]);
    }
}
private class TimerListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent event){
        if(source==gButton){
            gButton.setBackground(Color.GREEN);
        }
        else if(source==rButton){
            rButton.setBackground(Color.RED);
            rButton.setForeground(Color.WHITE);
        }
        else if(source==yButton){
            yButton.setBackground(Color.YELLOW);
        }
        else if(source==bButton){
            bButton.setBackground(Color.BLUE);
            bButton.setForeground(Color.WHITE);
        } 
    }
}
private class ColorButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent event){
        source=event.getSource();
        int delay=300;
        timer=new Timer(delay,new TimerListener());

        if(source==gButton){
            gButton.setBackground(Color.WHITE);
            timer.setRepeats(false);
            timer.start();
        }
        else if(source==rButton){
            rButton.setBackground(Color.WHITE);
            rButton.setForeground(Color.BLACK);
            timer.setRepeats(false);
            timer.start();
        }
        else if(source==yButton){
            yButton.setBackground(Color.WHITE);
            timer.setRepeats(false);
            timer.start();
        }
        else if(source==bButton){
            bButton.setBackground(Color.WHITE);
            bButton.setForeground(Color.BLACK);
            timer.setRepeats(false);
            timer.start();
        }     
    }
}
private class StartListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        // calls generateSequence() to make pattern for player to replicate
        // for debugging in output
        System.out.println(Arrays.toString(generateSequence()));
    }
}
public int[] generateSequence(){
    Random ran=new Random();
    ComputerListener cpu=new ComputerListener();
    computer=new javax.swing.Timer(500,cpu);
    int seqLen=4;
    int[] gameSequence=new int[seqLen];
    for(int x=0;x<seqLen;x++){
       int assign=ran.nextInt(4)+1;
       gameSequence[x]=assign;
    }
    for(int y=0;y<seqLen;y++){ // print and wait 1/2 second, repeat 3 times
        computer.start();
    }
    //computer.stop(); // should stop ComputerListener()???
    return gameSequence;
}
private class ComputerListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        // for debugging in output
        System.out.println("it worked");
    }
}
}

【问题讨论】:

    标签: java swing timer


    【解决方案1】:

    您在 for 循环中多次调用计算机 Swing Timer 的开始按钮,这不是您想要做的,事实上,计时器的全部目的是帮助你摆脱 for 循环。相反,计时器重复一个动作,改变一个状态,并一直持续到完成。考虑使用 int 数组或更好的 ArrayList 来保存计时器应迭代的颜色,并在该 ActionListener 中执行操作并将指针前进到数组或 List 中的下一个位置,使用该指针来决定要执行的操作接下来做。然后当指针完全通过集合时,停止 Timer。

    对于我所描述的完全示例,请在此处查看我的 Timer 的 ActionListener 以了解不完整的西蒙游戏:Method keeps window from closing

    Timer 的 ActionListener 注释如下:

    private class TimerListener implements ActionListener {
        private SimonPanel simonPanel;  // the Simon JPanel
        private int colorListIndex = 0; // index into the ArrayList of MyColor objects
        private int sliceCount = 0;
        private List<MyColor> myColorList; // the MyColor ArrayList -- the random colors to press
        private int maxCount;
    
        public TimerListener(SimonPanel simonPanel, List<MyColor> myColorList) {
            // pass in the key fields into the program via constructor parameter
            this.simonPanel = simonPanel;
            this.myColorList = myColorList;  // again the ArrayList that holds random MyColor objects
            maxCount = myColorList.size();  // size of my list
        }
    
        @Override
        public void actionPerformed(ActionEvent evt) {
            // if index at the end of the list -- get out and clean up
            if (colorListIndex == maxCount) {
                // clear the display of "pressed" colors
                for (MyColor myColor : MyColor.values()) {
                    simonPanel.setMyColorPressed(myColor, false);
                }
    
                // stop this timer
                ((Timer) evt.getSource()).stop();
                return;
            }
    
            // the listener is a little complex since it must turn on colors and turn them off 
            // which is why I use a sliceCount int counter variable here
            if (sliceCount == 0) {
                // turn on the next color in the list (using the index)
                MyColor myColor = myColorList.get(colorListIndex);
                simonPanel.setMyColorPressed(myColor, true);
                sliceCount++;
            } else if (sliceCount < TIME_SLICES - 1) {
                sliceCount++;
                return;
            } else if (sliceCount == TIME_SLICES - 1) {
                sliceCount = 0;
                MyColor myColor = myColorList.get(colorListIndex);
                simonPanel.setMyColorPressed(myColor, false);  // turn off the color 
                colorListIndex++;  // and increment the index
                return;
            }
        }
    }
    

    【讨论】:

    • 不需要两个给出相同建议的答案。
    • 我才刚刚开始真正使用 GUI,而 Timer 对象对我来说是新的。您的解决方案有很大帮助。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多