1/ Ecrire un programme permettant de réaliser l'interface suivante: (une calculatrice)import java.awt.*;
public class Calcul
{ public static void main(String[] args)
{Frame f;
f = new Frame();
Button b1,b2,b3,b4,b5;
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
TextField T=new TextField();
b5 = new Button("5");
//b6 = new Button("6");
f.setLayout(new BorderLayout(2,3));
f.add(BorderLayout.NORTH,b1);
f.add(BorderLayout.SOUTH,b2);
f.add(BorderLayout.EAST,b3);
f.add(BorderLayout.CENTER,T);
f.add(BorderLayout.WEST,b5);
http://f.add(BorderLayoutb6);f.setSize (120,200);
f.show();
}
}
2/exemple fenetreimport java.awt.*;
import java.awt.event.*;
class Fenetre1 extends Frame implements ActionListener
{
private Button b1,b2;
public Fenetre1()
{
b1=new Button("ROUGE");
b2=new Button("JAUNE");
this.setLayout(new FlowLayout());
this.add(b1);
this.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
this.setSize(200,200);
this.show();}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)
{
this.setBackground(Color.red);
System.out.println(" vous avez clique sur le bouton ROUGE");
}
if (e.getSource()==b2)
{
this.setBackground(Color.yellow);
System.out.println(" vous avez clique sur le bouton JAUNE");
}
}
}
public class Fenetre1Test
{public static void main(String[] args)
{
Fenetre1 f;
f=new Fenetre1();
}
}
3/test fenetreimport java.awt.*;
import java.awt.event.*;
class Fenetre1 extends Frame implements ActionListener
{
private Button b1,b2;
public Fenetre1()
{
b1=new Button("ROUGE");
b2=new Button("JAUNE");
this.setLayout(new FlowLayout());
this.add(b1);
this.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
this.setSize(200,200);
this.show();}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)
{
this.setBackground(Color.red);
System.out.println(" vous avez clique sur le bouton ROUGE");
}
if (e.getSource()==b2)
{
this.setBackground(Color.yellow);
System.out.println(" vous avez clique sur le bouton JAUNE");
}
}
}
4/test frameimport java.awt.*;
public class TestFrame
{ public static void main(String[] args)
{Frame f;
f = new Frame();
Button b1,b2,b3,b4,b5,b6;
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.setSize (120,200);
f.show();
}
}
5/test frame 2import java.awt.*;
public class TestFrame
{ public static void main(String[] args)
{Frame f;
f = new Frame();
Button b1,b2,b3,b4,b5;
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
TextField T=new TextField();
b5 = new Button("5");
//b6 = new Button("6");
f.setLayout(new BorderLayout(2,3));
f.add(BorderLayout.NORTH,b1);
f.add(BorderLayout.SOUTH,b2);
f.add(BorderLayout.EAST,b3);
f.add(BorderLayout.CENTER,T);
f.add(BorderLayout.WEST,b5);
http://f.add(BorderLayoutb6);f.setSize (120,200);
f.show();
}
}
6/instruction sous dosE:\>cd c:
C:\
E:\>c:
C:\>cd j2*
C:\j2sdk>cd bin
C:\j2sdk\bin>javac new.java
C:\j2sdk\bin>javac TestFrame.java
C:\j2sdk\bin>java TestFrame