Outils pour utilisateurs

Outils du site


helloworld:gui:java

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
helloworld:gui:java [2015/08/05 16:56] – ↷ Page déplacée et renommée de helloworld:gui à helloworld:gui:java roothelloworld:gui:java [2019/01/21 07:14] (Version actuelle) – supprimée root
Ligne 1: Ligne 1:
-=====Java===== 
-====AWT==== 
-{{:wiki:helloworld:gui:awt.png|Vue de la fenêtre AWT}} 
-<file java Awt.java> 
-package org.llgc; 
  
-import java.awt.Button; 
-import java.awt.FlowLayout; 
-import java.awt.Frame; 
-import java.awt.Label; 
-import java.awt.event.ActionEvent; 
-import java.awt.event.ActionListener; 
-import java.awt.event.WindowAdapter; 
-import java.awt.event.WindowEvent; 
- 
-@SuppressWarnings ("serial") 
-public class Awt extends Frame 
-{ 
-  public static void main (final String[] args) 
-  { 
-    new Awt (); 
-  } 
- 
-  public Awt () 
-  { 
-    initialize (); 
-  } 
- 
-  private void initialize () 
-  { 
-    setTitle ("Bonjour"); 
- 
-    addWindowListener (new WindowAdapter () 
-    { 
-      @Override 
-      public void windowClosing (final WindowEvent e) 
-      { 
-        dispose (); 
-      } 
-    }); 
- 
-    setLayout (new FlowLayout ()); // Le add place les composants les uns à la 
-                                   // suite des autres plutôt que les uns sur 
-                                   // les autres. 
-    add (new Label ("Label1", Label.CENTER)); 
-    add (new Label ("Label2", Label.CENTER)); 
-    Button button = new Button ("Fermer"); 
-    button.addActionListener (new ActionListener () 
-    { 
-      @Override 
-      public void actionPerformed (ActionEvent e) 
-      { 
-        dispose (); 
-      } 
-    }); 
-    add (button); 
- 
-    // setSize (280, 100); 
-    pack (); 
- 
-    setVisible (true); 
-  } 
-} 
-</file> 
-====Swing - Fenêtre basique==== 
-{{:wiki:helloworld:gui:swing.png|Vue de la fenêtre standars Swing}} 
-<file java Swing.java> 
-package org.llgc; 
- 
-import java.awt.BorderLayout; 
-import java.awt.FlowLayout; 
-import java.awt.GridLayout; 
-import java.awt.event.ActionEvent; 
- 
-import javax.swing.AbstractAction; 
-import javax.swing.ButtonGroup; 
-import javax.swing.JButton; 
-import javax.swing.JComboBox; 
-import javax.swing.JFrame; 
-import javax.swing.JLabel; 
-import javax.swing.JPanel; 
-import javax.swing.JRadioButton; 
-import javax.swing.JTextField; 
-import javax.swing.SwingConstants; 
- 
-@SuppressWarnings ("serial") 
-public class Swing extends JFrame 
-{ 
-  public static void main (final String[] args) 
-  { 
-    new Swing (); 
-  } 
- 
-  public Swing () 
-  { 
-    initialize (); 
-  } 
- 
-  private void initialize () 
-  { 
-    setTitle ("Bonjour"); 
- 
-    // addWindowListener (new ActionClose()); 
-    setDefaultCloseOperation (DISPOSE_ON_CLOSE); 
- 
-    getContentPane ().setLayout (new BorderLayout ()); // Le add place les 
-    // composants les uns à la suite des autres plutôt que les uns sur les 
-    // autres. 
-    getContentPane ().add (new JLabel ("Titre", SwingConstants.CENTER), BorderLayout.NORTH); 
- 
-    JPanel pnlSouth = new JPanel (new GridLayout (0, 2, 12, 12)); 
-    pnlSouth.setDoubleBuffered (true); 
- 
-    JLabel lbl = new JLabel ("Nom"); 
-    lbl.setHorizontalAlignment (SwingConstants.CENTER); 
-    pnlSouth.add (lbl, BorderLayout.CENTER); 
-    JTextField bt = new JTextField (25); 
-    pnlSouth.add (bt, BorderLayout.CENTER); 
- 
-    lbl = new JLabel ("Pays"); 
-    lbl.setHorizontalAlignment (SwingConstants.CENTER); 
-    pnlSouth.add (lbl, BorderLayout.CENTER); 
-    JComboBox <String> lstPays = new JComboBox <String> (); 
-    lstPays.addItem ("France"); 
-    lstPays.addItem ("Espagne"); 
-    lstPays.addItem ("Angleterre"); 
-    lstPays.addItem ("Belgique"); 
-    pnlSouth.add (lstPays, BorderLayout.CENTER); 
- 
-    lbl = new JLabel ("Civilité"); 
-    lbl.setHorizontalAlignment (SwingConstants.CENTER); 
-    pnlSouth.add (lbl, BorderLayout.CENTER); 
-    JPanel pnlCiv = new JPanel (new BorderLayout ()); 
-    JRadioButton optMonsieur = new JRadioButton ("Monsieur"); 
-    JRadioButton optMadame = new JRadioButton ("Madame"); 
-    ButtonGroup grpCivil = new ButtonGroup (); 
-    grpCivil.add (optMadame); 
-    grpCivil.add (optMonsieur); 
- 
-    pnlCiv.add (optMadame, BorderLayout.EAST); 
-    pnlCiv.add (optMonsieur, BorderLayout.WEST); 
-    pnlSouth.add (pnlCiv, BorderLayout.CENTER); 
- 
-    JPanel pnl2 = new JPanel (); 
-    // pnl2.setSize (new Dimension (500, 500)); 
-    // pnlSouth.setSize (new Dimension (500, 500)); 
-    pnl2.add (pnlSouth, BorderLayout.CENTER); 
-    pnlSouth.setLayout (new GridLayout (0, 2, 20, 20)); 
-    getContentPane ().add (pnl2, BorderLayout.CENTER); 
- 
-    pnlSouth = new JPanel (new FlowLayout ()); 
-    JButton button1 = new JButton (new ActionClose ("Fermer")); 
-    JButton button2 = new JButton (new ActionClose ("Fermer bis")); 
-    pnlSouth.add (button1, BorderLayout.WEST); 
-    pnlSouth.add (button2, BorderLayout.EAST); 
-    getContentPane ().add (pnlSouth, BorderLayout.SOUTH); 
- 
-    // setSize (280, 100); 
-    pack (); 
-    setVisible (true); 
-  } 
- 
-  private class ActionClose extends AbstractAction 
-  { 
-    public ActionClose (String name) 
-    { 
-      super (name); 
-    } 
- 
-    @Override 
-    public void actionPerformed (ActionEvent arg0) 
-    { 
-      dispose (); 
-    } 
-  } 
-} 
-</file> 
-====Swing - Menu et barre d'outils==== 
-{{:wiki:helloworld:gui:swing2.png|Vue de la fenêtre Swing2 avec son menu et sa barre d'outils}} 
-<file java Swing2.java> 
-package org.llgc; 
- 
-import java.awt.BorderLayout; 
-import java.awt.Container; 
-import java.awt.event.ActionEvent; 
-import java.awt.event.ActionListener; 
-import java.io.BufferedReader; 
-import java.io.File; 
-import java.io.FileInputStream; 
-import java.io.IOException; 
-import java.io.InputStreamReader; 
-import java.nio.charset.Charset; 
- 
-import javax.swing.AbstractAction; 
-import javax.swing.Icon; 
-import javax.swing.ImageIcon; 
-import javax.swing.JButton; 
-import javax.swing.JFileChooser; 
-import javax.swing.JFrame; 
-import javax.swing.JMenu; 
-import javax.swing.JMenuBar; 
-import javax.swing.JMenuItem; 
-import javax.swing.JScrollPane; 
-import javax.swing.JTextArea; 
-import javax.swing.JToolBar; 
-import javax.swing.filechooser.FileNameExtensionFilter; 
-import javax.swing.text.JTextComponent; 
-import javax.swing.text.PlainDocument; 
- 
-public class Swing2 extends JFrame implements ActionListener 
-{ 
-  private static final long serialVersionUID = -7065885007988227830L; 
-  private JTextComponent editor; 
- 
-  private Swing2 () 
-  { 
-    initialize (); 
-  } 
- 
-  private void initialize () 
-  { 
-    Container c = getContentPane (); 
- 
-    c.setLayout (new BorderLayout ()); 
-    setTitle ("Editeur"); 
-    setDefaultCloseOperation (DISPOSE_ON_CLOSE); 
-    setJMenuBar (createMenu ()); 
-    c.add (createToolBar (), BorderLayout.NORTH); 
-    editor = createTextComponent (); 
-    c.add (new JScrollPane (editor), BorderLayout.CENTER); 
-  } 
- 
-  private JMenuBar createMenu () 
-  { 
-    JMenuBar barre_de_menus = new JMenuBar (); 
-    JMenu menu_personnel = new JMenu ("Fichier"); 
-    JMenuItem item1 = new JMenuItem (new ActionNew ("Nouveau", new ImageIcon ("img/newdoc.png"))); 
-    JMenuItem item2 = new JMenuItem (new ActionOpen ("Ouvrir", new ImageIcon ("img/open.png"))); 
-    JMenuItem item4 = new JMenuItem (new ActionClose ("Fermer")); 
- 
-    barre_de_menus.add (menu_personnel); 
-    menu_personnel.add (item1); 
-    menu_personnel.add (item2); 
-    menu_personnel.addSeparator (); 
-    menu_personnel.add (item4); 
-    return barre_de_menus; 
-  } 
- 
-  private JToolBar createToolBar () 
-  { 
-    JToolBar tool = new JToolBar (); 
-    tool.add (new JButton (new ActionNew (new ImageIcon ("img/newdoc.png")))); 
-    tool.add (new JButton (new ActionOpen (new ImageIcon ("img/open.png")))); 
-    tool.addSeparator (); 
-    return tool; 
-  } 
- 
-  private JTextComponent createTextComponent () 
-  { 
-    JTextArea txt = new JTextArea (); 
-    return txt; 
-  } 
- 
-  public static void main (String[] args) 
-  { 
-    Swing2 frm = new Swing2 (); 
-    frm.pack (); 
-    frm.setVisible (true); 
-  } 
- 
-  public JTextComponent getEditor () 
-  { 
-    return editor; 
-  } 
- 
-  public class ActionNew extends AbstractAction 
-  { 
-    private static final long serialVersionUID = -9121532899728292588L; 
- 
-    public ActionNew (String name, Icon icon) 
-    { 
-      super (name, icon); 
-    } 
- 
-    public ActionNew (Icon icon) 
-    { 
-      super ("", icon); 
-    } 
- 
-    public ActionNew (String name) 
-    { 
-      super (name); 
-    } 
- 
-    @Override 
-    public void actionPerformed (ActionEvent e) 
-    { 
-      getEditor ().setDocument (new PlainDocument ()); 
-    } 
-  } 
- 
-  private class ActionOpen extends AbstractAction 
-  { 
-    private static final long serialVersionUID = -3137082881796863964L; 
- 
-    public ActionOpen (Icon icon) 
-    { 
-      super ("", icon); 
-    } 
- 
-    public ActionOpen (String name, Icon icon) 
-    { 
-      super (name, icon); 
-    } 
- 
-    @Override 
-    public void actionPerformed (ActionEvent e) 
-    { 
-      JFileChooser chooser = new JFileChooser (); 
-      chooser.setFileFilter (new FileNameExtensionFilter ("Fichier txt", "txt")); 
-      if (chooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) 
-      { 
-        File fileName = chooser.getSelectedFile (); 
-        try (BufferedReader br = new BufferedReader ( 
-            new InputStreamReader (new FileInputStream (fileName), Charset.forName ("UTF-8")))) 
-        { 
-          char[] buffer = new char[(int) fileName.length ()]; 
-          if (br.read (buffer, 0, (int) fileName.length ()) != fileName.length ()) 
-          { 
-            throw new IOException ("Erreur lors de la lecture du fichier"); 
-          } 
-          /* 
-           * Document doc = new PlainDocument (); doc.insertString (0, new 
-           * String (buffer), null); getEditor ().setDocument (doc); 
-           */ 
-          getEditor ().setText (new String (buffer)); 
-        } 
-        catch (IOException e1) 
-        { 
-          e1.printStackTrace(); 
-          return; 
-        } 
-      } 
-    } 
- 
-  } 
- 
-  private class ActionClose extends AbstractAction 
-  { 
-    private static final long serialVersionUID = 1813412014719789012L; 
- 
-    public ActionClose (String name) 
-    { 
-      super (name); 
-    } 
- 
-    @Override 
-    public void actionPerformed (ActionEvent e) 
-    { 
-      dispose (); 
-    } 
- 
-  } 
- 
-  @Override 
-  public void actionPerformed (ActionEvent e) 
-  { 
-  } 
-} 
-</file> 
-====Swing : Tableau==== 
-{{:wiki:helloworld:gui:swing3.png|Vue de la fenêtre Swing3 avec composant graphique tableau}} 
- 
-<file java Swing3.java> 
-package org.llgc; 
- 
-import java.awt.BorderLayout; 
-import java.awt.event.MouseAdapter; 
- 
-import javax.swing.ImageIcon; 
-import javax.swing.JFrame; 
-import javax.swing.JScrollPane; 
-import javax.swing.JTable; 
-import javax.swing.WindowConstants; 
-import javax.swing.table.DefaultTableModel; 
- 
-public class Swing3 
-{ 
- 
-  private static final int imageHeight = 30; // hauteur de ligne 
-  private JTable table; 
- 
-  public static void main (String[] args) 
-  { 
-    new Swing3 ().initialize (); 
-  } 
- 
-  public void initialize () 
-  { 
-    JFrame frame = new JFrame (); 
-    frame.setTitle ("Exemple de JTable"); 
- 
-    ImageIcon icon1 = new ImageIcon ("img/edit.png"); 
-    ImageIcon icon2 = new ImageIcon ("img/delete.png"); 
-    ImageIcon icon3 = new ImageIcon ("img/call.png"); 
- 
-    Object[] entete = { "Edit", "Delete", "Call", "Id", "Lastame", "Firstname", "IPAddress" }; 
-    Object[][] donnees = { { icon1, icon2, icon3, "1", "Dupont", "Pierre", "192.168.1.122" }, 
-        { icon1, icon2, icon3, "2", "Durand", "Jacques", "192.168.1.123" } }; 
- 
-    DefaultTableModel tableModel = new DefaultTableModel (donnees, entete) 
-    { 
-      private static final long serialVersionUID = 756473841910394607L; 
- 
-      @SuppressWarnings ({ "unchecked", "rawtypes" }) 
-      @Override 
-      public Class getColumnClass (int column) 
-      { 
-        if (column < 3) 
-          return ImageIcon.class; 
-        return Object.class; 
-      } 
-    }; 
- 
-    table = new JTable (tableModel); 
-    table.setRowHeight (imageHeight); 
- 
-    table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); 
- 
-    table.getColumn ("Edit").setPreferredWidth (30); 
-    table.getColumn ("Delete").setPreferredWidth (30); 
-    table.getColumn ("Call").setPreferredWidth (30); 
-    table.getColumn ("Id").setPreferredWidth (30); 
-    table.getColumn ("Lastame").setPreferredWidth (100); 
-    table.getColumn ("Firstname").setPreferredWidth (100); 
-    table.getColumn ("IPAddress").setPreferredWidth (100); 
- 
-    table.addMouseListener (new MouseAdapter () 
-    { 
-      @Override 
-      public void mouseClicked (java.awt.event.MouseEvent evt) 
-      { 
-        int row = table.rowAtPoint (evt.getPoint ()); 
-        int col = table.columnAtPoint (evt.getPoint ()); 
-        if (row >= 0 && col >= 0) 
-        { 
-          switch (col) 
-          { 
-            case 0: 
-              System.out.println ("Edit " + table.getValueAt (row, 3)); 
-              break; 
-            case 1: 
-              System.out.println ("Delete " + table.getValueAt (row, 3)); 
-              break; 
-            case 2: 
-              System.out.println ("Call " + table.getValueAt (row, 3)); 
-              break; 
-            default: 
-              break; 
-          } 
-        } 
-      } 
-    }); 
- 
-    frame.add (new JScrollPane (table), BorderLayout.CENTER); 
-    frame.setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); 
-    frame.pack (); 
-    frame.setVisible (true); 
-  } 
-} 
-</file> 
helloworld/gui/java.1438786592.txt.gz · Dernière modification : de root