1 Swing par la pratique contrat Creative Commons Paternité-Pas d'Utilisation Commerciale-Partage des Conditions Initiales à l'Identique 2.0 France License
2 Le minimum import java.awt.*; import javax.swing.*; public class Test { public static void main(String args[]) { JFrame f = new JFrame(); f.setTitle("Fi2"); f.setPreferredSize(new Dimension(400,400)); f.pack(); f.setVisible(true); }
3 Une classe séparée import java.awt.*; import javax.swing.*; public class Test { public static void main(String args[]) { Simple f = new Simple(); f.setTitle("Fi2"); f.setPreferredSize(new Dimension(400,400)); f.pack(); f.setVisible(true); } public class Simple extends JFrame { }
4 Un arbre pour Simple public class Simple extends JFrame { JPanel jPanelHaut; JPanel jPanelBas; public Simple() { getContentPane().setLayout(new BorderLayout()); jPanelHaut = new JPanel(); jPanelHaut.setPreferredSize(new Dimension(400, 100)); jPanelHaut.setBorder(BorderFactory.createLineBorder(Color.GREEN, 3)); getContentPane().add(jPanelHaut, BorderLayout.NORTH); jPanelBas = new JPanel(); jPanelBas.setPreferredSize(new Dimension(400, 200)); jPanelBas.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); getContentPane().add(jPanelBas, BorderLayout.SOUTH); }
5 public class Simple extends JFrame { JLabel texteDuHaut, texteDuBas; JPanel jPanelHaut; JPanel jPanelBas; public Simple() { getContentPane().setLayout(new BorderLayout()); jPanelHaut = new JPanel(); jPanelHaut.setPreferredSize(new Dimension(400, 100)); jPanelHaut.setBorder(BorderFactory.createLineBorder(Color.GREEN, 3)); getContentPane().add(jPanelHaut, BorderLayout.NORTH); jPanelBas = new JPanel(); jPanelBas.setPreferredSize(new Dimension(400, 200)); jPanelBas.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); getContentPane().add(jPanelBas, BorderLayout.SOUTH); texteDuHaut = new JLabel("l'IHM en Fi1..."); jPanelHaut.add(texteDuHaut); texteDuBas = new JLabel("...et aprés en Fi2"); jPanelBas.add(texteDuBas);
6 public class Simple extends JFrame { public Simple() { getContentPane().setLayout(new BorderLayout()); /* * */ JPanel jPanelHaut = new JPanel(); jPanelHaut.setPreferredSize(new Dimension(400, 100)); jPanelHaut.setBorder(BorderFactory.createLineBorder(Color.GREEN, 3)); jPanelHaut.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); getContentPane().add(jPanelHaut, BorderLayout.NORTH); JLabel texteDuHaut = new JLabel("l'IHM en Fi1..."); jPanelHaut.add(texteDuHaut); JButton boutonDuHaut = new JButton("Bouton Fi1"); jPanelHaut.add(boutonDuHaut); /* * */ JPanel jPanelBas = new JPanel(); jPanelBas.setPreferredSize(new Dimension(400, 200)); jPanelBas.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); jPanelBas.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); getContentPane().add(jPanelBas, BorderLayout.SOUTH); JLabel texteDuBas = new JLabel("...et aprés en Fi2"); jPanelBas.add(texteDuBas); JButton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas);
7
8 public class Simple extends JFrame { public Simple() {... JButton boutonDuHaut = new JButton("Bouton Fi1"); jPanelHaut.add(boutonDuHaut); boutonDuHaut.addActionListener(new ReponseBouton(texteDuHaut));.... JButton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ReponseBouton(texteDuBas)); } public class ReponseBouton implements ActionListener { JLabel jbl; ReponseBouton(JLabel par) { jbl=par; } public void actionPerformed(ActionEvent ae){ if (jbl != null) jbl.setText("nouveau "+ae.getWhen()); }
9 public class Simple extends JFrame { JLabel texteDuBas,texteDuHaut; JButton boutonDuBas,boutonDuHaut; public Simple() { /.../ texteDuHaut = new JLabel("l'IHM en Fi1..."); jPanelHaut.add(texteDuHaut); boutonDuHaut = new JButton("Bouton Fi1"); jPanelHaut.add(boutonDuHaut); boutonDuHaut.addActionListener(new ReponseBouton()); /.../ texteDuBas = new JLabel("...et aprés en Fi2"); jPanelBas.add(texteDuBas); boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ReponseBouton()); } public class ReponseBouton implements ActionListener { public void actionPerformed(ActionEvent ae){ if (ae.getSource().equals(boutonDuBas)) texteDuBas.setText("nouveau bas : "+ae.getWhen()); else if (ae.getSource().equals(boutonDuHaut)) texteDuHaut.setText("nouveau haut : "+ae.getWhen()); }
10 /.../ JButton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { texteDuBas.setText("nouveau bas : "+e.getWhen()); } ); /.../
11 TP de rappel
12 CODE POUR COPIER/COLLER DANS UN FICHIER JAVA import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Simple extends JFrame { // on sort ces deux references du contructeur pour en faire des donnees membres de la classe // car la classe interne (qui y a acces !) ReponseBouton en a besoin JLabel texteDuHaut; JButton boutonDuHaut; public Simple() { getContentPane().setLayout(new BorderLayout()); JPanel jPanelHaut = new JPanel(); jPanelHaut.setPreferredSize(new Dimension(400, 100)); jPanelHaut.setBorder(BorderFactory.createLineBorder(Color.GREEN, 3)); jPanelHaut.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); getContentPane().add(jPanelHaut, BorderLayout.NORTH); texteDuHaut = new JLabel("l'IHM en Fi1..."); jPanelHaut.add(texteDuHaut); boutonDuHaut = new JButton("Bouton Fi1"); jPanelHaut.add(boutonDuHaut); boutonDuHaut.addActionListener(new ReponseBouton()); JPanel jPanelBas = new JPanel(); jPanelBas.setPreferredSize(new Dimension(400, 200)); jPanelBas.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); jPanelBas.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); getContentPane().add(jPanelBas, BorderLayout.SOUTH); Jlabel texteDuBas = new JLabel("...et aprés en Fi2"); jPanelBas.add(texteDuBas); Jbutton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ActionListener() { // écriture compacte de la classe interne ReponseBouton (sous forme de classe anonyme) mais pour le bouton Bas public void actionPerformed(ActionEvent e) { texteDuBas.setText("nouveau bas : "+e.getWhen()); } ); } // classe interne pour la gestion d'évènement sur le bouton Haut public class ReponseBouton implements ActionListener { public void actionPerformed(ActionEvent ae){ if (ae.getSource().equals(boutonDuHaut)) texteDuHaut.setText("nouveau haut : "+ae.getWhen()); }