
Sources JavaConsultez toutes les sources
Nombre d'auteurs : 29, nombre de sources : 134, création le 13 avril 2013
Sommaire→Interfaces graphiques→Fenêtres et dialoguesLa classe TestPreferences étend JFrame. Grâce à la classe Preferences, du package java.util.prefs, elle est capable de mémoriser (pour chaque utilisateur) sa position, sa taille et son état (iconifiée, maximisée, etc.).
La classe TestLF étend JFrame. Elle a un JMenu l&f qui liste tous les L&F enregistrés et qui permet à l'utilisateur d'en changer par simple click.
La classe JPanelImageBg est une extension de JPanel. Elle redéfinit la méthode paintComponent afin de dessiner une Image. On peut choisir dans le constructeur si cette image va être centrée ou dessinée en mosaïque.
Exemple de SplashScreen construit à partir d'une JWindow.
Deux types d'utilisation possibles :
- de manière "absolue" : affichage pendant un temps donné puis traitement du reste du code
new SplashScreen("image.gif", 5000);
JOptionPane.showMessageDialog(null, "Hello world !!!",
"Titre", JOptionPane.INFORMATION_MESSAGE);
- de manière "relative" : on ne l'affiche que durant l'exécution d'une partie de code
SplashScreen splash = new SplashScreen("image.gif");
for(int i=0; i<12356; i++){
System.out.println(i);
}
splash.dispose();
Un splashscreen qui fonctionne également lorsque l'image à afficher se trouve dans un fichier jar.
// Upadates: 2004.04.02, 2004.01.09
import java.awt.*;
import javax.swing.*;
/**
* A splash screen to show while the main program is loading. A typical use
* is:
* <pre>
*
* public static void main(String[] args) {
* Splash s = new Splash(delay1);
* new MainProgram();
* s.dispose(delay2);
* }
*
* </pre>
* The first line creates a Splash that will appear until another frame
* hides it (MainProgram), but at least during "delay1" milliseconds.<br>
* To distroy the Splash you can either call "s.dispose()" or
* "s.dispose(delay2)", that will actually show the Splash for "delay2"
* milliseconds and only then hide it.<br>
* The picture to show must be in a file called "splash.png".
*/
public class Splash extends JWindow {
/**
* Creates a Splash that will appear until another frame hides it, but at
* least during "delay" milliseconds.
* @param delay the delay in milliseconds
*/
public Splash(int delay) {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new SplashPicture("splash.png"));
p.setBorder(BorderFactory.createLineBorder(Color.BLUE, 10));
getContentPane().add(p);
setSize(250, 250);
setLocationRelativeTo(null);
setVisible(true);
try {
Thread.sleep(delay);
}
catch (Exception e) {}
}
/**
* Shows the Splash during the specified time (in milliseconds) and then
* hides it.
* @param delay the delay in milliseconds
*/
public void dispose(int delay) {
dispose();
Splash s = new Splash(delay);
s.dispose();
}
/**
* This class loads and shows a picture, that can be either in the same
* jar file than the program or not. If the picture is smaller than the
* available space, it will be centered. If the picture is bigger than
* the available space, a zoom will be applied in order to fit exactly
* the space.
*/
class SplashPicture extends JPanel {
Image img;
public SplashPicture(String file) {
img = new ImageIcon(getClass().getResource(file)).getImage();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img == null) return;
int w = img.getWidth(this);
int h = img.getHeight(this);
boolean zoom = (w > getWidth() || h > getHeight());
if (zoom) g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
else g.drawImage(img, (getWidth()-w)/2, (getHeight()-h)/2, this);
}
}
}



