Tutoriel : Construction d'une applet
Code source de GoodEveningApplet.html
:
<html> <head> <meta http="Content-Type" content="text/html; charset=windows-1252"> <title> Page HTML de l'applet Good Evening </title> </head> <body> firstapplet.GoodEveningApplet apparaîtra dans un navigateur supportant Java.
<applet codebase = "." code = "firstapplet.GoodEveningApplet.class" archive = "GoodEvening.jar" name = "TestApplet" width = 400 height = 300 hspace = 0 vspace = 0 align = top > Vous devez avoir un navigateur supportant Java et exécutant JDK 1.1.x ou une version supérieure pour voir cette applet. </applet> </body> </html>
Code source de GoodEveningApplet.java
:
package firstapplet; import java.awt.*; import java.awt.event.*; import java.applet.*; public class GoodEveningApplet extends Applet { boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); Panel lower = new Panel(); Panel upper = new Panel(); CardLayout cardLayout1 = new CardLayout(); Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); Panel panel5 = new Panel(); BorderLayout borderLayout2 = new BorderLayout(); BorderLayout borderLayout3 = new BorderLayout(); BorderLayout borderLayout4 = new BorderLayout(); BorderLayout borderLayout5 = new BorderLayout(); Choice choice1 = new Choice(); Label label1 = new Label(); Label label2 = new Label(); Label label3 = new Label(); Label label4 = new Label(); Label label5 = new Label(); Label label6 = new Label(); FlowLayout flowLayout1 = new FlowLayout(); Button button1 = new Button(); /**Obtenir la valeur d'un paramètre*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construire l'applet*/ public GoodEveningApplet() { } /**Initialiser l'applet*/ public void init() { choice1.addItem("Anglais"); choice1.addItem("Allemand"); choice1.addItem("Français"); choice1.addItem("Suédois"); choice1.addItem("Australien"); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Initialiser le composant*/ private void jbInit() throws Exception { this.setLayout(borderLayout1); upper.setBackground(Color.orange); lower.setBackground(Color.magenta); lower.setLayout(cardLayout1); panel1.setLayout(borderLayout2); panel2.setLayout(borderLayout3); panel3.setLayout(borderLayout4); panel4.setLayout(borderLayout5); panel5.setLayout(flowLayout1); panel1.setBackground(new Color(190, 173, 255)); panel2.setBackground(new Color(83, 182, 255)); panel3.setBackground(new Color(255, 149, 66)); panel4.setBackground(new Color(239, 107, 140)); panel5.setBackground(new Color(17, 198, 99)); label1.setFont(new java.awt.Font("Serif", 1, 20)); label1.setForeground(Color.blue); label1.setText("Choisissez une langue"); label2.setFont(new java.awt.Font("Dialog", 1, 24)); label2.setForeground(Color.black); label2.setText("Good Evening"); label3.setFont(new java.awt.Font("Dialog", 1, 24)); label3.setForeground(Color.black); label3.setText("Guten Abend"); label4.setFont(new java.awt.Font("Dialog", 1, 24)); label4.setForeground(Color.black); label4.setText("Bonsoir"); label5.setFont(new java.awt.Font("Dialog", 1, 24)); label5.setForeground(Color.black); label5.setText("God Kvall"); choice1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(ItemEvent e) { choice1_itemStateChanged(e); } }); label6.setFont(new java.awt.Font("Dialog", 1, 24)); label6.setForeground(Color.black); label6.setText("Gudday, Mate"); button1.setLabel("Appuyer"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); this.add(lower, BorderLayout.CENTER); lower.add(panel1, "panel1"); panel1.add(label2, BorderLayout.NORTH); lower.add(panel2, "panel2"); panel2.add(label3, BorderLayout.SOUTH); lower.add(panel3, "panel3"); panel3.add(label4, BorderLayout.EAST); lower.add(panel4, "panel4"); panel4.add(label5, BorderLayout.WEST); lower.add(panel5, "panel5"); panel5.add(button1, null); panel5.add(label6, null); this.add(upper, BorderLayout.NORTH); upper.add(label1, null); upper.add(choice1, null); } /**Lancer l'applet*/ public void start() { } /**Arrêter l'applet*/ public void stop() { } /**Détruire l'applet*/ public void destroy() { } /**Obtenir l'information Applet*/ public String getAppletInfo() { return "Applet Information"; } /**Obtenir l'information Paramètre*/ public String[][] getParameterInfo() { return null; } void choice1_itemStateChanged(ItemEvent e) { if ("Anglais".equals(choice1.getSelectedItem())){ cardLayout1.show(lower, "panel1"); } else if ("Allemand".equals(choice1.getSelectedItem())){ cardLayout1.show(lower, "panel2"); } else if ("Français".equals(choice1.getSelectedItem())){ cardLayout1.show(lower, "panel3"); } else if ("Suédois".equals(choice1.getSelectedItem())){ cardLayout1.show(lower, "panel4"); } else if ("Australien".equals(choice1.getSelectedItem())){ cardLayout1.show(lower, "panel5"); } } void button1_actionPerformed(ActionEvent e) { label6.setForeground(new Color(255,0,0)); } }