Ex création image d’Android
//creation form
Form f = new Form("Logo Android",BoxLayout.y());
//creation container
Container cnt1 = new Container(new FlowLayout(CENTER,CENTER));
Container cnt2 = new Container(new FlowLayout(CENTER,CENTER));
Container cnt3 = new Container(new FlowLayout(CENTER,CENTER));
//creation imageViewer (on ajoute les image dans le theme ensuite on les importes)
ImageViewer head = new ImageViewer(theme.getImage("head.png"));
ImageViewer body = new ImageViewer(theme.getImage("body.png"));
ImageViewer hand1 = new ImageViewer(theme.getImage("hand.png"));
ImageViewer hand2 = new ImageViewer(theme.getImage("hand.png"));
ImageViewer leg1 = new ImageViewer(theme.getImage("leg.png"));
ImageViewer leg2 = new ImageViewer(theme.getImage("leg.png"));
//ajouter image dans les containers
cnt1.add(head);
cnt2.add(hand1);
cnt2.add(body);
cnt2.add(hand2);
cnt3.add(leg1);
cnt3.add(leg2);
//ajouter le containers dans la forme final
f.add(cnt1);
f.add(cnt2);
f.add(cnt3);
f.show();
Formulaire :


//text field
TextField name = new TextField("","name",20,TextArea.ANY);
TextField prenom = new TextField("","prenom",20,TextArea.ANY);
TextField psw = new TextField("","password",20,TextArea.ANY);
psw.setConstraint(TextField.PASSWORD); //pour cacher le mot de passe
//calendrier
Picker dateOfBirth=new Picker();
//button on/of
Container cnt = new Container(BoxLayout.x());
Label lb= new Label("Genre : ");
OnOffSwitch genre = new OnOffSwitch();
genre.setOn("Homme");
genre.setOff("Femme");
cnt.add(lb);
cnt.add(genre);
//checkBox
CheckBox cb1 = new CheckBox("Foot");
CheckBox cb2= new CheckBox("Lecture");
CheckBox cb3 = new CheckBox("Tennis");
CheckBox cb4 = new CheckBox("Natation");
//Création d’un button
Button submit = new Button("Submit");
// ajout de tout les éléments
home.addAll(name,prenom,psw,dateOfBirth,cnt,cb1,cb2,cb3,cb4,submit);
home.show();
//creation la 2eme page
Form f = new Form("", BoxLayout.y());
Label lbnom=new Label("nom: ");
Label lbprenom=new Label("prenom: ");
Label lbpsw=new Label("mot de passe: ");
Label lbdate=new Label("birthday date: ");
Label lbgenre=new Label("gender: ");
Label lbhobbies=new Label("hobbies: ");
Publicité
f.add(lbnom);
f.add(lbprenom);
f.add(lbdate);
f.add(lbgenre);
f.add(lbhobbies);
Button back = new Button("Back");
f.add(back);
//action après bouton submit
FontImage.setMaterialIcon(submit, FontImage.MATERIAL\_DONE);
submit.addActionListener(e -> {
//get valeur du bouton on/off switch
if(genre.isValue()) sgenre="Femme";
else sgenre="Homme" ;
//initialisation de genre/hobbies
String sgenre="",hobbies="" ;
//get valeur du check box
if(cb1.isSelected()) hobbies=hobbies+" "+"Foot";
if (cb2.isSelected()) hobbies=hobbies+" "+"Lecture";
if(cb3.isSelected()) hobbies=hobbies+" "+"Tennis";
if (cb4.isSelected()) hobbies=hobbies+" "+"Natation";
//afficher tous les valeurs du formulaire
lbnom.setText("nom :" + name.getText());
lbprenom.setText("prenom :" + prenom.getText());
lbpsw.setText("passwrd: " + psw.getText());
lbdate.setText("anniverssaire: " + dateOfBirth.getText());
lbgenre.setText("gender: " + sgenre );
lbhobbies.setText("hobbies: "+ hobbies);
f.show();
});
//retourner à la 1ere page
back.addActionListener(e->{
home.show();
});
Authentification + toolbar




//Creation page d’authentification
Form f1 = new Form("Hi World", BoxLayout.y());
Container cnt = new Container(BoxLayout.y());
TextField tlogin=new TextField("","Login");
TextField tpsw=new TextField("","password");
tpsw.setConstraint(TextField.PASSWORD);
Button btn=new Button("Valider");
cnt.add(tlogin);
cnt.add(tpsw);
cnt.add(btn);
f1.add(cnt);
f1.show();
//Action apres authentification :
Form f2 = new Form("",new FlowLayout(CENTER, CENTER));
Label lbnom=new Label();
f2.add(lbnom);
btn.addActionListener(e-> {
if(tlogin.getText().equals("admin")
&& tpsw.getText().equals("admin"))
{
lbnom.setText("Welcome :"+tlogin.getText());
f2.show();
}
else{
Publicité
Dialog.show("error","login ou psw invalide","ok","cancel");
}
});
//afficher 3eme page “about” avec image et spanLabel
Form f3=new Form("About",BoxLayout.y());
ImageViewer img = new ImageViewer(theme.getImage("panda.png"));
SpanLabel sp = new SpanLabel("this is a panda playing with apple computer");
Container cnt3 = new Container(BoxLayout.y());
cnt3.add(img);
cnt3.add(sp);
f3.add(cnt3);
//Toolbar et action
f3.getToolbar().addMaterialCommandToLeftBar
("Back",FontImage.MATERIAL\_BACKUP, ev->f1.show());
//Toolbar pour retour page d’acceuil
f2.getToolbar().addMaterialCommandToSideMenu
("Home",FontImage.MATERIAL\_HOME,e->{f1.show();});
f2.getToolbar().addMaterialCommandToSideMenu
("about",FontImage.MATERIAL\_INFO,e->{f3.show();});
//Toolbar pour quitter l’application
f3.getToolbar().addMaterialCommandToOverflowMenu
("exit", FontImage.MATERIAL\_EXIT\_TO\_APP, e->Display.getInstance().exitApplication());
Ajout de plusieurs éléments

1/On crée une class Personne avec ces attributs/constructeur/getters & setters
2/ public void start() {
if(current != null){
current.show();
return;
}
f1 = new Form("hi World", BoxLayout.y());
//Instantiation des personnes
Personne p1 = new Personne("nom1","prenom1","[email protected]","image.jpg",22,25368483);
Personne p2 = new Personne("nom2","prenom2","[email protected]","image.jpg",20,55984775);
Personne p3 = new Personne("nom3","prenom3","[email protected]","image.jpg",25,98206441);
//Ajout des personnes dans une liste
List<Personne> list=new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
//Affichage de la liste en faisant appel à la fonction addItem
for(Personne p : list){
f1.add(addItem(p));
}
f1.show();
}
//création d’une méthode addItem pour l’ajout d’une seule personne et l’afficher dans l’appli et qui retourne un container avec toutes les informations dedans (container : nom + email , form2 :détails)
public Container addItem(Personne p)
{
Container cnt = new Container(BoxLayout.y());
Label nom=new Label(p.getNom());
Button btn=new Button(p.getEmail());
cnt.add(nom);
cnt.add(btn);
Container cnt2 = new Container(BoxLayout.x());
ImageViewer img = new ImageViewer(theme.getImage(p.getNomImg()));
cnt2.add(cnt);
cnt2.add(img);
btn.addActionListener(e->{
f2 = new Form("Details", BoxLayout.y());
Label lbimage=new Label(theme.getImage(p.getNomImg()));
Label lb1=new Label("nom: "+p.getNom());
Publicité
Label lb2=new Label("prenom: "+p.getPrenom());
Label lb3=new Label("Email: "+p.getEmail());
Label lb4=new Label("age: "+p.getAge());
Label lb5=new Label("Num Tel: "+p.getNumTel());
f2.addAll(lbimage,lb1,lb2,lb3,lb4,lb5);
f2.show();
f2.getToolbar().addMaterialCommandToLeftBar
("Back",FontImage.MATERIAL\_BACKUP, ev->f1.show());
});
cnt2.setLeadComponent(btn);
return cnt2;
}
Manipulation de bases de données

try {
//creation de la base de donnees
Database db = Database.openOrCreate("test");
//creation d’une table DB
db.execute("create table if not exists user (id INTEGER PRIMARY KEY AUTOINCREMENT"+ ", nom TEXT, prenom TEXT);");
//Ajout d’éléments dans la table
db.execute("insert into user(nom, prenom) values ('nom1','prenom1');");
//Affichage de tous les éléments de la table dans un container
Cursor c=db.executeQuery("select \* from user");
while (c.next()) {
//get la ligne de la table
Row r = c.getRow();
int id = r.getInteger(0);
String name = r.getString(1);
String prenom= r.getString(2);
Container cnt = new Container(BoxLayout.y());
Label nom=new Label("id: "+id);
Button btn=new Button("détails");
Label el=new Label("élément insérer");
cnt.add(el);
cnt.add(nom);
cnt.add(btn);
f1 = new Form("hi World", BoxLayout.y());
f1.add(cnt);
btn.addActionListener(e->{
f2 = new Form("Details", BoxLayout.y());
Label lb1=new Label("id: "+id);
Label lb2=new Label("nom: "+name);
Label lb3=new Label("prenom: "+prenom);
f2.addAll(lb1,lb2,lb3);
f2.show();
f2.getToolbar().addCommandToLeftBar("Back",null, ev->f1.show());
});
}
} catch (IOException ex){
Label lb1=new Label("exception: "+ex);
f2.add(lb1);
}
f1.show();
}
Charger image à partir du serveur Xampp

Si le serveur n’est pas connecté si le serveur connecté
//On se connecte Apache on xampp
//ajouter l’image icon.png sous C:\xampp\htdocs\codename
String url="<http://localhost/codename/nader.jpg>
//load.jpg on l’ajoute dans le theme
EncodedImage enc=EncodedImage.createFromImage(theme.getImage("load.jpg"), false);
Publicité
//si on charge plusieurs image il faut faire attention de changer le nom "MaPhoto3" a chaque fois parcequ’une fois le serveur télécharge l’image elle est stocké dedans
Image img=URLImage.createToStorage(enc, "MaPhoto3", url, URLImage.RESIZE\_SCALE);
Form hi = new Form("Hi World");
hi.add(img);
hi.show();
Charger un texte à partir d’un serveur

//ajouter le texte texte.txt sous C:\xampp\htdocs\codename
Form hi = new Form("", BoxLayout.y());
//établir une connextion avec le serveur
ConnectionRequest con=new ConnectionRequest() ;
con.setUrl ("http://localhost/codename/texte.txt");
//récupérer le texte dans le fichier .txt
con.addResponseListener((e)->{
String str=new String(con.getResponseData());
Label lb=new Label(str);
hi.add(lb);
});
NetworkManager.getInstance().addToQueueAndWait(con);
hi.show();
Charger un texte à partir d’un serveur


//chargement fichier php
Form hi = new Form("Hi World", BoxLayout.y());
String url = "http://localhost/codename/login.php";
ConnectionRequest con=new ConnectionRequest() ;
con.setUrl(url);
con.setPost(false);
//preparation du formulaire d’identification
TextField name = new TextField("","name",20,TextArea.ANY);
TextField pwd = new TextField("","mot de passe",20,TextArea.ANY);
pwd.setConstraint(TextField.PASSWORD);
hi.addAll(name,pwd);
Button btn = new Button("Connect");
hi.add(btn);
hi.show();
//si l’identification est valable in passe à une page d’acceuil
Form f1 = new Form();
Button btn2 = new Button("logout");
btn2.addActionListener((e)->{
hi.showBack();
});
//recuperation des informations dans le fichier php et vérification des données entrées
btn.addActionListener((e)-> {
con.addArgument("name", name.getText());
con.addArgument("password", pwd.getText());
NetworkManager.getInstance().addToQueueAndWait(con);
});
//si les informations sont OK on affiche la page d’acceuil
con.addResponseListener((e)->{
String str=new String(con.getResponseData());
if(str.equals("OK")){
f1.add(new Label ("Welcome "+name.getText()));
f1.add(btn2);
f1.show();
}else{
Dialog.show("Error", str, "Ok","cancel");
}
});