Exercice 1 : Calculatrice simple
Description : Créer une interface de calculatrice simple avec un champ de
texte pour afficher les entrées et les résultats, et des boutons pour les opérations
de base (+, -, *, /).
Code incomplet :
import j a v a x . swing . ∗ ;
import j a v a . awt . ∗ ;
public c l a s s S i m p l e C a l c u l a t o r extends JFrame {
private J T e x t F i e l d d i s p l a y F i e l d ;
private JPanel b ut t o nP a n el ;
public S i m p l e C a l c u l a t o r ( ) {
s e t T i t l e ( ” Simple C a l c u l a t o r ” ) ;
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 ) ;
s e t S i z e (300 , 400);
s e t L o c a t i o n R e l a t i v e T o ( null ) ;
// C r e r l e champ de t e x t e pour l ’ a f f i c h a g e
displayField = new J T e x t F i e l d ( ) ;
displayField . setEditable ( false ) ;
displayField . s e t H o r i z o n t a l A l i g n m e n t ( J T e x t F i e l d . RIGHT ) ;
displayField . s e t F o n t (new Font ( ” A r i a l ” , Font . PLAIN , 2 4 ) ) ;
// C r e r l e panneau d e s b o u t o n s
b ut t o nP a n e l = new JPanel (new GridLayout ( 4 , 4 , 1 0 , 1 0 ) ) ;
b ut t o nP a n e l . s e t B o r d e r ( B o r d e r F a c t o r y . createEmptyBorder ( 1 0 , 1 0 , 1 0 , 1 0 ) ) ;
// A j o u t e r l e s b o u t o n s de l a c a l c u l a t r i c e
// TODO : A j o u t e r l e s b o u t o n s de l a c a l c u l a t r i c e (0 −9 , +, −, ∗ , / , =)
// A j o u t e r l e champ de t e x t e e t l e panneau d e s b o u t o n s la f e n t r e
add ( d i s p l a y F i e l d , BorderLayout .NORTH) ;
add ( buttonPanel , BorderLayout .CENTER) ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
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 Runnable ( ) {
public void run ( ) {
new S i m p l e C a l c u l a t o r ( ) . s e t V i s i b l e ( true ) ;
}
});
}
}
1
Exercice 2 : Convertisseur de température
Description : Créer une interface de convertisseur de température avec deux
champs de texte, l’un pour saisir la température et l’autre pour afficher le
résultat, ainsi que des boutons radio pour sélectionner l’unité de température
(Celsius, Fahrenheit, Kelvin).
Code incomplet :
import j a v a x . swing . ∗ ;
import j a v a . awt . ∗ ;
public c l a s s TemperatureConverter extends JFrame {
private J T e x t F i e l d i n p u t F i e l d ;
private J T e x t F i e l d o u t p u t F i e l d ;
private ButtonGroup unitGroup ;
private JRadioButton c e l s i u s B u t t o n ;
private JRadioButton f a h r e n h e i t B u t t o n ;
private JRadioButton k e l v i n B u t t o n ;
public TemperatureConverter ( ) {
s e t T i t l e ( ” Temperature C o n v e r t e r ” ) ;
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 ) ;
s e t S i z e (400 , 300);
s e t L o c a t i o n R e l a t i v e T o ( null ) ;
// C r e r l e panneau p r i n c i p a l
JPanel mainPanel = new JPanel (new GridBagLayout ( ) ) ;
G r i d B a g C o n s t r a i n t s gbc = new G r i d B a g C o n s t r a i n t s ( ) ;
gbc . i n s e t s = new I n s e t s ( 1 0 , 1 0 , 1 0 , 1 0 ) ;
// C r e r l e champ de s a i s i e de l a t e m p r a t u r e
i n p u t F i e l d = new J T e x t F i e l d ( 1 0 ) ;
gbc . g r i d x = 0 ;
gbc . g r i d y = 0 ;
mainPanel . add ( i n p u t F i e l d , gbc ) ;
// C r e r l e champ d ’ a f f i c h a g e du r s u l t a t
o u t p u t F i e l d = new J T e x t F i e l d ( 1 0 ) ;
outputField . setEditable ( false ) ;
gbc . g r i d x = 1 ;
gbc . g r i d y = 0 ;
mainPanel . add ( o u t p u t F i e l d , gbc ) ;
// C r e r l e s b o u t o n s r a d i o pour s l e c t i o n n e r l ’ u n i t
// TODO : A j o u t e r l e s b o u t o n s r a d i o pour l e s u n i t s de t e m p r a t u r e
2
add ( mainPanel , BorderLayout .CENTER) ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
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 Runnable ( ) {
public void run ( ) {
new TemperatureConverter ( ) . s e t V i s i b l e ( true ) ;
}
});
}
}
Exercice 3 : Gestionnaire de tâches
Description : Créer une interface de gestionnaire de tâches avec une liste de
tâches, des boutons pour ajouter, modifier et supprimer des tâches, et un champ
de texte pour saisir le titre de la tâche.
Code incomplet :
import j a v a x . swing . ∗ ;
import j a v a . awt . ∗ ;
public c l a s s TaskManager extends JFrame {
private J L i s t <S t r i n g > t a s k L i s t ;
private D e f a u l t L i s t M o d e l <S t r i n g > taskModel ;
private J T e x t F i e l d t a s k F i e l d ;
public TaskManager ( ) {
s e t T i t l e ( ” Task Manager” ) ;
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 ) ;
s e t S i z e (500 , 400);
s e t L o c a t i o n R e l a t i v e T o ( null ) ;
// C r e r l e panneau p r i n c i p a l
JPanel mainPanel = new JPanel (new BorderLayout ( ) ) ;
mainPanel . s e t B o r d e r ( B o r d e r F a c t o r y . createEmptyBorder ( 1 0 , 1 0 , 1 0 , 1 0 ) ) ;
// C r e r l a l i s t e d e s t c h e s
taskModel = new D e f a u l t L i s t M o d e l < >();
t a s k L i s t = new J L i s t <>(taskModel ) ;
t a s k L i s t . s e t S e l e c t i o n M o d e ( L i s t S e l e c t i o n M o d e l . SINGLE SELECTION ) ;
J S c r o l l P a n e l i s t S c r o l l P a n e = new J S c r o l l P a n e ( t a s k L i s t ) ;
mainPanel . add ( l i s t S c r o l l P a n e , BorderLayout .CENTER) ;
// C r e r l e panneau d e s b o u t o n s
3
JPanel b ut t o nP a n el = new JPanel (new FlowLayout ( FlowLayout . RIGHT ) ) ;
// TODO : A j o u t e r l e s b o u t o n s ” A j o u t e r ” , ” M o d i f i e r ” e t ” Supprimer ”
// C r e r l e champ de s a i s i e du t i t r e de l a t c h e
t a s k F i e l d = new J T e x t F i e l d ( 2 0 ) ;
b ut t o nP a n e l . add ( t a s k F i e l d ) ;
mainPanel . add ( buttonPanel , BorderLayout .SOUTH) ;
add ( mainPanel , BorderLayout .CENTER) ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
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 Runnable ( ) {
public void run ( ) {
new TaskManager ( ) . s e t V i s i b l e ( true ) ;
}
});
}
}