// Definition
public class Ex16 {
class Tarif {
private int i = 10;
public int value() {
return i;
}
}
class Destination {
private String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
// Usage
public Destination envoi(String s){
Tarif c = new Tarif();
Destination d=new Destination(s);
System.out.println(d.readLabel());
return d;
}
public static void main(String[] args){
Ex16 p = new Ex16();
p.envoi("Tanzanie");
}
}// Adapté du livre de Bruce Eckel
Ecouteur caché (Espion de la CIA !)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex17 extends JFrame implements ComponentListener {
JLabel label;
JButton button;
int clicCount = 0;
class MyButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
clicCount++;
label.setText(Integer.toString(clicCount));
}
}
public Ex17() {
this.setTitle("example event 2");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new GridLayout(1,2));
button = new JButton("clique?");
label = new JLabel();
label.setText("...");
button.addActionListener(new MyButtonActionListener());
cp.add(button);
cp.add(label);
this.addComponentListener(this);
}
public void componentHidden(ComponentEvent e) { }
public void componentMoved(ComponentEvent e) { }
public void componentResized(ComponentEvent e) { }
public void componentShown(ComponentEvent e) { }
public static void main(String[] args) {
JFrame frame = new Ex17();
frame.setSize(300,300);
frame.setVisible(true);
}
}
Double Ecouteur caché (CIA + KGB)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex18 extends JFrame {
JButton b1 = new JButton("Clique ici");
JButton b2 = new JButton("Clique la");
JTextField txt = new JTextField(10);
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = ((JButton) e.getSource()).getText();
txt.setText(name);
}
}
ButtonListener bl = new ButtonListener();
public Ex18() {
b1.addActionListener(bl);
b2.addActionListener(bl);
Container cp = this.getContentPane();
this.setTitle("example 3");
cp.add(new JLabel("Swing Demo 3"));
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
public static void main(String[] args) {
JFrame frame = new Ex18();
frame.setSize(200,200);
frame.setVisible(true);
}
}
Big brother sait où tu clique !
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex19 extends JFrame {
JTextField a, b;
JButton btn;
public Ex19() {
super("CursorFrame");
setSize(400, 200);
setLayout(new FlowLayout());
add(new JLabel("Click the mouse..."));
a = new JTextField("0", 4);
b = new JTextField("0", 4);
btn = new JButton("RESET");
add(a); add(b); add(btn);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
a.setText(String.valueOf(e.getX()));
b.setText(String.valueOf(e.getY()));
}
} );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
} );
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a.setText("0");
b.setText("0");
}
} );
}
public static void main(String[] args) {
JFrame app = new Ex19();
app.setVisible(true);
}
}
A vous de coder !
Faire une calculatrice avec
des boutons pour: les chiffres et les opérateurs (+, -, /, *, =)
zone texte pour le résultat
JPanel + paintComponent()
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex20 extends JPanel {
// like paint(Graphics g) but only interior
public void paintComponent(Graphics g){
super.paintComponent(g); // erases background
Graphics2D g2 = (Graphics2D)g; //cast for java2
// my graphics:
g2.setColor(new Color(255,0,0));
g2.fillRect(10,10,200,50); // left, top, width, height
g2.setColor(new Color(0,0,0));
g2.drawString("Hello World", 20, 20);// S, left, BOTTOM
}
public static void main(String[] args) {
JFrame app = new JFrame();
app.setContentPane(new Ex20());
app.setSize(250, 200);
app.setVisible(true);
}
}
Dessin direct
import javax.swing.*;
import java.awt.*;
public class Ex21 extends JFrame {
public JPanel panel;
public Ex21() {
Container cp = getContentPane();
this.setTitle("example 7");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
cp.add(panel);
}
public static void main(String[] args) throws java.lang.InterruptedException{
Ex21 frame = new Ex21();
frame.setSize(250,250);
frame.setVisible(true);
Thread.sleep(4000);
Graphics g = frame.panel.getGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
for (int i = 0; i < 100; ++i) {
g2.drawLine ((int) (250 * Math.random()), (int) (250 * Math.random()),
(int) (250 * Math.random()), (int) (250 * Math.random()));
}
}
}