Ola Galera, sou novo por aki e meio iniciante em Java!
O problema eh o seguinte, estou desenvolvendo um programinha para a faculdade! utilizo o netbeans para fazer os frames, mas de modo pratico, utilizando as ferramentas do programa! Meu programa possui varios frames, e quero fazer um frame inicial, onde eu inicio meu programa, e quero colocar um jprogressbar para fingir um carregamento, da seguinte forma: Clico no botao, finge um carregamento no JprogressBar e chama o proximo frame!!
importjavax.swing.SwingWorker;/* * ProgressTest.java * * Created on 25 de Abril de 2008, 01:03 *//** * * @author Tadeu */publicclassProgressTestextendsjavax.swing.JFrame{/** Creates new form ProgressTest */publicProgressTest(){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. */// <editor-fold defaultstate="collapsed" desc="Generated Code">privatevoidinitComponents(){jProgressBar1=newjavax.swing.JProgressBar();jButton1=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jProgressBar1.setStringPainted(true);jButton1.setText("jButton1");jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton1ActionPerformed(evt);}});javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap(77,Short.MAX_VALUE).addComponent(jProgressBar1,javax.swing.GroupLayout.PREFERRED_SIZE,261,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(62,62,62)).addGroup(layout.createSequentialGroup().addGap(155,155,155).addComponent(jButton1).addContainerGap(172,Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(75,75,75).addComponent(jProgressBar1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(36,36,36).addComponent(jButton1).addContainerGap(149,Short.MAX_VALUE)));pack();}// </editor-fold>/*Tudo acontece aqui*/privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt){// TODO add your handling code here:SwingWorkerworker=newSwingWorker(){@OverrideprotectedObjectdoInBackground()throwsException{for(inti=0;i<=100;){jProgressBar1.setValue(i++);try{Thread.sleep(100);}catch(InterruptedExceptione){e.printStackTrace();}}returnnull;}};worker.execute();}/** * @param args the command line arguments */publicstaticvoidmain(Stringargs[]){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newProgressTest().setVisible(true);}});}// Variables declaration - do not modifyprivatejavax.swing.JButtonjButton1;privatejavax.swing.JProgressBarjProgressBar1;// End of variables declaration}
Exemplo tosco mas que mostra bem a idéia da coisa =)
A parte essencial é o evento do botão ( linha 71 )…fiz isso rapidão no NetBeans…
Javabuntu
assim ele não vai aprender direito…
A
alissonoliveira
Ola amigo, obrigado pela ajuda
mas quando implemento esse codigo que você me mandou, da um erro na importação:
import javax.swing.SwingWorker; no SwingWorker
e conseguentemente onde tiver esse SwingWorker!
O que pode esta acontecendo??
Abraços
A
alissonoliveira
Valeu Carnevali!!
Consegui!!
Brigadão!!
A
alissonoliveira
Mais Uma Duvida!!!
Não Consegui chamar o proximo frame, depois que termina o jprogressbar!!!
Como eu faço??
Abraços
C
Carnevalli
importjavax.swing.JOptionPane;importjavax.swing.SwingWorker;/* * ProgressTest.java * * Created on 25 de Abril de 2008, 01:03 *//** * * @author Tadeu */publicclassProgressTestextendsjavax.swing.JFrame{/** Creates new form ProgressTest */publicProgressTest(){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. */// <editor-fold defaultstate="collapsed" desc="Generated Code">privatevoidinitComponents(){jProgressBar1=newjavax.swing.JProgressBar();jButton1=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jProgressBar1.setStringPainted(true);jButton1.setText("jButton1");jButton1.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton1ActionPerformed(evt);}});javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap(77,Short.MAX_VALUE).addComponent(jProgressBar1,javax.swing.GroupLayout.PREFERRED_SIZE,261,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(62,62,62)).addGroup(layout.createSequentialGroup().addGap(155,155,155).addComponent(jButton1).addContainerGap(172,Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(75,75,75).addComponent(jProgressBar1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(36,36,36).addComponent(jButton1).addContainerGap(149,Short.MAX_VALUE)));pack();}// </editor-fold>/*Tudo acontece aqui*/privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt){// TODO add your handling code here:SwingWorkerworker=newSwingWorker(){@OverrideprotectedObjectdoInBackground()throwsException{for(inti=0;i<=100;){jProgressBar1.setValue(i++);try{Thread.sleep(100);}catch(InterruptedExceptione){e.printStackTrace();}}returnnull;}@Overrideprotectedvoiddone(){JOptionPane.showMessageDialog(null,"Finalizado!","Fim",JOptionPane.INFORMATION_MESSAGE);}};worker.execute();}/** * @param args the command line arguments */publicstaticvoidmain(Stringargs[]){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newProgressTest().setVisible(true);}});}// Variables declaration - do not modifyprivatejavax.swing.JButtonjButton1;privatejavax.swing.JProgressBarjProgressBar1;// End of variables declaration}