When the user hits "close" in one JFrame frame
, I want the JFrame credits
to be displayed showing my name and stuff for 2.5 seconds before the program exits. Now, credits
is showing, but empty without the textArea
and the Button
- couldnt find whats wrong.
Heres my code:
For the closing operation of frame
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION){
credits.setVisible(true);
try {
Thread.sleep(2500);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.exit(0);
} else {
//do nothing
}
}
});
For credits
(just the class initializing the frame):
public class CreditsFrame extends JFrame {
Positioner pos = new Positioner();
private JPanel contentPane;
ImageIcon frameIcon = new ImageIcon("files/images/frameicon.png");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreditsFrame frame = new CreditsFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CreditsFrame() {
setIconImage(frameIcon.getImage());
setAlwaysOnTop(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(pos.posX(pos.screenX, 441), pos.posY(pos.screenY, 210), 441, 210);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JTextArea txtarea = new JTextArea();
txtarea.setBounds(10, 11, 415, 125);
txtarea.setEditable(false);
txtarea.setBackground(Color.WHITE);
txtarea.setWrapStyleWord(true);
txtarea.setLineWrap(true);
txtarea.append("created by & more here");
contentPane.setLayout(null);
contentPane.add(txtarea);
JButton btnOk = new JButton("Ok");
btnOk.setBounds(154, 147, 89, 23);
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
contentPane.add(btnOk);
}
}
Any suggestions for a quick fix? Thanks.
Any suggestions for a quick fix?
Yes, don't call Thread.sleep(...)
on the Swing event thread unless you want to put your entire GUI to sleep. Instead use a Swing Timer to handle the delay. Basically, the timer's ActionListener will be called after the milliseconds delay has passed.
e.g.,
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION){
credits.setVisible(true);
new Timer(2500, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit();
}
}).start();
} else {
//do nothing
}
}
});
Also, look at The Use of Multiple JFrames, Good/Bad Practice?
Other issues that will bite you in the future: