2017-08-29 1 views
0

Ich erstelle einen neuen Splash-Bildschirm in Java (mit Netbeans als meine IDE).Fehler im Begrüßungsbildschirm in Java/Swing

Aber das Problem ist, dass nach dem Erreichen 100% von Fortschrittsbalken automatisch zum zweiten Frame bewegt wird und den ersten Frame, der den Begrüßungsbildschirm zeigt, verlassen. Aber hier wird der erste Bildschirm des Begrüßungsbildschirms nicht verlassen.

Dies ist mein Code

public NewJFrame() { 
     initComponents(); 
     try { 
      for (int i=0;i<=100;i++){ 
       Thread.sleep(40); 
      jLabel1.setText(Integer.toString(i)+"%"); 
      jProgressBar2.setValue(i); 
      if (i==100) 
      { 
       this.setVisible(false); 
       new NewJFrame1().setVisible(true); 
      } 
      } 
     } catch (Exception e) { 
     } 
    } 
+0

Siehe die SO-Beitrag mit dem Titel: [Wie man einen Splash machen s creen für GUI?] (https://stackoverflow.com/questions/16134549/how-to-make-a-splash-screen-for-gui). – DevilsHnd

Antwort

0

ich einmal eine einfache spritzen, nicht zu Phantasie geschaffen, aber für mich ist es den Trick.

Dies ist eine Kopie einfügen aus der gesamten Klasse von Netbeans, so dass Sie etwas ändern müssen.

In diesem Beispiel erhalten Sie einen Splash mit einem Bild und einem Fortschrittsbalken unten.

import javax.swing.SwingUtilities; 

public class Splash extends javax.swing.JWindow { 

private static final long serialVersionUID = -6711792976773608816L; 

/** 
* Creates new form Splash 
*/ 
public Splash() { 
    initComponents(); 
} 

public void splashScreenInit() { 
    setLocationRelativeTo(null); 
    setProgressMax(100); 
    setVisible(true);   
} 

/** 
* 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("all") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    imageLabel = new javax.swing.JLabel(); 
    progressBar = new javax.swing.JProgressBar(); 

    imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help/splash.png"))); // NOI18N 
    getContentPane().add(imageLabel, java.awt.BorderLayout.CENTER); 

    progressBar.setForeground(new java.awt.Color(51, 85, 112)); 
    progressBar.setStringPainted(true); 
    getContentPane().add(progressBar, java.awt.BorderLayout.PAGE_END); 

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

/** 
* Sets the max value for the progressbar 
* @param maxProgress 
*/ 
public void setProgressMax(int maxProgress) { 
    progressBar.setMaximum(maxProgress); 
} 

public void setProgress(int progress) { 
    final int theProgress = progress; 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      progressBar.setValue(theProgress); 
     } 
    }); 
} 

/** 
* Changes the string message and set a new value fro the progressbar 
* @param message String the message 
* @param progress int the new value of the progressbar. This cant be higher than the value defined in the setProgressMax 
*/ 
public void setProgress(String message, int progress) { 
    final int theProgress = progress; 
    final String theMessage = message; 
    setProgress(progress); 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      progressBar.setValue(theProgress); 
      setMessage(theMessage); 
     } 
    }); 
} 

private void setMessage(String message) { 
    if (message == null) { 
     message = ""; 
     progressBar.setStringPainted(false); 
    } else { 
     progressBar.setStringPainted(true); 
    } 
    progressBar.setString(message); 
} 

// Variables declaration - do not modify      
private javax.swing.JLabel imageLabel; 
private javax.swing.JProgressBar progressBar; 
// End of variables declaration     
} 

Um

final Splash s = new Splash(); 
SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
     s.splashScreenInit(); 
    } 
}); 

und im

Ende zu nennen
s.setVisible(false); 
0

folgende ist mein Code, um es richtig in meinem System funktioniert .. u versuchen, hinzufügen können, um Ihre Import die folgenden in Codes

//import com.sun.awt.AWTUtilities; 
//import java.awt.event.ActionEvent; 
//import javax.swing.Timer; 
//import javax.swing.UIManager; 
//import java.awt.event.ActionListener; 
//import javax.swing.JProgressBar; 



    private Timer t; 
     private ActionListener al; 

     /** 
     * Creates new form NewJFrame 
     */ 

     public NewJFrame() { 

      ActionListener a1 = new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
       if(jProgressBar1.getValue()<100) 
        { 
        jProgressBar1.setValue(jProgressBar1.getValue()+5); 
        } 
       else 
        { 
         t.stop(); 
         abcd(); 
        } 
       } 
      }; 
     t=new Timer(80, a1); 
     initComponents(); 
     AWTUtilities.setWindowOpaque(this, false); 
     t.start(); 
     } 

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

      jPanel1 = new javax.swing.JPanel(); 
      jLabel1 = new javax.swing.JLabel(); 
      jProgressBar1 = new javax.swing.JProgressBar(); 

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
      setUndecorated(true); 

      jLabel1.setText("jLabel1"); 

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
      jPanel1.setLayout(jPanel1Layout); 
      jPanel1Layout.setHorizontalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(jPanel1Layout.createSequentialGroup() 
          .addGap(123, 123, 123) 
          .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGroup(jPanel1Layout.createSequentialGroup() 
          .addGap(92, 92, 92) 
          .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
        .addContainerGap(141, Short.MAX_VALUE)) 
      ); 
      jPanel1Layout.setVerticalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(66, 66, 66) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(41, 41, 41) 
        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(133, Short.MAX_VALUE)) 
      ); 

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
      getContentPane().setLayout(layout); 
      layout.setHorizontalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      ); 
      layout.setVerticalGroup(
       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      ); 

      pack(); 
     }// </editor-fold>       
    private void abcd() 
    { 
     NewJFrame1 f1= new NewJFrame1(); 
     f1.setLocationRelativeTo(null); 
    f1.setVisible(true); 
    this.dispose(); 
    } 
Verwandte Themen