【问题标题】:JButton does not change icon when pressed memory game按下记忆游戏时JButton不会更改图标
【发布时间】:2015-07-12 05:32:04
【问题描述】:

我正在尝试制作自己的简单记忆游戏,并且我有一个 JPanel,它实现了 ActionListener 接口并包含一些从 JButton 继承的 Tiles。几乎所有东西都可以正常工作,除了作为第二个按钮被点击的事实之外,它并没有像它应该做的那样改变它的图标。但是,当我单击两个匹配的图块时,它们仍然翻转并且没问题。当我单击第一个按钮时,它会翻转,并且第二个按下的图块如果它不匹配,则其行为类似于未添加 ActionListener 的按钮。我想过用 PropertyChangeListener 解决这个问题,但不知道如何解决。

这是我的 JPanel 中的方法:

@Override
    public void actionPerformed(ActionEvent e){

        Tile tile = (Tile)e.getSource();

        if(tilesFlipped < 2 ){
            tile.flip();
            flippedTiles[tilesFlipped++] = tile;

            if(tilesFlipped == 1){
                return;
            }

            if(flippedTiles[0].equals(flippedTiles[1])){
                tilesFlipped = 0;
                return;

            } else {
                try {
                    Thread.sleep(1000);
                    for(int i = 0; i < 2; i++){
                        flippedTiles[i].flipBack();
                    }
                } catch (InterruptedException e1) {
                }   
            }
            tilesFlipped = 0;
        } 

    }

这是我的 Tile 课程:

private class Tile extends JButton {
    String photoPath;
    ImageIcon photo;

    Tile(int i){
        photoPath = String.format("/home/stn/Desktop/p/9-%d.jpg", i);
        setPreferredSize(new Dimension(150, 150));
        this.setBackground(Color.cyan);
        this.photo = new ImageIcon(photoPath);
    }

    public void flip(){
        this.setIcon(photo);
        this.setBackground(Color.white);
    }

    public void flipBack(){
        this.setBackground(Color.cyan);
        this.setIcon(null);

    }

    public String getPhotoPath(){
        return photoPath;
    }

    @Override
    public boolean equals(Object o){
        return o instanceof Tile && ((Tile)o).getPhotoPath().equals(photoPath);
    }
    @Override
    public int hashCode(){
        return photoPath.hashCode();
    }

}

【问题讨论】:

  • 看起来tilesFlipped总是为0——我们这里没有所有代码,但这看起来像是一个错误。
  • @dbillz tilesFlipped 在游戏开始时为 0,然后似乎工作正常(当按下按钮等于最后点击一个时,我没有阻止这种情况,但无论如何更改按钮图标应该可以工作)跨度>
  • 更新,问题几乎肯定是由 Thread.sleep() 方法引起的,但我不知道为什么以及如何解决这个问题。 Thread.sleep() 的最佳选择是什么?
  • 你为什么在那儿睡一秒钟?你想达到什么目的?也可能想放弃这个问题并提出一个新问题,因为除了我之外没有人看到。
  • @dbillz 我正试图让两个翻转的瓷砖在那段时间之后翻转回来,因为它在体面的记忆游戏中:)

标签: java swing


【解决方案1】:

使用Swing Timer 代替Thread.sleep(...)。了解Thread.sleep(...) 使当前线程进入睡眠状态,这里是 Swing Event Dispatch ThreadEDT,这将使您的整个 GUI 进入睡眠状态,冻结您的应用程序。 Swing Timer 将执行一个或重复的操作,而不会占用 EDT。例如:

 public void actionPerformed(ActionEvent e) {

    Tile tile = (Tile) e.getSource();

    if (tilesFlipped < 2) {
       tile.flip();
       flippedTiles[tilesFlipped++] = tile;

       if (tilesFlipped == 1) {
          return;
       }

       if (flippedTiles[0].equals(flippedTiles[1])) {
          tilesFlipped = 0;
          return;

       } else {
          Timer timer = new Timer(1000, new ActionListener() {

             @Override
             public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 2; i++) {
                   flippedTiles[i].flipBack();
                }
                tilesFlipped = 0;
             }
          });
          timer.setRepeats(false);
          timer.start();
       }
    }

 }

【讨论】:

  • 非常感谢,这完全解决了我的问题,事实上我最后用摆动计时器修复了它,但解决方案并不那么顺利。
猜你喜欢
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多