helloworld:gui:java
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| helloworld:gui:java [2019/01/21 06:29] – [AWT] : déplacement dans lang:java root | helloworld:gui:java [2019/01/21 07:14] (Version actuelle) – supprimée root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | =====Java===== | ||
| - | ====Swing - Fenêtre basique==== | ||
| - | {{: | ||
| - | <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 (" | ||
| - | public class Swing extends JFrame | ||
| - | { | ||
| - | public static void main (final String[] args) | ||
| - | { | ||
| - | new Swing (); | ||
| - | } | ||
| - | |||
| - | public Swing () | ||
| - | { | ||
| - | initialize (); | ||
| - | } | ||
| - | |||
| - | private void initialize () | ||
| - | { | ||
| - | setTitle (" | ||
| - | |||
| - | // 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 (" | ||
| - | |||
| - | JPanel pnlSouth = new JPanel (new GridLayout (0, 2, 12, 12)); | ||
| - | pnlSouth.setDoubleBuffered (true); | ||
| - | |||
| - | JLabel lbl = new JLabel (" | ||
| - | lbl.setHorizontalAlignment (SwingConstants.CENTER); | ||
| - | pnlSouth.add (lbl, BorderLayout.CENTER); | ||
| - | JTextField bt = new JTextField (25); | ||
| - | pnlSouth.add (bt, BorderLayout.CENTER); | ||
| - | |||
| - | lbl = new JLabel (" | ||
| - | lbl.setHorizontalAlignment (SwingConstants.CENTER); | ||
| - | pnlSouth.add (lbl, BorderLayout.CENTER); | ||
| - | JComboBox < | ||
| - | lstPays.addItem (" | ||
| - | lstPays.addItem (" | ||
| - | lstPays.addItem (" | ||
| - | lstPays.addItem (" | ||
| - | pnlSouth.add (lstPays, BorderLayout.CENTER); | ||
| - | |||
| - | lbl = new JLabel (" | ||
| - | lbl.setHorizontalAlignment (SwingConstants.CENTER); | ||
| - | pnlSouth.add (lbl, BorderLayout.CENTER); | ||
| - | JPanel pnlCiv = new JPanel (new BorderLayout ()); | ||
| - | JRadioButton optMonsieur = new JRadioButton (" | ||
| - | JRadioButton optMadame = new JRadioButton (" | ||
| - | ButtonGroup grpCivil = new ButtonGroup (); | ||
| - | grpCivil.add (optMadame); | ||
| - | grpCivil.add (optMonsieur); | ||
| - | |||
| - | pnlCiv.add (optMadame, BorderLayout.EAST); | ||
| - | pnlCiv.add (optMonsieur, | ||
| - | 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 (" | ||
| - | JButton button2 = new JButton (new ActionClose (" | ||
| - | 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 (); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | ====Swing - Menu et barre d' | ||
| - | {{: | ||
| - | <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 (" | ||
| - | 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 (" | ||
| - | JMenuItem item1 = new JMenuItem (new ActionNew (" | ||
| - | JMenuItem item2 = new JMenuItem (new ActionOpen (" | ||
| - | JMenuItem item4 = new JMenuItem (new ActionClose (" | ||
| - | |||
| - | 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 (" | ||
| - | tool.add (new JButton (new ActionOpen (new ImageIcon (" | ||
| - | 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 ("", | ||
| - | } | ||
| - | |||
| - | 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 ("", | ||
| - | } | ||
| - | |||
| - | public ActionOpen (String name, Icon icon) | ||
| - | { | ||
| - | super (name, icon); | ||
| - | } | ||
| - | |||
| - | @Override | ||
| - | public void actionPerformed (ActionEvent e) | ||
| - | { | ||
| - | JFileChooser chooser = new JFileChooser (); | ||
| - | chooser.setFileFilter (new FileNameExtensionFilter (" | ||
| - | if (chooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) | ||
| - | { | ||
| - | File fileName = chooser.getSelectedFile (); | ||
| - | try (BufferedReader br = new BufferedReader ( | ||
| - | new InputStreamReader (new FileInputStream (fileName), Charset.forName (" | ||
| - | { | ||
| - | char[] buffer = new char[(int) fileName.length ()]; | ||
| - | if (br.read (buffer, 0, (int) fileName.length ()) != fileName.length ()) | ||
| - | { | ||
| - | throw new IOException (" | ||
| - | } | ||
| - | /* | ||
| - | * 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) | ||
| - | { | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | ====Swing : 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 (" | ||
| - | |||
| - | ImageIcon icon1 = new ImageIcon (" | ||
| - | ImageIcon icon2 = new ImageIcon (" | ||
| - | ImageIcon icon3 = new ImageIcon (" | ||
| - | |||
| - | Object[] entete = { " | ||
| - | Object[][] donnees = { { icon1, icon2, icon3, " | ||
| - | { icon1, icon2, icon3, " | ||
| - | |||
| - | DefaultTableModel tableModel = new DefaultTableModel (donnees, entete) | ||
| - | { | ||
| - | private static final long serialVersionUID = 756473841910394607L; | ||
| - | |||
| - | @SuppressWarnings ({ " | ||
| - | @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 (" | ||
| - | table.getColumn (" | ||
| - | table.getColumn (" | ||
| - | table.getColumn (" | ||
| - | table.getColumn (" | ||
| - | table.getColumn (" | ||
| - | table.getColumn (" | ||
| - | |||
| - | 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 (" | ||
| - | 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); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
helloworld/gui/java.1548048568.txt.gz · Dernière modification : de root
