2013-01-07 46 views
-4

我有一個Java撲克項目。我爲遊戲編寫了兩個JFrame s,當你運行這個項目時,一起顯示JFrame,而不是第一個,當它完成第二個時。有任何想法嗎?使用JFrame撲克項目

+3

我可能有一個想法,可惜你沒有顯示(你的)相關部分的代碼。 – home

+0

那麼,只有在第一幀「完成」時,纔會在第二幀調用setVisible(true)... – lbalazscs

+1

Present [SSCCE](http://sscce.org/)。 –

回答

2

請參閱The Use of Multiple JFrames, Good/Bad Practice?而是爲第一個「框架」使用模態對話框。本例使用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); 
    } 
} 
+1

多幀的使用是一個經典的,在那裏與查爾斯狄更斯。 「這是最好的代碼,它是最糟糕的代碼。」 –