Académique Documents
Professionnel Documents
Culture Documents
Sommaire
Rendu
Illustration d'une application graphique (v2)
L'application étend Frame
import java.awt.*;
setVisible(true);
}
FLowLayout GridLayout
BorderLayout GridBagLayout
North
South
Les composants
homme/machine Component
TextComponent
MenuBar
TextField
TextField
Label
TextArea
Choice
List
List Label
Button
Choice
Checkbox
Scrollbar
Checkbox
CheckboxGroup
Button
Application AWT
import java.awt ;
–Structure globale
class MonApplicationAWT extends Frame
{ private TextField tf;
private Choice ch;
private List ls;
private Checkbox cbSolaris, cbLinux, cbMacOs;
private CheckboxGroup rbGroup;
public MonApplicationAWT() {
// TextField Panel
// Choice Panel
// List Panel
// Checkbox Panel
// Radiobox Panel
// Buttons Panel
// Menu
// Layout
}
public static void main(String[] args) {
new MonApplicationAWT();
}
}
Les panneaux TextField et Choice
MonApplicationAWT() {
// TextField panel
Panel tfPanel = new Panel(
new FlowLayout(FlowLayout.LEFT));
tfPanel.setBackground(Color.lightGray);
tfPanel.add(new Label("TextField"));
tf = new TextField(15);
tfPanel.add(tf);
// Choice panel
Panel chPanel = new Panel(
new FlowLayout(FlowLayout.LEFT));
chPanel.setBackground(Color.lightGray);
chPanel.add(new Label("Choice"));
ch = new Choice();
ch.addItem("Solaris");
ch.addItem("Linux");
ch.addItem("MacOS");
chPanel.add(ch);
Les panneaux List et Checkbox
// List panel
Panel lsPanel = new Panel(
new FlowLayout(FlowLayout.LEFT));
lsPanel.setBackground(Color.lightGray);
ls = new List();
ls.add("Motif");
ls.add("AWT");
ls.add("Windows");
lsPanel.add(new Label("List"));
lsPanel.add(ls);
// Checkbox panel
Panel cbPanel = new Panel(
new FlowLayout(FlowLayout.LEFT));
cbPanel.setBackground(Color.lightGray);
cbPanel.add(cbSolaris = new Checkbox("Solaris"));
cbPanel.add(cbLinux = new Checkbox("Linux"));
cbPanel.add(cbMacOs = new Checkbox("MacOS"));
Boutons radio et boutons simples
// Radiobox panel
Panel rbPanel = new Panel(
new FlowLayout(FlowLayout.LEFT));
rbPanel.setBackground(Color.lightGray);
//CheckboxGroup (logique)
rbGroup = new CheckboxGroup();
rbPanel.add(new Checkbox("Solaris",rbGroup,true));
rbPanel.add(new Checkbox("Linux",rbGroup,false));
rbPanel.add(new Checkbox("MacOS",rbGroup,false));
// Buttons panel
Panel btPanel = new Panel(
new GridLayout(1,0,3,3));
Button ok = new Button("ok");
Button cancel = new Button("annuler");
Button quitter = new Button("quitter");
btPanel.add(ok);
btPanel.add(cancel);
btPanel.add(quitter);
La barre de menu
import java.awt.*;
import java.awt.event.*;
}}
class Ecouteur_Fenetre implements WindowListener
{
public void windowActivated(WindowEvent e) {} //obligatoire même si
vide
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
System.out.println(" Au revoir !!!");
System.exit(-1);
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
Adaptateurs
JComponent
Component
Container AbstractButton
Les composants Swing constituent une évolution naturelle
JComponent JToggleButton
des composants AWT. javax.Swing comporte les éléments
suivants : Panel
JCheckBox
Les conteneurs de plus haut niveau : JFrame, Japplet, Applet JRadioButton
…
JApplet JButton
Les composants légers (Light-Weight) : JButton,
Window JMenuButton
JCheckBox, etc.
SWING réorganise les classes selon JFC (Java JWindow JMenu
JCheckButtonMenuItem
Nombreux nouveaux composants JFrame
Dialog JColorChooser
Nombreuses facilités JComboBox
JDialog
Modèle MVC :séparation entre JInternalFrame
JList
aspect visuel (UI)
JMenuBar
contrôle
JOptionPane
Les composants Swing se placent dans un conteneur de plus JPanel
haut niveau “ContentPane” : C’est la fenêtre visible.
JPopupMenu
JProgressBar
...
SWING : Généralités
Les widgets
javax.swing.JButton
javax.swing.JRadioButton
javax.swing.JCheckBox
javax.swing.JLabel
javax.swing.JList
…
javax.swing.JTable
javax.swing.JSplitPane
javax.swing.JSlider
javax.swing.JProgressBar
SWING est compatible avec AWT. Il utilise le même modèle d'évènements et les
mêmes gestionnaires de Layouts de l'AWT.
JFrame
Une JFrame contient une fille unique,
JFrame
de la classe JRootPane
Cette fille contient deux filles, JRootPane
public MonFrame_V0(){
setSize(new Dimension(220,150));
setTitle("Mon tout premier JFrame");
getContentPane().setLayout(new
flowLayout());
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(b3);
setVisible(true);
}
public MonApplicationSwingPlaf() {
// JTextField
JPanel tfPanel = new JPanel(new
FlowLayout(FlowLayout.LEFT));
tfPanel.add(new JLabel("TextField"));
tf = new JTextField(15);
tfPanel.add(tf);
// JComboBox
JPanel chPanel = new JPanel(new
FlowLayout(FlowLayout.LEFT));
chPanel.add(new JLabel("Combobox"));
ch = new JComboBox();
ch.addItem("Solaris"); ch.addItem("Linux");
ch.addItem("MacOS");
chPanel.add(ch);
...
Exemple complet SWING + Plaf (2/5)
JPanel lsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
String[] donnees = { "Motif", "AWT", "Windows", "Swing" ,
"IlogViews", "XWindow", "Tcl/Tk", "gtk", "gnome"};
ls = new JList(donnees);
lsPanel.add(new JLabel("Liste"));
lsPanel.add(new JScrollPane(ls)); // ascenseur
JPanel cbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
cbPanel.setBorder(new TitledBorder("Boutons a cocher"));
cbPanel.add(cbSolaris = new JCheckBox("Solaris"));
cbPanel.add(cbLinux = new JCheckBox("Linux"));
cbPanel.add(cbMacOs = new JCheckBox("MacOS"));
JPanel rbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
rbPanel.setBorder(new TitledBorder("Boutons radio"));
rbGroup = new ButtonGroup();
JRadioButton rb;
rb = new JRadioButton("Solaris",true);
rb.setActionCommand("Solaris");
rbPanel.add(rb); rbGroup.add(rb);
rb = new JRadioButton("Linux",false);
rb.setActionCommand("Linux");
rbPanel.add(rb); rbGroup.add(rb);
rb = new JRadioButton("MacOS",false);
rb.setActionCommand("MacOS");
rbPanel.add(rb); rbGroup.add(rb);
...
Exemple complet SWING + Plaf (3/5)
//Buttons panel
JPanel btPanel = new JPanel( new GridLayout(1,0,3,3));
JButton ok = new JButton("ok");
JButton cancel = new JButton("annuler");
JButton quitter = new JButton("quitter");
btPanel.add(ok); btPanel.add(cancel); btPanel.add(quitter);
//Menus
//JMenu functMenu = new JMenu("fonctionnels");
JMenu procMenu = new JMenu("proceduraux");
procMenu.add(new JMenuItem("Pascal"));
procMenu.add(new JMenuItem("C"));
getRootPane().setJMenuBar(bMenu);
public Fenetre() {
super();
setTitle(" Titre de la Fenetre ");
setSize(300, 150);
setLayout(new FlowLayout());
add(new Button("Bouton 1"));
add(new Button("Bouton 2"));
add(new Button("Bouton 3"));
pack();
setVisible(true);
}
public Fenetre() {
super();
setTitle(" Titre de la Fenetre ");
setSize(300, 150);
setLayout(new BorderLayout());
add("North", new Button(" bouton haut "));
add("South", new Button(" bouton bas "));
add("West", new Button(" bouton gauche "));
add(new Button(" bouton droite "),
BorderLayout.EAST);
add("Center", new Button(" bouton milieu "));
pack();
setVisible(true);
}
public Fenetre() {
super();
setTitle(" Titre de la Fenetre ");
setSize(300, 150);
setLayout(new GridLayout(2, 3));
add(new Button("bouton 1"));
add(new Button("bouton 2"));
add(new Button("bouton 3"));
add(new Button("bouton 4"));
add(new Button("bouton 5 tres long"));
add(new Button("bouton 6"));
pack();
setVisible(true);
}
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button1"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new JButton("Button 2"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new JButton("Button 3"), gbc);
getContentPane().add(panel);
setVisible(true);
}
GridBagLayout
Les éléments peuvent être dans plusieurs cellules, +flexible