2014-06-17 2 views
1

스윙을 처음 사용하고 다른 클래스의 창을 시작하는 데 문제가 있습니다. 같은 클래스에있는 창 개체를 새로 만들면 다른 클래스에서 시작하려고하면 빈 상태가됩니다. 다른 클래스에서 사용하면 스윙 창이 비어집니다.

내가 뭘하려했으나 제대로 동작하지 않습니다 : 여기에

ProgressWindow dow = new ProgressWindow(this, null); 

내 스윙 창에 대한 코드 : 여기

package example; 

import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class ProgressWindow extends javax.swing.JFrame { 

    private final JPanel panel; 
    private final javax.swing.JLabel jLabel1; 
    private final javax.swing.JLabel jLabel2; 
    private String message = "Vorgang läuft..."; 
    private final int maxLength = 30; 

    public ProgressWindow(JFrame frame, String pMessage) { 
     super("Bitte warten"); 
     //Set Message 
     setMessage(pMessage); 
     //Labels und panel anlegen 
     panel = new JPanel(new GridLayout(2, 1)); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     //Position 
     this.setLocationRelativeTo(frame); 
     //Close-Operation 
     setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); 
     //Size 
     setMaximumSize(new java.awt.Dimension(240, 90)); 
     setMinimumSize(new java.awt.Dimension(240, 90)); 
     setResizable(false); 
     //Content 
     jLabel1.setText(message); 
     jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xyz/ajax-loader.gif"))); 
     //Alignment 
     jLabel1.setHorizontalAlignment(JLabel.CENTER); 
     jLabel2.setHorizontalAlignment(JLabel.CENTER); 
     //Finish Layout 
     panel.add(jLabel1); 
     panel.add(jLabel2); 
     getContentPane().add(panel); 
     pack(); 
     setVisible(true); 
    } 

    private void setMessage(String pMessage) { 
     if (pMessage != null) { 
      //Cut string if too long 
      if (pMessage.length() > maxLength) { 
       pMessage = pMessage.substring(0, maxLength) + "..."; 
      } 
      this.message = pMessage; 
     } 
    } 

    //This works as expected: 
    public static void main(String[] args) { 
     try { 
      ProgressWindow pr = new ProgressWindow(null, null); 
      Thread.sleep(2000); 
      pr.dispose(); 
     } 
     catch (InterruptedException ex) { 
     } 
    } 
} 

신속하고 더러운 예를-클래스는 내가 넷빈즈로 만든 GUI-Builder는 작동하지 않는 코드를 보여줍니다.

package example; 

public class Otherclass extends javax.swing.JFrame { 

    public Otherclass() { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("jButton1"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(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() 
       .addContainerGap() 
       .addComponent(jButton1) 
       .addContainerGap(317, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jButton1) 
       .addContainerGap(266, Short.MAX_VALUE)) 
     ); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     try { 
      ProgressWindow pr = new ProgressWindow(this, null); 
      Thread.sleep(2000); 
      pr.dispose(); 
     } catch (InterruptedException ex) { 
      System.err.println(ex); 
     } 
    }           

    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 
     */ 
     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(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Otherclass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Otherclass.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 Otherclass().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    // End of variables declaration     
} 
+0

당신이 제공 할 수는는 [MCVE은] (http://stackoverflow.com/help/mcve) 즉, **하지 않는 코드를 보여줍니다 ** 작동합니까? –

+0

우리와 공유 할 수있는 예외가 있습니까? –

+0

ProgressWindow를 호출 할 때 두 번째 클래스를 제공 할 수 있습니까? – dARKpRINCE

답변

2

사용 Swing Timer 대신 Thread.sleep(2000).

How to Use Swing Timers

샘플 코드를 읽기 :

final ProgressWindow pr = new ProgressWindow(null, null); 
Timer timer=new Timer(2000,new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     pr.dispose(); 
    } 
}); 
timer.setRepeats(false); 
timer.start(); 
+0

그것은 나를 위해 속임수를했다. 고마워요! – AndreasBerliner

0

const로 전달 된 프레임을 사용해야합니다 루터.

변경에 생성자에서 이러한 호출 : 스윙 응용 프로그램에서

frame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); 
    //Size 
    frame.setMaximumSize(new java.awt.Dimension(240, 90)); 
    frame.setMinimumSize(new java.awt.Dimension(240, 90)); 
    frame.setResizable(false); 

    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
관련 문제