Como faço para fechar um JFrame atrávez de uma Thread?
Tenho uma classe que é uma Thread e quero que quando ela terminar, ela feche uma janela.
Tentei isso:
InstalarFrame frame = new InstalarFrame();
frame.dispose();
Poxa… :shock: Achei que o meu problema era simples… :?
Ironlynx
gqferreira, tá vaga a sua figura.Tem certeza que depois dos 10 segundos sua Thread foi finalizada?E não consegue fechar como?O que tá dizendo no prompt?Congelou?
E vc pode postar o seu código aqui sem precisar usar a tag code(quando é muito grande, geralmente maior do que 500 linhas).Zipa ele(os .java) num arquivo zip e posta como anexo.(Acho que dificilmente ficará maior do que 500KB, certo?500KB zipados de .java é codigo á beça, quase um windows…huahauha)
gqferreira
Fiz uma gambiarra aqui que funcionou mas com certeza dever ter jeito melhor..^^
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JLabel;importjavax.swing.WindowConstants;publicclassPaiextendsjavax.swing.JFrame{{//Set Look & Feeltry{javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){e.printStackTrace();}}privateJLabellb;privateJButtonbt;/** * Auto-generated main method to display this JFrame */publicstaticvoidmain(String[]args){Paiinst=newPai();inst.setLocationRelativeTo(null);inst.setVisible(true);}publicPai(){try{setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);getContentPane().setLayout(null);{lb=newJLabel();getContentPane().add(lb);lb.setText("PAI");lb.setBounds(88,51,173,103);lb.setFont(newjava.awt.Font("Arial Black",1,72));}{bt=newJButton();getContentPane().add(bt);bt.setText("Chamar filho");bt.setBounds(88,183,165,28);bt.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){Filhof=newFilho();f.setVisible(true);MinhaThreadm=newMinhaThread();Threadt=newThread(m);t.start();m.filho=f;}});}this.setSize(361,269);}catch(Exceptione){e.printStackTrace();}}}
importjavax.swing.JLabel;publicclassFilhoextendsjavax.swing.JFrame{privateJLabellb;/** * Auto-generated main method to display this JFrame */publicFilho(){try{getContentPane().setLayout(null);{lb=newJLabel();getContentPane().add(lb);lb.setText("FILHO");lb.setBounds(44,48,329,139);lb.setFont(newjava.awt.Font("Arial Black",1,72));}setSize(400,300);}catch(Exceptione){e.printStackTrace();}}}
publicclassMinhaThreadimplementsRunnable{publicFilhofilho;@Overridepublicvoidrun(){try{Thread.sleep(10000);filho.dispose();// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<}catch(InterruptedExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}}
Tô muito errado? :lol: ... Pelo menos agora consegui fazer funcionar.. :?