I have a Java Poker Project. I programmed two JFrame
s for the game and when you run the project, displays the JFrame
s together instead of the first and when it's done the second. Any ideas?
Asked
Active
Viewed 787 times
-5

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
3I might have an idea, sadly you did not show the (relevant parts of your) code. – home Jan 07 '13 at 17:44
-
Well, call setVisible(true) on the second frame only when you are "done" with the first... – lbalazscs Jan 07 '13 at 17:48
-
1Present [SSCCE](http://sscce.org/). – Adam Stelmaszczyk Jan 07 '13 at 17:49
1 Answers
2
See The Use of Multiple JFrames, Good/Bad Practice? Instead use a modal dialog for the first 'frame'. This example uses a JOptionPane
.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class TwoStageGUI {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, "Gratuitous splash screen");
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(20, 200, 20, 200));
gui.add(new JLabel("Play!"));
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Game");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See https://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}

Community
- 1
- 1

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
1The Use of Multiple Frames is a classic, right up there with Charles Dickens. "It was the best of code, it was the worst of code." – Gilbert Le Blanc Jan 07 '13 at 18:24