Issue
I'm a beginner to Java, and I'm trying to create a simple virtual library with a random book selection feature, which will occur at the click of a button named "Random".
So far in my project, I have a login form, register form, homepage and 3 books contained in their own respective jFrames. In the homepage jFrame, I would like to have a button that will randomly choose one of the 3 book jFrames and open it. However, it shouldn't be able to open any of the other jFrames (login and register). Is there a way that I can achieve this? I am using Netbeans IDE 12.2
Solution
Nevermind, I figured it out myself, I first used a random number generator and then paired it with a few if-statements to randomly open a page, its not the most elegant solution but it works:
private void randomActionPerformed(java.awt.event.ActionEvent evt) {
int random_int = (int)(Math.random() * (3 - 1 + 1) + 1);
if(random_int == 1){
dispose();
new book1().setVisible(true);
}
else if(random_int == 2){
dispose();
new book2().setVisible(true);
}
else if(random_int == 3){
dispose();
new book3().setVisible(true);
}
}
Answered By - Schwiro
Answer Checked By - David Marino (JavaFixing Volunteer)