IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Agents de placement/Fenêtres Java Discussion :

Layout dans JFrame


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 16
    Par défaut Layout dans JFrame
    Salut à tous ,

    J'essaye actuellement de placer de simple JPanel dans une JFrame j. En faite celle ci est composé en trois parties.

    - Un JPanel header : Qui se situe en haut d'un JFrame avec une hauteur de 80 px et sur toute la largeur de la jFrame.
    - Un JPanel menu : Qui se situe sous le header à gauche avec une largeur fixe de 100px et sur toute la hauteur de la JFrame-80 (du header).
    - un JPanel body : Qui se situe sous le header et prend le reste de la place disponible à droite du menu

    Le problème est que ça ne s'affiche pas correctement,l'affichage se fait au centre de la jframe, ne s'ajuste pas quand je redimensionne la fenêtre, bref tout va mal et je ne comprend pas le pourquoi du comment donc si une âme charitable pouvait m'éclairer

    Voici le code que j'ai réalisé :

    MainFrame:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class MainFrame extends JFrame implements ComponentListener{
     
    	private Header header;
    	private RoundedPanel body;
    	private MenuPanel left;
     
    	public MainFrame(){
     
    		setTitle("test");
    		this.setSize(800,600);
    		this.setPreferredSize(this.getSize());
     
    		this.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
     
    		header = new Header(getWidth());
    		c.fill = GridBagConstraints.BOTH;
    		c.gridwidth = 2;
    		c.gridheight= 1;
    		c.gridx = 0;
    		c.gridy = 0;
    		add(header, c);
     
     
    		left = new MenuPanel(getHeight());
    		c.fill = GridBagConstraints.BOTH;
    		c.gridwidth = 1;
    		c.gridheight= 1;
    		c.gridx = 0;
    		c.gridy = 1;
    		add(left,c);
     
    		body 	= new RoundedPanel(getWidth(),getHeight());
    		JLabel corps = new JLabel("corps");
    		body.add(corps);
    		c.fill = GridBagConstraints.BOTH;
    		c.gridwidth = 1;
    		c.gridheight= 1;
    		c.gridx = 1;
    		c.gridy = 1;
    		add(body,c);
     
    		pack();
    		setVisible(true);
    		setLocationRelativeTo(null);
    		addComponentListener(this);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	public static void main(String[] args){
    		MainFrame fen = new MainFrame();
    		fen.setVisible(true);
    	}
     
    	@Override
    	public void componentHidden(ComponentEvent arg0) {	
    	}
     
    	@Override
    	public void componentMoved(ComponentEvent arg0) {
     
     
    	}
     
    	@Override
    	public void componentResized(ComponentEvent arg0) {
    		header.setWidth(this.getWidth());
    		left.setHeight(this.getHeight());
    		body.setWidthHeight(this.getWidth(),this.getHeight());
    	}
     
    	@Override
    	public void componentShown(ComponentEvent arg0) {	
    	}
    }
    header :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.awt.Color;
    import javax.swing.JPanel;
     
    public class Header extends JPanel {
     
    	private int width;
     
     
    	public Header(int width){
    		super();
     
    		this.width = width;
    		this.setSize(width,80);
    		setPreferredSize(getSize());
     
    		setBackground(Color.GREEN);		
     
    	}
    	public void setWidth(int width) {
    		this.width = width;
    		setSize(width, 80);
    	}
    }
    Menu:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.awt.Color;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    public class MenuPanel extends JPanel {
     
    	private JButton boutton1,boutton2,boutton3;
    	private int height;
     
    	public MenuPanel(int height){
    		super();
     
    		this.height = height;
    		this.setSize(100,height);
    		this.setPreferredSize(getSize());
    		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
    		setBackground(Color.red);
     
    		boutton1 = new JButton("boutton1");
    		boutton2 = new JButton("boutton2");
    		boutton3 = new JButton("boutton3");
     
    		this.add(boutton1);
    		this.add(boutton2);
    		this.add(boutton3); 
    	}
     
    	public void setHeight(int height) {
    		this.height = height;
    		this.setSize(100,height);
    	}
     
    }
    Corps:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.util.prefs.BackingStoreException;
     
    import javax.swing.JPanel;
     
    public class RoundedPanel extends JPanel {
     
        /** Stroke size. it is recommended to set it to 1 for better view */
        protected int strokeSize = 1;
        /** Color of shadow */
        protected Color shadowColor = Color.black;
        /** Sets if it drops shadow */
        protected boolean shady = false;
        /** Sets if it has an High Quality view */
        protected boolean highQuality = true;
        /** Double values for Horizontal and Vertical radius of corner arcs */
        protected Dimension arcs = new Dimension(10, 10);
        /** Distance between shadow border and opaque panel border */
        protected int shadowGap = 5;
        /** The offset of shadow.  */
        protected int shadowOffset = 0;
        /** The transparency value of shadow. ( 0 - 255) */
        protected int shadowAlpha = 150;
     
        private int width,height;
     
        public RoundedPanel(int width, int height) {
            super();
            this.width = width;
            this.height = height;
            this.setSize(width-100,height-80);
    		this.setPreferredSize(getSize());
    		setBackground(Color.WHITE);
     
            setOpaque(false);
        }
        public void setWidthHeight(int width, int height) {
    		this.width = width;
    		this.height = height;
    		setSize(width, height);
    	}
    	@Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int width = getWidth();
            int height = getHeight();
            int shadowGap = this.shadowGap;
            Color shadowColorA = new Color(shadowColor.getRed(), 
    	shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
            Graphics2D graphics = (Graphics2D) g;
     
            //Sets antialiasing if HQ.
            if (highQuality) {
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    			RenderingHints.VALUE_ANTIALIAS_ON);
            }
     
            //Draws shadow borders if any.
            if (shady) {
                graphics.setColor(shadowColorA);
                graphics.fillRoundRect(
                        shadowOffset,// X position
                        shadowOffset,// Y position
                        width - strokeSize - shadowOffset, // width
                        height - strokeSize - shadowOffset, // height
                        arcs.width, arcs.height);// arc Dimension
            } else {
                shadowGap = 1;
            }
     
            //Draws the rounded opaque panel with borders.
            graphics.setColor(getBackground());
            graphics.fillRoundRect(0, 0, width - shadowGap, 
    		height - shadowGap, arcs.width, arcs.height);
            graphics.setColor(getForeground());
            graphics.setStroke(new BasicStroke(strokeSize));
            graphics.drawRoundRect(0, 0, width - shadowGap, 
    		height - shadowGap, arcs.width, arcs.height);
     
            //Sets strokes to default, is better.
            graphics.setStroke(new BasicStroke());
        }
    }
    Merci d'avance!

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Pas besoin d'un GridBag et de ComponentListener pour faire ce que tu cherches...

    Un vieux BorderLayout fait amplement l'affaire...
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package layout;
    import java.awt.BorderLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class MainFrame extends JFrame {
     
        private Header header;
        private RoundedPanel body;
        private MenuPanel left;
     
        public MainFrame(){
     
            setTitle("test");
            this.setSize(800,600);
            this.setPreferredSize(this.getSize());
     
            this.setLayout(new BorderLayout());
     
            header = new Header(getWidth());
     
            add(header, BorderLayout.NORTH);
     
     
            left = new MenuPanel(getHeight());
            add(left,BorderLayout.LINE_START);
     
            body     = new RoundedPanel(getWidth(),getHeight());
            JLabel corps = new JLabel("corps");
            body.add(corps);
            add(body);
     
            pack();
     
            setLocationRelativeTo(null);
            //addComponentListener(this);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
     
        public static void main(String[] args){
            MainFrame fen = new MainFrame();
            fen.setVisible(true);
        }
     
    }
    Ensuite, je te déconseille de prendre une approche basée sur une taille fixe en pixel. En effet, suivant la machine exécutant l'appli, tu pourrais avoir des soucis de taille. Par exemple des menus minuscules par rapport à la taille de l'écran t inversement, sans parler de tout ce qui HDPI, et des différences de tailles des chaînes suivant les OS et les configs ...

    De fait, il suffit juste de laisser les layouts calculer les taille optimales en fonction des composants présents dans l'application.

    Une approche intéressante est celle prise par Karsten Lentzsch dans son FormLayout où il définit une unité de mesure qui s'abstrait des résolutions et densité des écrans...

    cf: http://www.jgoodies.com/freeware/forms/index.html

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 16
    Par défaut
    Merci pour ta réponse Sinok:cool:
    Effectivement, parfois les solutions les plus simples sont les meilleurs et va savoir pourquoi je n'y avais pas pensé

Discussions similaires

  1. Comment inserer image dans JFrame ?
    Par kevin88 dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 17/03/2007, 00h09
  2. [C# 2.0] Layout dans un Panel
    Par jeff_76960 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/08/2006, 20h01
  3. Outils swing marche pas dans JFrame
    Par pat-trix dans le forum Agents de placement/Fenêtres
    Réponses: 7
    Dernier message: 27/06/2006, 19h32
  4. status bar dans JFrame
    Par hysah dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 01/06/2006, 19h29
  5. JPanel dans JFrame non visible au lancement.
    Par PRomu@ld dans le forum Agents de placement/Fenêtres
    Réponses: 16
    Dernier message: 17/03/2006, 08h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo