简体   繁体   English

从另一个类调用Jframe

[英]Call a Jframe from another class

I have a main class hello.java and a Jframe welcome.java. 我有一个主类hello.java和一个Jframe welcome.java。 I wanted to call Welcome.java file from hello.Java file. 我想从hello.Java文件调用Welcome.java文件。 Hello.java displays a Splashscreen so after the splash screen is displayed i want my Jframe to get displayed. Hello.java显示一个启动画面,因此在显示启动画面后,我希望显示我的Jframe。 I did dis in 我确实在

hello.java` 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);

//edit comment this line } }).start(); //编辑此行的注释}})。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);
              }
          }
  );
}

} }

and dis in welcome.java 和dis in 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);

// edit comment this line // } }); //编辑此行的注释//}}); } }

// 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. create JDialog as new JDialog(null)
  2. remove System.exit(0); from Thread's run() method

then, the order of things should be sth like this: 然后,事物的顺序应该是这样的:
1. create splash (splash=new JDialog(null)) 1. create splash (splash=new JDialog(null))
2. show it (splash.setVisible(true)) 2. show it (splash.setVisible(true))
3. create your main window (frame=new MyWindow()) 3. create your main window (frame=new MyWindow())
4. hide splah 4.隐藏
5. show main 5.显示主要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM