1 / 12

Swing par la pratique

Swing par la pratique. Cedric.Dumas@emn.fr contrat Creative Commons Paternité-Pas d'Utilisation Commerciale-Partage des Conditions Initiales à l'Identique 2.0 France License. Le minimum. import java.awt.*; import javax.swing.*; public class Test {

elsa
Download Presentation

Swing par la pratique

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Swing par la pratique Cedric.Dumas@emn.frcontrat 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.*; publicclass Test { publicstaticvoid 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.*; publicclass Test { publicstaticvoid main(String args[]) { Simple f = new Simple(); f.setTitle("Fi2"); f.setPreferredSize(new Dimension(400,400)); f.pack(); f.setVisible(true); } } publicclass Simple extends JFrame { }

  4. Un arbre pour Simple publicclass 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. publicclass 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. publicclass 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. publicclass 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)); } publicclass ReponseBouton implements ActionListener { JLabel jbl; ReponseBouton(JLabel par) { jbl=par; } publicvoid actionPerformed(ActionEvent ae){ if (jbl != null) jbl.setText("nouveau "+ae.getWhen()); } } }

  8. publicclass 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()); } publicclass ReponseBouton implements ActionListener { publicvoid actionPerformed(ActionEvent ae){ if (ae.getSource().equals(boutonDuBas)) texteDuBas.setText("nouveau bas : "+ae.getWhen()); elseif (ae.getSource().equals(boutonDuHaut)) texteDuHaut.setText("nouveau haut : "+ae.getWhen()); } } }

  9. /.../ JButton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ActionListener() { publicvoid actionPerformed(ActionEvent e) { texteDuBas.setText("nouveau bas : "+e.getWhen()); } } ); /.../

  10. TP de rappel

  11. CODE POUR COPIER/COLLER DANS UN FICHIER JAVA import java.awt.*; import javax.swing.*; import java.awt.event.*; publicclass 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 publicvoid actionPerformed(ActionEvent e) { texteDuBas.setText("nouveau bas : "+e.getWhen()); } } ); } // classe interne pour la gestion d'évènement sur le bouton Haut publicclass ReponseBouton implements ActionListener { publicvoid actionPerformed(ActionEvent ae){ if (ae.getSource().equals(boutonDuHaut)) texteDuHaut.setText("nouveau haut : "+ae.getWhen()); } } }

More Related