简体   繁体   中英

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

So i'm almost at the finish line right now and having trouble with stopping my music when choosing a new song or to play a new song.

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

        }
    }
});

As you guys can see. there is two buttons, one for Play button and one for Open button ( Open button has a method where the FileChooser and so on is so there is nothing special there) however. I named a method for stop() where the music stops when it should too. I tried if the function worked and it does so there is nothing wrong with the method but this code.

As you guys see I might be confused with the booleans and what I'm trying to do is to make something like this:

First I pick a song so I use the Open button and choose a file. then I press Play to get the song playing. (Here ->) so Whenever I play Open again, the music should stop. thats what I was thinking to do but I can't get it to work. I might be blind right now but all help would be appreciated!

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


                        }
                    }
                }
            });

The issue that happends now is that I have to double click on Open and Play to make it work

EDIT PART 2.0 ( Issue with Open button where I have to double click for it)

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 = false

EDIT part 4.0 got it to work by removing if-else statement in Openbutton!

You have 2 mistakes

  • if(thread2Status = true) you are assigning, not comparing! just use if(thread2Status)
  • you never update your boolean flag, so the flow of the program in the if is always the same

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

and

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM