Vous êtes sur la page 1sur 4

TP2 : Atelier Java Compte Rendu

Applications Avec Interface


Graphique

I. Additionner deux entiers avec interface graphique


import java.awt.Frame;
import java.awt.*;
import java.awt.event.*;

public class Sum_1


{
private static TextField Input_1 = new TextField("0",6);
private static TextField Input_2 = new TextField("0",6);
private static Label Label_1 = new Label("First Integer");
private static Label Label_2 = new Label("Second Integer");

static int sum(int a, int b)


{
return(a+b);
}

public static void main(String [] args)


{
MyFrame window =new MyFrame() ;
window.setLayout(new GridLayout(3,2));
int x=800,y=450,w=400,h=300;
x-=0.5*w;y-=0.5*h;
window.setBounds(x,y,w,h);
window.setBackground(Color.orange);

Button Result_Button = new Button();


Result_Button.setLabel("Show Sum");
Result_Button.setBackground(Color.cyan);

Result_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
Result_Button.setLabel(String.valueOf("sum = " +
sum(Integer.parseInt(Input_1.getText()) , Integer.parseInt(Input_2.getText()))) );}}
);

par : Samet MohamedAmin GL2/2


Button Exit_Button = new Button();
Exit_Button.setLabel("quit");
Exit_Button.setBackground(Color.red);
Exit_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
System.exit(0);}}
);

window.add(Label_1);
window.add(Input_1);
window.add(Label_2);
window.add(Input_2);
window.add(Result_Button);
window.add(Exit_Button);
window.setVisible(true);
}
}

II. Additionner Plusieurs Nombres


import java.awt.Frame;
import java.awt.*;
import java.awt.event.*;

public class Sum_2


{
static TextField Input_1 = new TextField("0",12);
static Label Label_1 = new Label("Sequence of numbers");

static float sum(String s)


{
String[] s2;
s2=s.split(" ");
int i=0; float sum=0;

for(i=0;i<s2.length;i++)
{
try
{sum+=Float.parseFloat(s2[i]);}
catch(Exception e)
{
System.out.println(e.toString()+'\n'+e.getMessage());
sum=Float.NaN;
break;
}
}
return(sum);
} par : Samet MohamedAmin GL2/2
public static void main(String [] args)
{
MyFrame window =new MyFrame() ;
window.setLayout(new GridLayout(2,2));
int x=800,y=450,w=400,h=300;x-=0.5*w;y-=0.5*h;
window.setBounds(x,y,w,h);
window.setBackground(Color.orange);

Button Result_Button = new Button();


Result_Button.setLabel("Show Sum");
Result_Button.setBackground(Color.cyan);

Result_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
Result_Button.setLabel( String.valueOf("sum = " +
sum(Input_1.getText() ) ) );}}
);

Button Exit_Button = new Button();


Exit_Button.setLabel("quit");
Exit_Button.setBackground(Color.red);
Exit_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
System.exit(0);}}
);

window.add(Label_1);
window.add(Input_1);
window.add(Result_Button);
window.add(Exit_Button);
window.setVisible(true);
}
}
III. Conctener Deux Strings
import java.awt.Frame;
import java.awt.*;
import java.awt.event.*;

public class Concatenate


{
static TextField Input_1 = new TextField("exa",10);
static TextField Input_2 = new TextField("mple",10);
static Label Label_1 = new Label("First String");
static Label Label_2 = new Label("Second String");

static String concatenate(String s1, String s2)


{
return(s1+s2);
}
par : Samet MohamedAmin GL2/2
public static void main(String [] args)
{
MyFrame window =new MyFrame() ;
window.setLayout(new GridLayout(3,2));
int x=800,y=450,w=400,h=300;x-=0.5*w;y-=0.5*h;
window.setBounds(x,y,w,h);
window.setBackground(Color.orange);

Button Result_Button = new Button();


Result_Button.setLabel("result : example");
Result_Button.setBackground(Color.cyan);

Result_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
Result_Button.setLabel( String.valueOf("result : " +
concatenate(Input_1.getText() , Input_2.getText() ) ) );}}
);

Button Exit_Button = new Button();


Exit_Button.setLabel("quit");
Exit_Button.setBackground(Color.red);
Exit_Button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e) {
System.exit(0);}}
);

window.add(Label_1);
window.add(Input_1);
window.add(Label_2);
window.add(Input_2);
window.add(Result_Button);
window.add(Exit_Button);
window.setVisible(true);
}
}

par : Samet MohamedAmin GL2/2