2013-03-14 4 views
1

메시지 표시에 프레임을 사용하고 있지만 프레임의 내용은 표시되지 않습니다. 다음은 내 MessageBox 프레임 코드입니다.JFrame의 내용이 표시되지 않습니다.

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); 
     } 
    }); 
} 

}

 public Class CallingClass 
    { 
     public CallingClass(){ 
    } 
public caller(){ 
     MessageBox mb =new MessageBox("The Data is saved successfully", "Success"); 

      mb.setLocation(400, 300); 
      mb.setVisible(true); 
     try { 
      Thread.sleep(5000); 
} catch(InterruptedException ex) { 
Thread.currentThread().interrupt(); 
} 
      mb.setVisible(false); 

    } 
    } 
+2

왜 JOptionPane을 사용하지 않습니까? 이미 메시지 대화 상자를 표시하는 메소드가 있습니다. –

+0

사용자 간섭으로 5 초 후에 메시지 상자가 사라지고 JOptionPane이 –

답변

3

대신 Thread.sleep()에 대한 사용 javax.swing.Timer. 귀하의 모든 코드는 다음과 같습니다

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(); 
      } 
     }); 
    } 
} 



CallingClass.java

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); 
    } 
} 

참고 :를 Camic에 의해 제안 kr, 나는 JFrame이 표시되기 전에 타이머를 시작했습니다. 이렇게하면 JFrame이 표시 될 때까지 지속 시간의 정확성이 향상됩니다 (귀하의 경우 5 초 ).

+2

+1을 지원하지 않지만 창이 나타나기 전에 타이머가 시작되어야합니다. 이 방법은 JOptionPane 또는 모달 대화 상자에서 코드가 작동하므로 JFrame 대신 팝업 창으로 사용해야합니다. – camickr

+0

@camickr : 귀중한 제안에 감사드립니다 ... –

+0

@Syed Muhammad Mubas : 업데이트 된 게시물보기 .. –

관련 문제