从另一个类调用Jframe



我有一个主类hello.java和一个Jframe welcome.java。我想从hello调用Welcome.java文件。Java文件。Hello.java显示一个启动屏幕,所以在显示启动屏幕之后,我希望我的Jframe能够显示出来。我在中做了dis

你好.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);

//编辑此行的注释}}).start();

    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                   
}

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

那么,事情的顺序应该是这样的:
1.show it (splash.setVisible(true))
2.create your main window (frame=new MyWindow())
3.CCD_6
4.隐藏斑点
5.显示主

最新更新