background preloader

Java

Facebook Twitter

Cours Java et Eclipse de J.M. Doudoux. Développons en Java Préambule A propos de ce document Remerciements Notes de licence Marques déposées Historique des versions Partie 1 : Les bases du langage Java 1. Présentation de Javaintroduit le langage Java en présentant les différentes éditions et versions du JDK, les caractéristiques du langage et décrit l'installation du JDK1.1.

Les caractéristiques1.2. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Partie 2 : Les API de base 12. 13. 14. 15. 16. 17. 18. 19. Partie 3 : Les API avancées 20. 21. 22. 23. 24. 25. 26. 27. 28. Partie 4 : La programmation parallèle et concurrente 29. 30. 31. 32. 33. Partie 5 : Le développement des interfaces graphiques 34. 35. 36. 37. 38. 39. 40. Partie 6 : L'utilisation de documents XML et JSON 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. Partie 7 : L'accès aux bases de données 51. 52. 53. 54. 55. Partie 8 : La machine virtuelle Java (JVM) 56. 57. 58. 59. 60. Partie 9 : Le développement d'applications d'entreprises 61. 62. 63. 64. 65. 66. 67. 68. 69. Menus and toolbars in Java Swing. HomeContents A menu is a group of commands located in a menubar. A toolbar has buttons with some common commands in the application. To implement a menubar in Java Swing, we use three objects: a JMenuBar, a JMenu and a JMenuItem.

Simple menu We begin with a simple menubar example. Our example will show a menu with one item. JMenuBar menubar = new JMenuBar(); Here we create a menubar. ImageIcon icon = new ImageIcon("exit.png"); We will display an icon in the menu. JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); We create a menu object. eMenuItem.setToolTipText("Exit application"); This code line creates a tooltip for a menu item. Figure: Simple menu Submenu Each menu can also have a submenu. In this example, we create a submenu, a menu separator and an accelerator key. JMenu imp = new JMenu("Import"); ... file.add(imp); A submenu is just like any other normal menu. FileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK)); file.addSeparator(); Figure: Submenu. Programmation en Java (API) How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Many Swing components, such as labels, buttons, and tabbed panes, can be decorated with an icon — a fixed-sized picture. An icon is an object that adheres to the Icon interface. Swing provides a particularly useful implementation of the Icon interface: ImageIcon, which paints an icon from a GIF, JPEG, or PNG image. Here's a snapshot of an application with three labels, two decorated with an icon: The program uses one image icon to contain and paint the yellow splats. One statement creates the image icon and two more statements include the image icon on each of the two labels: ImageIcon icon = createImageIcon("images/middle.gif", "a pretty but meaningless splat"); label1 = new JLabel("Image and Text", icon, JLabel.CENTER); ... label3 = new JLabel(icon); The createImageIcon method (used in the preceding snippet) is one we use in many of our code samples.

ImageIcon icon = new ImageIcon("images/middle.gif", "a pretty but meaningless splat"); The rest of this section covers the following topics: Les codes JAVA / J2EE par date d'ajout page 1/66.