布尔值 - 它不会在应该停止的时候停止 (JAVA)

Boolean - It doesn't stop when it should (JAVA)

所以我现在快到终点线了,在选择一首新歌或播放一首新歌时无法停止我的音乐。

static boolean thread2Status = false;

btnPlay.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnPlay) {
            if(thread2Status) {
                mp3_player.play();
                lblPlaying.setText("Enjoy the music!");

            }
            else if(!thread2Status) {
                stop();
            }
        }

    }       
});

btnOpen.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnOpen) {

            try {
                if(thread2Status = true) {
                    Choose();


                } else if(!thread2Status) {
                    stop();
                }

            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }
});

如你们所见。有两个按钮,一个用于 Play 按钮,一个用于 Open 按钮(Open 按钮有一个方法FileChooser 等所在的位置因此没有什么特别之处)但是。我为 stop() 命名了一个方法,音乐在该停止的时候停止。我试过该功能是否有效,它确实如此,该方法没有任何问题,但这段代码。

正如你们所见,我可能对布尔值感到困惑,我想做的是做这样的事情:

首先我选择了一首歌曲,所以我使用 打开 按钮并选择一个文件。然后我按 Play 播放歌曲。 (这里 ->)所以每当我再次播放 Open 时,音乐应该停止。那就是我想做的,但我无法让它工作。我现在可能是盲人,但我们将不胜感激!

编辑 1.1:

btnPlay.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == btnPlay) {
                        if(thread2Status) {
                            mp3_player.play();
                            lblPlaying.setText("Enjoy the music!");
                        }
                        else if(!thread2Status) {
                            stop();
                        }
                        thread2Status = !thread2Status;  // this line switches boolean
                    }
                }
            });

            btnOpen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(e.getSource() == btnOpen) {

                        try {
                            if(thread2Status) { // not necessary to explicit compare booleans
                                Choose();
                            } else if(!thread2Status){
                                stop();
                            }
                            thread2Status = !thread2Status;  // this line switches boolean


                        } catch (IOException e1) {
                            e1.printStackTrace();


                        }
                    }
                }
            });

现在发生的问题是我必须双击 打开播放 才能正常工作

编辑第 2.0 部分(打开按钮出现问题,我必须双击它)

btnOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == btnOpen) {
                    try {
                        if(sant) { 
                            Choose();
                        } else{
                            stop();
                        }
                        sant = !sant;  


                    } catch (IOException e1) {
                        System.out.println("we been balling");


                    }
                }
            }
        });

桑特=真, falsk = false

编辑第 4.0 部分通过删除 Openbutton 中的 if-else 语句使其正常工作!

你有 2 个错误

  • if(thread2Status = true)你是赋值,不是比较!只需使用 if(thread2Status)
  • 你永远不会更新你的 boolean 标志,所以 if 中的程序流程总是相同的

if (e.getSource() == btnPlay) {
     if(thread2Status) {
          mp3_player.play();
          lblPlaying.setText("Enjoy the music!");
     }
     else {
          stop();
    }
    thread2Status = !thread2Status;  // this line switches boolean
}

if(e.getSource() == btnOpen) {
    try {
        if(thread2Status) { // not necessary to explicit compare booleans
            Choose();
        } else {
            stop();
        }
        thread2Status = !thread2Status;  // this line switches boolean


    } catch (IOException e1) {
        e1.printStackTrace();
    }

}