Escolar Documentos
Profissional Documentos
Cultura Documentos
Tutorial Java Swing
Tutorial Java Swing
Java Swing
31 de Maro de 2008 c
Objetivos
Objetivos
Construir uma aplicao grca com elementos bsicos do ca a a Swing; Programar o tratamento de eventos.
Baseado no Abstract Window Toolkit (AWT). Introduzido no Java 1.0; Capaz de executar na maioria das arquiteturas operacionais que apresente a mquina virtual java e interface grca; a a
Baseado no Abstract Window Toolkit (AWT). Introduzido no Java 1.0; Capaz de executar na maioria das arquiteturas operacionais que apresente a mquina virtual java e interface grca; a a Elaborado pela Sun junto com a Netscape (IFC - Internet Foundation Classes);
Baseado no Abstract Window Toolkit (AWT). Introduzido no Java 1.0; Capaz de executar na maioria das arquiteturas operacionais que apresente a mquina virtual java e interface grca; a a Elaborado pela Sun junto com a Netscape (IFC - Internet Foundation Classes); Faz parte do Java Foundation Classes (JFC). O JFC, alm do e Swing, contm API de acessibilidade, API 2D, etc. e
7 8 9 10 11 12 13 14
15 16 17
public SimplesJanela ( int largura , int altura , String titulo ) { this . largura = largura ; this . altura = altura ; this . setSize ( largura , altura ) ; this . titulo = titulo ; t h i s . setName ( t i t u l o ) ; this . setTitle ( titulo ) ; // Tratamento do e v e n t o de f e c h a r j a n e l a t h i s . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE ) ; // J a n e l a pode s e r r e d i m e n s i o n a d a t h i s . s e t R e s i z a b l e ( true ) ; }
... pr iv at e Container area = t h i s . getContentPane () ; // Campos do F o r m u l A r i o p r i v a t e J T e x t F i e l d nome = new J T e x t F i e l d ( ) ; ... p r i v a t e J B u t t o n j b C a n c e l a r = new J B u t t o n ( " Cancelar " ) ; ...
private void carregaElementosFormulario () { // D e f i n i n d o t i p o de l a y o u t t h i s . a r e a . s e t L a y o u t ( new G r i d L a y o u t ( 4 , 2 ) ) ; this this ... this this } . a r e a . add ( new J L a b e l ( " Nome :" ) ) ; . a r e a . add ( nome ) ; . jbCancelar . addActionListener ( this ) ; . a r e a . add ( j b C a n c e l a r ) ;
public void actionPerformed ( ActionEvent event ) { i f ( event . getSource () . equals ( jbCadastrar ) ){ ... } ... }
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { j a v a x . s w i n g . S w i n g U t i l i t i e s . i n v o k e L a t e r ( new R u n n a b l e () { p u b l i c void run ( ) { S i m p l e s J a n e l a j a n e l a = new S i m p l e s J a n e l a ( 4 0 0 , 3 0 0 , " Janela de Teste " ) ; janela . carregaJanela () ; } }) ; }
3 4 5
6 7 8 9
Core Java 2 Volume 1 Fundamentos Cay S. Horstmann and Gary Cornell Prentice Hall PTR ISBN 0-13-047177-1 agosto, 2002 Site: http://java.sun.com/developer/onlineTraining/GUI/