Вы находитесь на странице: 1из 16

École Supérieure d'Infotronique d'Haïti

Advanced Java Programming


Module 2: Introduction to Swing Foundations
Moïse Edner BRUTUS M. Sc.
February 2017
Plan

1. Rappels sur les exercices d'échauffement


2. Introduction to Swing Programming
3. First Swing Program
4. Core Concepts
5. Second Swing Program
6. Questions & Responses

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 2


Introduction to Swing (suite)

Votre premier program en Java

Mais Coder en JAVA ressemble à cela

class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Prints the string to the console.
}
}

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 3


Introduction to Swing (suite)

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 4


Introduction to Swing (suite)

Votre Premier Programme avec Swing en Java

import javax.swing.*;

class hello {

public static void main(String args []){

JFrame frame = new JFrame("Hello Everybody, this is it");

frame.setVisible(true);

}
}

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 5


Core Concepts

Nous y voila enfin...


Qu'est-ce qu'un USER INTERFACE?
C'est un programme qui fait trois actions essentiellement:
● Recevoir un flux informationnel de l'utilisateur
● Traiter les flux informationnels reçus à l'entrée
● Produire une sortie

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 6


Core Concepts (suite)

USER INTERFACE

Text-Based UI Graphic-Based UI

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 7


Core Concepts (suite)

CONTAINER
Un “container” est un component qui sert qui réceptacle à
d'autres containers ou un component.

Container 1

Container 2

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 8


Core Concepts (suite)

ROOT PANE Layer Pane


JFrame

Title Bar

Menu Bar

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 9


Glass Pane
Introduction to Swing (suite)

Votre Deuxième Programme en Java


import javax.swing.*;

class SwingDemo {

SwingDemo() {

// Create a new JFrame container.


JFrame jfrm = new JFrame("A Simple Swing Program");

// Give the frame an initial size.


jfrm.setSize(275, 100);

// Terminate the program when the user closes the application.


jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 10


Introduction to Swing (suite)

// Create a text-based label.


JLabel jlab = new JLabel(" Swing powers the modern Java GUI.");

// Add the label to the content pane.


jfrm.getContentPane().add(jlab);

// Display the frame.


jfrm.setVisible(true);
}

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 11


Introduction to Swing (suite)

public static void main(String args[]) {

// Create the frame on the event dispatching


thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo();
}
});

}
}

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 12


Introduction to Swing (suite)

package trainingclass2;

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

public class RevisedSimplestSwing {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

JFrame frame = new JFrame("Revised Simplest Swing");

JButton closeButton = new JButton("close");


JButton helpButton = new JButton("help");

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 13


● Introduction to Swing (suite)

//frame.getContentPane().setLayout(new
FlowLayout(FlowLayout.RIGHT));

//frame.getContentPane().setComponentOrientation(ComponentOrientation.
RIGHT_TO_LEFT);
// frame.setBounds(50, 50, 200, 200);

frame.getContentPane().add(closeButton);
frame.getContentPane().add(helpButton);

frame.pack();

//frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 14


● Introduction to Swing (suite)

frame.setVisible(true);

System.out.println("Here there are "+


frame.getContentPane().getComponents().length+" Components ");

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 15


Questions & Réponses!!

February 14, 2017 Moïse Edner Brutus, Cryptography's Class 16

Вам также может понравиться