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

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();
            }

        }
    }
});


你们可以看到。但是有两个按钮,一个是“播放”按钮,一个是“打开”按钮(“打开”按钮的方法是FileChooser等,因此没有什么特别的地方)。我为stop()命名了一种方法,在该方法下,音乐也应在此停止。我试过了该函数是否起作用,但该方法没有什么问题,但这段代码没有问题。

就像你们看到的那样,我可能对布尔值感到困惑,而我正在尝试做的事情是这样的:

首先,我选择一首歌曲,然后使用“打开”按钮选择一个文件。然后按“播放”播放歌曲。 (在这里->),所以每当我再次播放“打开”时,音乐都会停止。多数民众赞成在我想做的事情,但我无法使其工作。我现在可能是盲人,但所有帮助将不胜感激!

编辑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");


                    }
                }
            }
        });


sant = true,
Falsk =假

编辑部分4.0通过删除Openbutton中的if-else语句使它起作用了!

最佳答案

你有两个错误


您正在分配的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();
    }

}

09-11 19:52
查看更多