Showing posts with label swing. Show all posts
Showing posts with label swing. Show all posts

Tuesday, February 3, 2015

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class calculette implements ActionListener
{


JFrame f = new JFrame("Calculatrice" );

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton btn1=new JButton("+");
JButton btn2=new JButton("-");
JButton btn3=new JButton("*");
JButton btn4=new JButton("/");
JLabel res=new JLabel();




JLabel lbl1=new JLabel("Nombre1");
JTextField txt1=new JTextField(10);
JLabel lbl2=new JLabel("Nombre2");
JTextField txt2=new JTextField(10);
public calculette()
{
p1.add(lbl1);p1.add(txt1);
p1.add(lbl2);p1.add(txt2);
p2.add(btn1);p2.add(btn2);
p2.add(btn3);p2.add(btn4);
p3.add(res);
f.add(p1, BorderLayout.NORTH);f.add(p2,BorderLayout.CENTER);f.add(p3,BorderLayout.SOUTH);

btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
f.setBounds(400, 400, 400, 400);
f.setVisible(true);
}

public int add(int a,int b){
int res=a+b;
return res;
}


public int sous(int a,int b){
int res=a-b;
return res;
}


public int div(int a,int b){
int res=a/b;
return res;
}

public int mult(int a,int b){
int res=a*b;
return res;
}
public void actionPerformed(ActionEvent evt)
{
String op = evt.getActionCommand();
int a=Integer.parseInt(txt1.getText());
int b=Integer.parseInt(txt2.getText());

switch (op) {
case "+":
res.setText("resultat= "+(a+b));
break;
case "-":
res.setText("resultat= "+(a-b));
break;
case "*":
res.setText("resultat= "+(a*b));
break;
case "/":
res.setText("resultat= "+(a/b));
break;
default:
break;
}

}

public static void main(String[] args) {
new calculette();
}
}

Thursday, December 4, 2014

Exemple jfilechooser

Dans ce tutorial nous allons montrer un exemple qui montre comment choisir in fichier sur le disque de l'utilisateur en utlisant le composant JFileChooser.


/**
 *
 * @author Ram
 *
 */

public class DemoJFileChooser {
JFrame f = new JFrame("JFileChooser Demo");
JButton btn = new JButton("Parcourir...");
JTextField txtpath = new JTextField(20);
JFileChooser chooser = new JFileChooser();

public void graphique() {

JPanel panel = new JPanel();

panel.add(btn);
panel.add(txtpath);
f.add(panel);
f.setBounds(300, 300, 500, 150);
f.setVisible(true);



btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int returnVal = chooser.showOpenDialog(f);

if (returnVal == JFileChooser.APPROVE_OPTION) {

String path = chooser.getSelectedFile().getPath();
txtpath.setText(path);

} else {

JOptionPane.showMessageDialog(null,
"Vous n'avez rien sélectionné.","Attention",JOptionPane.ERROR_MESSAGE);

}

}

});

}


et voici la methode main pour l'execution de cet exemple:

public static void main(String[] args) {

DemoJFileChooser demo=new DemoJFileChooser();
demo.graphique();

}

}