2015-11-05 50 views
0

我有一個主類hello.java和一個Jframe welcome.java。 我想從hello.Java文件中調用Welcome.java文件。 Hello.java顯示一個Splashscreen,所以在顯示啓動畫面之後,我想讓我的Jframe顯示出來。我做了DIS在從另一個類調用Jframe

hello.java`

package splash; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 


/** 
* 
* @author Admin 
*/ 
public class Splash { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    final JDialog frame = new JDialog(new JFrame()); 
    frame.setSize(640, 360); 
    frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\try5.gif"))); 
    frame.setUndecorated(true); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 

    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      sleepThread(); 
      CloseDialog(frame); 
     // System.exit(0); 

//編輯點評此行 }} )開始();

ShowDialog(frame); 

} 

private static void sleepThread() { 
    try { 
     Thread.sleep(5000); 
    } catch (InterruptedException ex) { 
     // Do something, if there is a exception 
     System.out.println(ex.toString()); 
    } 
} 

private static void ShowDialog(final JDialog dialog) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      dialog.setVisible(true); 
     } 
    }); 
} 

private static void CloseDialog(final JDialog dialog) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      dialog.setVisible(false); 
     } 
    }); 
    welcome wel = new welcome(); 
    welcome.setNimbusFeel(); 
    java.awt.EventQueue.invokeLater(new Runnable() 
      { 
       public void run(){ 
        new welcome().setVisible(true); 
       } 
      } 
); 
} 

}

和DIS在welcome.java

package splash; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
/** 
* 
* @author Admin 
*/ 
public class welcome extends javax.swing.JFrame { 
final JDialog frame = new JDialog(new JFrame()); 

/** 
* Creates new form welcome 
*/ 
public welcome() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jToggleButton1 = new javax.swing.JToggleButton(); 
    jRadioButton1 = new javax.swing.JRadioButton(); 
    jComboBox1 = new javax.swing.JComboBox(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("jLabel1"); 

    jLabel2.setText("jLabel2"); 

    jToggleButton1.setText("jToggleButton1"); 

    jRadioButton1.setText("jRadioButton1"); 

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(60, 60, 60) 
        .addComponent(jToggleButton1)) 
       .addGroup(layout.createSequentialGroup() 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(82, Short.MAX_VALUE)) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addGap(69, 69, 69) 
      .addComponent(jRadioButton1) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jLabel2) 
      .addGap(107, 107, 107)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap(67, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING) 
       .addComponent(jRadioButton1, javax.swing.GroupLayout.Alignment.TRAILING)) 
      .addGap(53, 53, 53) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jToggleButton1) 
        .addGap(27, 27, 27) 
        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void setNimbusFeel() { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new welcome().setVisible(true); 

//編輯評論此線// } }); }

// Variables declaration - do not modify      
private javax.swing.JComboBox jComboBox1; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JRadioButton jRadioButton1; 
private javax.swing.JToggleButton jToggleButton1; 
// End of variables declaration     
} 

+0

問題是什麼? – Stefan

+0

我需要調用Jframe從類dat顯示啓動屏幕 –

+0

Thx fr的回覆。但只是刪除系統退出(0)frm主類和welcome.setvisible(true)在框架類給了一個完美的輸出。 –

回答

0
  1. create JDialog as new JDialog(null)
  2. remove System.exit(0); from Thread's run() method

那麼,事情的順序應該是某事像這樣:
1. create splash (splash=new JDialog(null))
2. show it (splash.setVisible(true))
3. create your main window (frame=new MyWindow())
4.隱藏splah
5.展會主要