Treads gerar varias telas apos clicar no botão

1 resposta
L

galera como poderia fazer apos vc clicar botão irá gerar um monte de tela de uma só vez

segue o codigo

/**  
 *Esta classe Cria uma tela de interação com o usuário  
 * @author vcarretero  
 * @since 01/10/2010 
 */  
public class Tela extends javax.swing.JFrame {   
  
    MudarPosicao t1;   
  
    /** Creates new form Tela */  
    public Tela() {   
        initComponents();   
        setTitle("Mudar Tela de Posição");   
        setLocation(400,300);   
        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("unchecked")   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                             
    private void initComponents() {   
  
        btBotao = new javax.swing.JButton();   
  
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);   
  
        btBotao.setText("Mudar Tela de Posição");   
        btBotao.addActionListener(new java.awt.event.ActionListener() {   
            public void actionPerformed(java.awt.event.ActionEvent evt) {   
                btBotaoActionPerformed(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(121, 121, 121)   
                .addComponent(btBotao)   
                .addContainerGap(138, Short.MAX_VALUE))   
        );   
        layout.setVerticalGroup(   
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)   
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()   
                .addContainerGap(142, Short.MAX_VALUE)   
                .addComponent(btBotao)   
                .addGap(135, 135, 135))   
        );   
  
        pack();   
    }// </editor-fold>                           
  
    /*Quando clicado no botão, a tela muda de posição*/  
    private void btBotaoActionPerformed(java.awt.event.ActionEvent evt) {                                           
        t1 = new MudarPosicao();   
        t1.start();   
}                                          
  
    /**  
    * @param args the command line arguments  
    */  
    public static void main(String args[]) {   
        java.awt.EventQueue.invokeLater(new Runnable() {   
            public void run() {   
                new Tela().setVisible(true);   
            }   
        });   
    }   
  
    // Variables declaration - do not modify                        
    private javax.swing.JButton btBotao;   
    // End of variables declaration                      

	public void setLocation(int x, int y, int w) {
		
		
	}
  
}
import javax.swing.JOptionPane;   
  
/**  
 * Esta classe executa uma Thread, criando uma nova janela visual  
 * e localizando aleatóriamente, a mesma, na tela  
 * @author vcaretero  
 * @since 01/10/2010 
 */  
public class MudarPosicao extends Thread {   
  
    int x,y,w;   
  
    public MudarPosicao(){   
           
    }   
  
    public void run(){   
        try{   
            /*Criando um objeto do tipo tela*/  
            Tela tela = new Tela();   
  
            /*Gerando um numero aleatório para mudar aleatóriamente a tela de local*/  
            x = (int)(Math.random()*500) ;   
            y = (int)(Math.random()*500) ; 
            w =(int) (Math.random()*600); 
  
            /*Mudando a tela de posição a cada vez que o botão é acionado pelo usuario*/  
            tela.setLocation(x,y,w);   
               
        }catch(Exception e){   
            JOptionPane.showMessageDialog(null,"Ferrou");   
        }//fim da catch   
    }   
  
}
/**  
 * Esta é a classe principal onde cria o objeto Tela  
 * @author vcarretero  
 * @since 01/10/2010 
 */  
public class Main {   
  
    public static void main (String[] args){   
  
        Tela tela = new Tela();   
  
    }//fim da void main   
  
}//fim da

1 Resposta

nakai000

E ai bele

Fiz algumas alterações no seu código

Na classe Tela eu alterei o construtor para receber a posição da tela

/**  
 *Esta classe Cria uma tela de interação com o usuário  
 * @author vcarretero  
 * @since 01/10/2010 
 */  
public class Tela extends javax.swing.JFrame {   
  
    MudarPosicao t1;   
  
    /** Creates new form Tela */  
    public Tela(int x, int y) {   
        initComponents();   
        setTitle("Mudar Tela de Posição");   
        setLocation(x,y);   
        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("unchecked")   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                             
    private void initComponents() {   
  
        btBotao = new javax.swing.JButton();   
  
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);   
  
        btBotao.setText("Mudar Tela de Posição");   
        btBotao.addActionListener(new java.awt.event.ActionListener() {   
            public void actionPerformed(java.awt.event.ActionEvent evt) {   
                btBotaoActionPerformed(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(121, 121, 121)   
                .addComponent(btBotao)   
                .addContainerGap(138, Short.MAX_VALUE))   
        );   
        layout.setVerticalGroup(   
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)   
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()   
                .addContainerGap(142, Short.MAX_VALUE)   
                .addComponent(btBotao)   
                .addGap(135, 135, 135))   
        );   
  
        pack();   
    }// </editor-fold>                           
  
    /*Quando clicado no botão, a tela muda de posição*/  
    private void btBotaoActionPerformed(java.awt.event.ActionEvent evt) {                                           
        t1 = new MudarPosicao();   
        t1.start();   
}                                          
  
    /**  
    * @param args the command line arguments  
    */  
    public static void main(String args[]) {   
        java.awt.EventQueue.invokeLater(new Runnable() {   
            public void run() {   
                new Tela(300,400).setVisible(true);   
            }   
        });   
    }   
  
    // Variables declaration - do not modify                        
    private javax.swing.JButton btBotao;   
    // End of variables declaration                      

	public void setLocation(int x, int y, int w) {
		
		
	}
  
}

Na classe MudarPosicao eu coloquei um loop para criar as telas

import javax.swing.JOptionPane;   
  
/**  
 * Esta classe executa uma Thread, criando uma nova janela visual  
 * e localizando aleatóriamente, a mesma, na tela  
 * @author vcaretero  
 * @since 01/10/2010 
 */  
public class MudarPosicao extends Thread {   
  
    int x,y,w;   
  
    public MudarPosicao(){   
           
    }   
  
    public void run(){   
        try{                 
  
            /*Cria várias telas definido pelo valor de i*/
				for(int i = 0; i < 10; i++){
            x = (int)(Math.random()*500) ;   
            y = (int)(Math.random()*500) ; 
            w =(int) (Math.random()*600); 
				
				Tela tela = new Tela(x, y); 
				}
  
            /*Mudando a tela de posição a cada vez que o botão é acionado pelo usuario*/  
           // tela.setLocation(x,y,w);   
               
        }catch(Exception e){   
            JOptionPane.showMessageDialog(null,"Ferrou");   
        }//fim da catch   
    }   
  
}

Na classe Main só o local inicial da tela

/**  
 * Esta é a classe principal onde cria o objeto Tela  
 * @author vcarretero  
 * @since 01/10/2010 
 */  
public class Main {   
  
    public static void main (String[] args){   
  
        Tela tela = new Tela(300,400);   
  
    }//fim da void main   
  
}//fim da

Coloquei pra criar 10 se quiser mais é só mudar o valor de i :twisted:

Criado 22 de dezembro de 2010
Ultima resposta 22 de dez. de 2010
Respostas 1
Participantes 2