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

Composants Java Discussion :

[swing / awt] Probleme personalisation JPanel


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut [swing / awt] Probleme personalisation JPanel
    Bonjour a tous,
    j'ai un petit soucis concernant la personnalisation d'un JPanel en "GroupBox"

    Enfait, j'ai surcharge la methode "public void paintComponent(Graphics g)" jusqu'ici pas de soucis, cependant je voudrais redefinir la zone ou va se trouver le layout.
    En fait je voudrais que lorsqu'on fait un setLayout() ba le Layout ne se position pas sur tout le JPanel mais seulement sur le rectagle que j'ai defini ...
    J'espere etre clair...

    voici mon code actuel si ca peut aider :

    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
     
    package WindowComponents;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class GroupBox extends JPanel
    {
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	String _title;
    	int _margin;
    	int _header_size;
     
    	public GroupBox(String Title)
    	{
    		super();
    		this._title = Title;
    		this._margin = 2;
    		this._header_size = 25;
    	}
     
    	public int getHeaderSize()
    	{
    		return (this._header_size);
    	}
     
    	public void setHeaderSize(int HeaderSize)
    	{
    		this._header_size = HeaderSize;
    	}
     
    	public int getMargin()
    	{
    		return (this._margin);
    	}
     
    	public void setMargin(int Margin)
    	{
    		this._margin = Margin;
    	}
    	public String getTitle()
    	{
    		return (this._title);
    	}
    	public void setTitle(String Title)
    	{
    		this._title = Title;
    	}
     
    	/*public void setLayout(LayoutManager M)
    	{
    		//super.setLayout(M);
    		//???
    	}*/
     
    	public void paintComponent(Graphics g)
    	{
     
            Graphics2D g2d = (Graphics2D)g;
            g2d.setBackground(Color.WHITE);
            GradientPaint gp = new GradientPaint(0, 0, new Color(116, 178, 237), 0, this._header_size, new Color(73, 138, 228), true);
            g2d.setPaint(gp);
            g2d.fillRect(this._margin, this._margin, this.getWidth() - this._margin, this._header_size);
            Color C = new Color(218, 219, 223);
            g2d.setColor(C);
            //Stroke S = g2d.getStroke();
            g2d.setStroke(new BasicStroke(2));
            g2d.drawLine(this._margin, this._margin, this.getWidth() - this._margin, this._margin);
            g2d.drawLine(this._margin, this._margin + this._header_size, this.getWidth() - this._margin, this._margin + this._header_size);
            g2d.drawLine(this._margin, this._margin, this._margin, this.getHeight() - this._margin);
            g2d.drawLine(this.getWidth(), this._margin, this.getWidth(), this.getHeight() - this._margin);
            g2d.drawLine(2, this.getHeight() - this._margin, this.getWidth() - this._margin, this.getHeight() - this._margin);
            g2d.setColor(Color.BLACK);
            g2d.setFont(getFont().deriveFont(HEIGHT, 15).deriveFont(WIDTH, 15));
            g2d.drawString(this._title, (float)10.0, (float)(this._header_size / 2 + this._margin + 5));
    	}	
    }
    Merci d'avance
    Cordialement,
    NeoKript

  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
    Mouais, ou sinon tu change juste la border de ton composant en lui mettant une TitledBorder et le tour sera joué.

  3. #3
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut
    C'est ce que j'avais fais au d'ebut mais je n'arrive pas au meme resultat.

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 909
    Billets dans le blog
    54
    Par défaut
    Ca devrait pas être trop compliqué en utilisant la délégation mais y a pas mal de méthodes à surcharger.

    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
    package WindowComponents;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class GroupBox extends JPanel {
      private JPanel contentArea = new JPanel();
     
      public GroupBox() {
        super.setLayout(null);
        super.add(contentArea);
        layoutContentArea();
        addComponentListener(new ComponentAdapter()) {
           public void componentResized(ComponentEvent evt) {
              layoutContentArea();
           }
        }
      }
     
      protected void layoutContentArea()  {
        [...]
        contentArea.setBounds(cax, cay, caw, cah);
      }
     
      /**
      * {@inheritDoc}
      */
      @Override
      public void setLayout(LayoutManager mgr) { 
        contentArea.setLayout(mgr);
      }
     
      /**
      * {@inheritDoc}
      */
      @Override
      public Component add(Component comp) {
        return contentArea.add(comp);
      }
      ...
    Mais ca devrait rester plus simple au final que de réécrire un nouveau LayoutManager.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut
    Salut, ca ne fonctionne pas mieux,

    j'ai une exception dans setLayout "NullPointerException", le JPanel ContentPanel est null ... alors qu'il est instancier dans le constructeur...
    Je ne comprend pas.


    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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    package WindowComponents;
     
    import java.awt.*;
    import java.awt.event.ComponentEvent;
     
    import javax.swing.*;
     
    public class GroupBox extends JPanel
    {
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	private String Title;
    	private int Margin;
    	private int HeaderSize;
    	private JPanel ContentPanel;
     
    	public GroupBox(String Title)
    	{
    		this.Title = Title;
    		this.Margin = 2;
    		this.HeaderSize = 25;
    		this.ContentPanel = new JPanel();
    		super.setLayout(null);
    		super.add(ContentPanel);
    	}
     
    	public int getHeaderSize()
    	{
    		return (this.HeaderSize);
    	}
     
    	public void setHeaderSize(int HeaderSize)
    	{
    		this.HeaderSize = HeaderSize;
    		this.repaint();
    	}
     
    	public int getMargin()
    	{
    		return (this.Margin);
    	}
     
    	public void setMargin(int Margin)
    	{
    		this.Margin = Margin;
    		this.repaint();
    	}
    	public String getTitle()
    	{
    		return (this.Title);
    	}
    	public void setTitle(String Title)
    	{
    		this.Title = Title;
    	}
     
    	public void componentResized(ComponentEvent evt) 
    	{
            this.LayoutContentArea();
         }
     
     
    	protected void LayoutContentArea()  
    	{
    	    this.ContentPanel.setBounds(10, 10, 40, 40);
    	}
     
    	public void setLayout(LayoutManager M)
    	{
    		this.ContentPanel.setLayout(M);
    	}
    	@Override
    	public Component add(Component Comp) 
    	{
    	    return (this.ContentPanel.add(Comp));
    	  }
     
     
    	public void paintComponent(Graphics g)
    	{
     
            Graphics2D g2d = (Graphics2D)g;
            g2d.setBackground(Color.WHITE);
            GradientPaint gp = new GradientPaint(0, 0, new Color(116, 178, 237), 0, this.HeaderSize, new Color(73, 138, 228), true);
            g2d.setPaint(gp);
            g2d.fillRect(this.Margin, this.Margin, this.getWidth() - this.Margin, this.HeaderSize);
            Color C = new Color(218, 219, 223);
            g2d.setColor(C);
            //Stroke S = g2d.getStroke();
            g2d.setStroke(new BasicStroke(2));
            g2d.drawLine(this.Margin, this.Margin, this.getWidth() - this.Margin, this.Margin);
            g2d.drawLine(this.Margin, this.Margin + this.HeaderSize, this.getWidth() - this.Margin, this.Margin + this.HeaderSize);
            g2d.drawLine(this.Margin, this.Margin, this.Margin, this.getHeight() - this.Margin);
            g2d.drawLine(this.getWidth(), this.Margin, this.getWidth(), this.getHeight() - this.Margin);
            g2d.drawLine(2, this.getHeight() - this.Margin, this.getWidth() - this.Margin, this.getHeight() - this.Margin);
            g2d.setColor(Color.BLACK);
            g2d.setFont(getFont().deriveFont(HEIGHT, 15).deriveFont(WIDTH, 15));
            g2d.drawString(this.Title, (float)10.0, (float)(this.HeaderSize / 2 + this.Margin + 5));
            //this.ContentPanel.setBounds(this.Margin, this.HeaderSize + this.Margin, this.getWidth() - this.Margin, this.getHeight() - this.Margin);
    	}
     
     
    }
    Merci d'avance

  6. #6
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 909
    Billets dans le blog
    54
    Par défaut
    Trace de l'erreur STP.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problem entre GCJ et Swing/AWT (linux)
    Par dmichel dans le forum EDI et Outils pour Java
    Réponses: 1
    Dernier message: 25/03/2009, 15h10
  2. [SWING] Séparateur dans un JPanel
    Par ederf dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 21/07/2006, 15h45
  3. [Debutant]Probleme dimensionnement JPanel dans CardLayout
    Par etiennegaloup dans le forum Agents de placement/Fenêtres
    Réponses: 9
    Dernier message: 22/12/2005, 19h08
  4. [swing & AWT] double buffering hardware
    Par bidules dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 27/11/2005, 11h15
  5. [AWT] probleme de réaffichage
    Par thomasletiers dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 19/01/2005, 15h23

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