Insérer des JPanel dans une interface JFrame
salut,
Depuis mon application je veux insérer 3 JPanel dans mon interface.
J'ai 3 classes comme la suite:
la classe Jtree:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
public class Jtree extends JPanel {
//************liste des attributs
private JPanel arbre;
private DefaultMutableTreeNode racine;
private DefaultTreeModel model;
//*************Constructeur
public Jtree()
{
this.setSize(200, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setName("JTree");
//On invoque la méthode de construction de l'arbre
}
private void setDefaultCloseOperation(int exitOnClose) {
}
private void listRoot()
{
this.racine = new DefaultMutableTreeNode();
int count = 0;
for(File file : File.listRoots())
{
DefaultMutableTreeNode lecteur =new DefaultMutableTreeNode(file.getAbsolutePath());
try
{
for(File nom : file.listFiles()){
DefaultMutableTreeNode node =
new DefaultMutableTreeNode(nom.getName()+"\\");
lecteur.add(this.listFile(nom, node));
}
} catch (NullPointerException e) {}
this.racine.add(lecteur);
}
//*************Nous créons, avec notre hiérarchie, un arbre
arbre = new JPanel();
this.model = new DefaultTreeModel(this.racine);
arbre.setVisible(false);
arbre.setFocusable(true);
((TreeModel) arbre.getColorModel()).addTreeModelListener(new TreeModelListener() {
public void treeNodesChanged(TreeModelEvent evt) {
System.out.println("Changement dans l'arbre");
Object[] listNoeuds = evt.getChildren();
int[] listIndices = evt.getChildIndices();
for (int i = 0; i < listNoeuds.length; i++) {
System.out.println("Index " + listIndices[i] + " noeud déclencheur : "
+ listNoeuds[i]);
}
}
public void treeNodesInserted(TreeModelEvent event) {
System.out.println("Un noeud a été inséré !");
}
public void treeNodesRemoved(TreeModelEvent event) {
System.out.println("Un noeud a été retiré !");
}
public void treeStructureChanged(TreeModelEvent event) {
System.out.println("La structure d'un noeud a changé !");
}
});
this.getRootPane().add(new JScrollPane(arbre), BorderLayout.CENTER);
}
private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node){
int count = 0;
if(file.isFile())
return new DefaultMutableTreeNode(file.getName());
else{
File[] list = file.listFiles();
if(list == null)
return new DefaultMutableTreeNode(file.getName());
for(File nom : list){
count++;
//Pas plus de 5 enfants par noeud
if(count < 3){
DefaultMutableTreeNode subNode;
if(nom.isDirectory()){
subNode = new DefaultMutableTreeNode(nom.getName()+"\\");
node.add(this.listFile(nom, subNode));
}else{
subNode = new DefaultMutableTreeNode(nom.getName());
}
node.add(subNode);
}
}
return node;
}
}
} |
la classe ImageSelectionnée:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.io.PrintWriter;
import java.io.FileWriter;
public class ImageSelectionnée extends JPanel implements ActionListener {
//************les attributs
private JPanel container = new JPanel();
private Thread t;
private JMenuBar menuBar = new JMenuBar();
private JMenu fichier = new JMenu("Fichier"),edition = new JMenu("Edition");
private JMenuItem ouverture = new JMenuItem("ouvrire une image"),quitter = new JMenuItem("Quitter"),editionItem = new JMenuItem("saveAS");
private image pan;
String path;
// le constructeure
public ImageSelectionnée()
{
this.setName("L'image Selectionnée");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
//Display the window.
this.setVisible(true);
container.setBackground(Color.blue);
container.setLayout(new BorderLayout());
this.initMenu();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setContentPane(new image());
this.setVisible(true);
}
private void setDefaultCloseOperation(int exitOnClose) {
}
private void initMenu()
{
//Menu animation
fichier.add(ouverture);
fichier.addSeparator();
//Pour quitter l'application
quitter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
fichier.add(quitter);
//Menu forme
ouverture.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//try{
JFileChooser dialogue=new JFileChooser(new File("D:"));
//PrintWriter sortie;
File file;
if (dialogue.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
file=dialogue.getSelectedFile();
//sortie=new PrintWriter(new FileWriter(file.getPath(),true));
//sortie.close();
path=file.getPath();
System.out.println(path);
pan=new image(path);
}
//}catch(IOException e){};
}});
fichier.add(fichier);
this.setJMenuBar(menuBar);
//Menu À propos
edition.add(editionItem);
//Ajout des menus dans la barre de menus
menuBar.add(fichier);
menuBar.add(edition);
//Ajout de la barre de menus sur la fenêtre
this.setJMenuBar(menuBar);
}
private void setJMenuBar(JMenuBar menuBar2) {
menuBar2=menuBar;
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
public static void main(String[]args) { ImageSelectionnée Im = new ImageSelectionnée(); }
} |
avec le chemin de l'image:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.awt.Graphics;
import java.awt.Point;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class image {
String path;
Graphics gra;
Point p=new Point(1,1);
image (String path)
{
this.path=path;
this.paintComponent(gra);
}
public void paintComponent(Graphics g){
try {
Image img = ImageIO.read(new File(path));
g.drawImage(img,1,1,(ImageObserver) this);
System.out.println(path);
//Pour une image de fond
//g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
} catch (IOException e) {
e.printStackTrace();
}
}
} |
et enfin j'ai la classe Aperçu:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.event.*;
public class Aperçu extends JPanel {
//les attributs de la classe
private static final String EXIT_ON_CLOSE = null;
private String base = "D:/.";
private Répertoires répertoires = new Répertoires();
private ListePhotos photos = new ListePhotos();
private Photo photo = new Photo();
//Constructeur
public Aperçu()
{
super();
JPanel panneau1 = new JPanel();
panneau1.setLayout(new BorderLayout());
JPanel panneau2 = new JPanel();
panneau2.setLayout(new BorderLayout());
panneau1.add(répertoires, BorderLayout.NORTH);
panneau2.add(photos, BorderLayout.NORTH);
panneau1.add(panneau2);
add(panneau1, BorderLayout.WEST);
add(photo);
setSize(600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//la classe Répertoires
private void setDefaultCloseOperation(String exitOnClose) {
}
private class Répertoires extends JComboBox implements ActionListener
{
JPanel p1 = new JPanel();
//Constructeur de la classe Répertoires
public Répertoires()
{
change();
setMaximumRowCount(20);
addActionListener(this);
}
public void change()
{
p1.setName(base);
File[] fichiers = new File(base).listFiles();
removeAllItems();
for (File fichier : fichiers)
if (fichier.isDirectory()) addItem(fichier.getName());
if (getItemCount()==0) addItem("");
addItem("..");
}
@Override
public void actionPerformed(ActionEvent e)
{
String nom = (String)getSelectedItem();
if (!nom.equals("..")) {
String nouveau = base + '/' + nom;
base = nouveau;
}
else {
String[] rep = base.split("/");
if (rep.length>2) {
StringBuilder nouveau = new StringBuilder(rep[0]);
for (int i=1 ; i<rep.length-1; i++) nouveau.append( '/'+rep[i]);
base = nouveau.toString();
}
}
change();
photos.change();
}
}
//la classe ListePhotos
private class ListePhotos extends JComboBox implements ActionListener
{
private final int largeur = 159;
private File[] fichiers;
//Constructeur de la classe ListePhotos
public ListePhotos()
{
addActionListener(this);
change();
}
public void change()
{
JPanel p2 = new JPanel();
p2.setName(base);
fichiers = new File(base).listFiles(new Filtre());
removeAllItems();
for (File fichier : fichiers) addItem(retailler(base+'/'+fichier.getName()));
}
@Override
public void actionPerformed(ActionEvent e)
{
try {
photo.change(new ImageIcon(base+'/'+fichiers[getSelectedIndex()].getName()).getImage());
}
catch (Exception ex) { photo.change(null); }
}
private ImageIcon retailler(String nom)
{
BufferedImage imageRetaillée = null;
try
{
BufferedImage source = ImageIO.read(new File(nom));
imageRetaillée = new BufferedImage(largeur, largeur*3/4, source.getType());
double ratio = largeur / (double)source.getWidth();
AffineTransform retailler = AffineTransform.getScaleInstance(ratio, ratio);
int interpolation = AffineTransformOp.TYPE_BICUBIC;
AffineTransformOp retaillerImage = new AffineTransformOp(retailler, interpolation);
retaillerImage.filter(source, imageRetaillée);
}
catch (IOException ex)
{
System.out.println("Photo non récupérée");
}
return new ImageIcon(imageRetaillée);
}
//la classe Filtre
private class Filtre implements FilenameFilter
{
public boolean accept(File rep, String nom)
{
return nom.matches(".+jpg");
}
}
}
//la classe Photo
private class Photo extends JComponent
{
private Image photo = null;
public void change(Image photo)
{
this.photo = photo;
repaint();
}
@Override
protected void paintComponent(Graphics g)
{
if (photo != null) {
int largeur = getWidth();
int hauteur = (int)(largeur * photo.getHeight(null) / (double)photo.getWidth(null));
g.drawImage(photo, 0, 0, largeur, hauteur, null);
}
}
}
} |
Mon problème c'est que je ne peut pas insérer ces classes ou ces JPanel dans un interface JFrame pour obtenir un interface comme celui là:
http://docs.google.com/file/d/0B5p8j...lMaU5vc2c/edit
j'ai besoins de vos aides :cry:
merci d'avance.