Exemples du cours
from http://vernier.frederic.free.fr/Teaching/IHM

main() qui ne termine pas

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

public class Ex9 {
  public static void main(String[] args) {
    JFrame jf = new JFrame("Et hop!");
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    System.out.println("c'est terminé ? ! ?");
    System.out.println("pourtant ça marche encore");
  }
}

Dialog/ Frame

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

public class Ex10 {
  public static void main(String[] args) {
    JFrame jf = new JFrame("Et hop!");
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDialog jd = new JDialog(jf,"Un dialogue",true);
    jd.setVisible(true);
}
}

ou est le label ?

import javax.swing.*;

public class Ex11 {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    frame.setTitle("example 1");

    frame.setDefaultCloseOperation
    (javax.swing.JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(new JLabel("Swing Demo 1"));
    frame.getContentPane().add(new JButton("clique ici"));

    frame.setSize(100,50);
    frame.setVisible(true);
  }
}

FlowLayout

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

public class Ex12 extends JFrame {
  public Ex12() {
    this.setTitle("example 12");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container contentPane = this.getContentPane();
    contentPane.add(new JLabel("Swing Demo 2"));
    contentPane.setLayout(new FlowLayout());
    contentPane.add(new JButton("clique ici"));
    contentPane.add(new JButton("clique là"));
  }

  public static void main(String[] args) {
    JFrame frame = new Ex12();
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}

Acne : plein de boutons !

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

public class Ex13 extends JFrame {
  public Ex13() {
    Container cp = getContentPane();
    this.setTitle("example 13");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    cp.setLayout(new FlowLayout());
    for(int i = 0; i < 20; i++)
      cp.add(new JButton("Button " + i));
  }

  public static void main(String[] args) {
    JFrame frame = new Ex13();
    frame.setSize(200,700);
    frame.setVisible(true);
  }
}

Bouton Quitter

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Ex14 extends JFrame implements ActionListener{

  public void actionPerformed(ActionEvent event) {
    System.exit(0);
  }

  public Ex14() {
    JButton quitButton = new JButton("Quit");
    quitButton.addActionListener(this);
    getContentPane().add(quitButton);
  }

  public static void main(String[] args) {
    JFrame frame = new Ex14();
    frame.setTitle("Quit button");
    frame.setSize(100, 100);
    frame.setVisible(true);
  }
}

Ecoute d'évènements Quitter

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

public class Ex15 extends JFrame implements ComponentListener {
  protected JTextArea display;

  public Ex15() {
    this.setTitle("example event 1");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    display = new JTextArea();
    display.setEditable(false);
    cp.add(display);
    this.addComponentListener(this);
  }

  protected void displayMessage(String message) {
    display.append(message + "\n");
    display.setCaretPosition(display.getDocument().getLength());
  }

  // ComponentListener methods
  public void componentHidden(ComponentEvent e) {
    displayMessage(e.getComponent().getClass().getName()+ " --- Hidden");
  }
  public void componentMoved(ComponentEvent e) {
    displayMessage(e.getComponent().getClass().getName()+ " --- Moved");
  }
  public void componentResized(ComponentEvent e) {
    displayMessage(e.getComponent().getClass().getName()+ " --- Resized");
  }
  public void componentShown(ComponentEvent e) {
    displayMessage(e.getComponent().getClass().getName()+ " --- Shown");
  }

  public static void main(String[] args) {
    JFrame frame = new Ex15();
    frame.setSize(250,700);
    frame.setVisible(true);
  }
}

Inner class

// 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 !


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()));
    }
  }
}