I've just started coding in java swing today, so I'm very newbie and sorry if my question is silly. I have searched the net a lot but nothing was found.
My problem is that I can't make a Jfraim invisible by setVisible(false)
.
The code is very simple. a window with just a button that after being clicked it will show a showMessageDialog
"Hello World" and I want the window to be invisible after that.
here is my code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Temp extends JFrame{
private JPanel panel1;
private JButton button1;
private Temp() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
JOptionPane.showMessageDialog(null, "Hello World");
}
});
}
public static void main(String[] args) {
JFrame tempWindow = new JFrame("TempWindow");
tempWindow.setContentPane(new Temp().panel1);
tempWindow.setLocationRelativeTo(null); // this line set the window in the center of the screen
tempWindow.setDefaultCloseOperation(tempWindow.EXIT_ON_CLOSE);
tempWindow.pack();
tempWindow.setVisible(true);
}
}
I don't know what I'm doing wrong. I did everything just like this youtube video but my window won't get invisible after clicking the button.
any help would be appreciated.