Java 未显示JFrame的内容

Java 未显示JFrame的内容,java,swing,user-interface,jframe,visibility,Java,Swing,User Interface,Jframe,Visibility,我使用框架显示消息,但框架内容不显示。 下面是我的MessageBox框架的代码 public class MessageBox extends javax.swing.JFrame { /** * Creates new form MessageBox */ public static String title=null,message=null; public MessageBox(String message,String title) { initComponents

我使用框架显示消息,但框架内容不显示。 下面是我的MessageBox框架的代码

public class MessageBox extends javax.swing.JFrame {

/**
 * Creates new form MessageBox
 */
 public static String title=null,message=null;

public MessageBox(String message,String title) {

    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() {

    txtMessage = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(java.awt.event.WindowEvent evt) {
            formWindowOpened(evt);
        }
    });

    txtMessage.setBackground(new java.awt.Color(236, 233, 216));
    txtMessage.setOpaque(false);
    txtMessage.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtMessageActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(18, 18, 18)
            .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(31, 31, 31)
            .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(24, Short.MAX_VALUE))
    );

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

private void txtMessageActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

private void formWindowOpened(java.awt.event.WindowEvent evt) {
    // TODO add your handling code here:
   this.setTitle(title);
   txtMessage.setText(message);
   txtMessage.revalidate();
   txtMessage.repaint();
   this.repaint();
}

/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify
private javax.swing.JTextField txtMessage;
// End of variables declaration

public static void main(String args[]) {
    /*
     * 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
     */

    //</editor-fold>

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            //  new ERPMainMenu().setVisible(true);
          new MessageBox(title,message).setVisible(true);
        }
    });
}
改为在
Thread.sleep()
上使用。您的所有代码如下所示:

MessageBox.java

import javax.swing.*;
import java.awt.event.*;
public class MessageBox extends javax.swing.JFrame 
{
    /**
    * Creates new form MessageBox
    */
    public static String title=null,message=null;
    public MessageBox(String message,String title) 
    {
        this.message = message;
        this.title = title;
        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() 
    {

        txtMessage = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() 
        {
            public void windowOpened(java.awt.event.WindowEvent evt) 
            {
                formWindowOpened(evt);
            }
        });
        txtMessage.setBackground(new java.awt.Color(236, 233, 216));
        txtMessage.setOpaque(false);
        txtMessage.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                txtMessageActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
        .addGap(18, 18, 18)
        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
        .addGap(31, 31, 31)
        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(24, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>
    private void txtMessageActionPerformed(java.awt.event.ActionEvent evt) 
    {
        // TODO add your handling code here:
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) 
    {
        // TODO add your handling code here:
        this.setTitle(title);
        txtMessage.setText(message);
        txtMessage.revalidate();
        txtMessage.repaint();
        this.repaint();
    }
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify
    private javax.swing.JTextField txtMessage;
    // End of variables declaration
    public static void main(String args[]) 
    {
        /*
        * 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
        */

        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                CallingClass cl = new CallingClass();
                cl.caller();
            }
        });
    }
}
注意:根据Camickr的建议,我已经在显示
JFrame
之前启动了计时器。这将提高持续时间(5秒)的准确性 在您的情况下,
JFrame
将可见


为什么不使用JOptionPane?它已经有了显示消息对话框的方法。我想在五秒钟后在用户干扰下消失消息框,而JOptionPane不支持+1,但是应该在显示窗口之前启动计时器。通过这种方式,代码将适用于JOptionPane或模态对话框,它们应该用作弹出窗口,而不是JFrame。
import javax.swing.*;
import java.awt.event.*;
public class MessageBox extends javax.swing.JFrame 
{
    /**
    * Creates new form MessageBox
    */
    public static String title=null,message=null;
    public MessageBox(String message,String title) 
    {
        this.message = message;
        this.title = title;
        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() 
    {

        txtMessage = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() 
        {
            public void windowOpened(java.awt.event.WindowEvent evt) 
            {
                formWindowOpened(evt);
            }
        });
        txtMessage.setBackground(new java.awt.Color(236, 233, 216));
        txtMessage.setOpaque(false);
        txtMessage.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                txtMessageActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
        .addGap(18, 18, 18)
        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
        .addGap(31, 31, 31)
        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(24, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>
    private void txtMessageActionPerformed(java.awt.event.ActionEvent evt) 
    {
        // TODO add your handling code here:
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) 
    {
        // TODO add your handling code here:
        this.setTitle(title);
        txtMessage.setText(message);
        txtMessage.revalidate();
        txtMessage.repaint();
        this.repaint();
    }
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify
    private javax.swing.JTextField txtMessage;
    // End of variables declaration
    public static void main(String args[]) 
    {
        /*
        * 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
        */

        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                CallingClass cl = new CallingClass();
                cl.caller();
            }
        });
    }
}
class CallingClass
{
    public CallingClass(){}
    public void caller()
    {
        final MessageBox mb =new MessageBox("The  Data is saved successfully", "Success");
        mb.setLocation(400, 300);   
        javax.swing.Timer timer = new javax.swing.Timer(5000,new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                mb.setVisible(false);
            }
        });
        timer.start();
        mb.setVisible(true);
    }
}