0% found this document useful (0 votes)
9 views7 pages

Swing Exercices

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Swing Exercices

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TD : Swing

Exercice :
Écrire, en Java, le code nécessaire pour réaliser ces interfaces graphiques (On utilisera la bibliothèque
swing)

1-

Illustration 1: Boutons de choix

2-

Illustration 2: Sélection d'une ville

3-
Illustration 3: Fenêtre de connexion

4-

Illustration 4: Choix d'une date


Correction du TD : Swing

1-
import java.awt.*;

import javax.swing.*;

public class btn extends JFrame {

JButton abort = new JButton("Annuler");

JButton retry = new JButton("Réessayer");

JButton fail = new JButton("Echec");

public btn() {

super("Buttons");

setSize(80, 140);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane = new JPanel();

pane.add(abort);

pane.add(retry);

pane.add(fail);

setContentPane(pane);

public static void main(String[] arguments) {

btn rb = new btn();

rb.show();

}
2-
import java.awt.*;

import javax.swing.*;

public class ville extends JFrame {

JRadioButton[] v = new JRadioButton[4];

public ville() {

super("Choisir une ville");

setSize(140, 190);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

v[0] = new JRadioButton("Moscou");

v[1] = new JRadioButton("Londres", true);

v[2] = new JRadioButton("Tokyo");

v[3] = new JRadioButton("Berlin");

JPanel pane = new JPanel();

ButtonGroup group = new ButtonGroup();

for (int i = 0; i < v.length; i++) {

group.add(v[i]);

pane.add(v[i]);

setContentPane(pane);

show();

public static void main(String[] arguments) {

ville ct = new ville();

}
}

3-
import java.awt.*;

import javax.swing.*;

class pass extends JFrame {

public pass() {

super("Password");

setSize(210, 130);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel pane = new JPanel();

JLabel usernameLabel = new JLabel("Username:


");

JTextField username = new JTextField (8);

JLabel passwordLabel = new JLabel ("Password:


");

JPasswordField password = new JPasswordField


(8);

pane.add (usernameLabel);

pane.add (username);

pane.add (passwordLabel);

pane.add (password);

setContentPane (pane);

public static void main(String[] arguments) {

pass ask = new pass();

ask.show();
}

4-
import java.awt.*;

import javax.swing.*;

public class Dat extends JFrame {

JComboBox monthBox = new JComboBox();

JComboBox yearBox = new JComboBox();

public Dat() {

super("Choisir une date");

setSize(220, 90);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane = new JPanel();

JLabel exp = new JLabel("Votre choix:");

pane.add(exp);

for (int i = 1; i < 13; i++)

monthBox.addItem("" + i);

for (int i = 2000; i < 2010; i++)

yearBox.addItem("" + i);

pane.add(monthBox);

pane.add(yearBox);

setContentPane(pane);

show();

public static void main(String[] arguments) {


Dat ct = new Dat();

You might also like