130 likes | 236 Views
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 {
E N D
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
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); } }
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 { }
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); } }
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);
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);
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()); } } }
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()); } } }
/.../ JButton boutonDuBas = new JButton("Bouton Fi2"); jPanelBas.add(boutonDuBas); boutonDuBas.addActionListener(new ActionListener() { publicvoid actionPerformed(ActionEvent e) { texteDuBas.setText("nouveau bas : "+e.getWhen()); } } ); /.../
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()); } } }