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 :

Taille d'un slider dans un JPanel


Sujet :

Composants Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2008
    Messages
    757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2008
    Messages : 757
    Points : 572
    Points
    572
    Par défaut Taille d'un slider dans un JPanel
    Bonjour,

    Je suis en train de réaliser une interface graphique en utilisant le GridBagLayout.
    Jusqu'ici j'ai réalisé correctement la grille que je voulais (grâce à Joel.drigo qui m'a déjà aidé et a bien expliqué dans cette intéressante discussion : ici)

    Mon problème est que le JSlider que j'ai ajouté dans un panel, ne prend pas toute la place que le panel pourrait lui offrir .... et je me retrouve avec un slider trop petit comme vous pouvez voir dans la copie d'écran :
    Nom : Screenshot from 2015-12-19 00:56:48.png
Affichages : 387
Taille : 11,4 Ko

    Si quelqu'un peut m'aider ! Je vous remercie !

    et voici le code que j'ai fait :
    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    package com.symphonic.view;
     
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
     
    public class Oscillator_view_02 extends JFrame {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 7111394988989518864L;
     
    	/**
             * DECLARATIONS
             */
    		//DECLARATIONS ELEMENTS GRAPHIQUES
    	JPanel panelGeneral, panelSlider, panelSliderLabel, panelFrequencyField, panelFrequencyLabel;
    	JSlider slider;
    	JButton btn_start;
     
    		//CONSTANTES OSCILLATEUR
    	private final int OSC_FREQ_MIN = 100;
    	private final int OSC_FREQ_MAX = 4200;
    	private final int OSC_FREQ_VALUE = 440;
     
     
    	//CONSTRUCTEUR
    	public Oscillator_view_02(String title){
    		super();
    		//parapétrage de la fenêtre
    		buildOscWindow(title);
    		buildSlider(title, OSC_FREQ_MIN, OSC_FREQ_MAX, OSC_FREQ_VALUE);
     
    		panelGeneral = new JPanel(new GridBagLayout());
    		add(panelGeneral);
     
    		panelSlider = new JPanel();
    		panelSlider.add(slider);
    //1ere ligne horizontale
    		addPanel(null, panelGeneral, Color.BLUE, 0, 0, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 1, 0, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 2, 0, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 3, 0, 2, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 5, 0, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 6, 0, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 7, 0, 1, 1, 1, 1);
     
    //2ème ligne horizontale
    		addPanel(null, panelGeneral, Color.GREEN, 0, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 1, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 2, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 3, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 4, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 5, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 6, 1, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 7, 1, 1, 1, 1, 1);
     
    //3ème ligne horizontale
    		addPanel(null, panelGeneral, Color.RED, 0, 2, 7, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 7, 2, 1, 1, 1, 1);
     
    //4ème ligne horizontale
    		addPanel(panelSlider, panelGeneral, null, 0, 3, 7, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 7, 3, 1, 1, 1, 1);
     
    //5ème ligne horizontale
    		addPanel(null, panelGeneral, Color.RED, 0, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 1, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 2, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 3, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 4, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 5, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.RED, 6, 4, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.BLUE, 7, 4, 1, 1, 1, 1);
     
    //6ème ligne horizontale
    		addPanel(null, panelGeneral, Color.GREEN, 0, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 1, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 2, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 3, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 4, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 5, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.GREEN, 6, 5, 1, 1, 1, 1);
    		addPanel(null, panelGeneral, Color.YELLOW, 7, 5, 1, 1, 1, 1);
     
     
    	}
     
     
    	private static void addPanel(JPanel panelChild, JPanel panelParent, Color color,
    			int x, int y, int width, int height, int weightx, int weighty) {
     
    		GridBagConstraints gbc = new GridBagConstraints(x, y, width, height, weightx, weighty, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0);
    		JPanel panelColor;
    		if(panelChild == null) {
    			panelColor = new JPanel();
    		} else {
    			panelColor = panelChild;
    		}
    		if(color != null) panelColor.setBackground(color);
    		panelParent.add( panelColor, gbc );
    	}
     
    	private void buildSlider(String title, int freq_min, int freq_max, int freq_value) {
    		slider = new JSlider();
    		slider.setName(title);
    		slider.setMinimum(freq_min);
    		slider.setPaintLabels(true);
    		slider.setPaintTicks(true);
    		slider.setMajorTickSpacing(500);
    		slider.setMinorTickSpacing(100);
    		slider.setMaximum(freq_max);
    		slider.setValue(freq_value);
    	}
     
    	private void buildOscWindow(String title) {
    		setTitle(title);
    		setSize(1000, 600);
    		setLocationRelativeTo(null);	//On centre la fenêtre sur l'écran
    		setResizable(false);	//On interdit la redimensionnement de la fenêtre
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	//On dit à l'application de se fermer lors du clic sur la croix
    	}
    }
    OS : LinuxMint 20

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2008
    Messages
    757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2008
    Messages : 757
    Points : 572
    Points
    572
    Par défaut
    Hello !

    Bon, j'ai trouvé un bout de solution mais ce n'est pas au top :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    slider.setPreferredSize(new Dimension(800, 40));
    Par contre, je vois qu'il reste beaucoup d'espace en dessous du slider .... quelqu'un a-t'il une idée pour centrer mon slider et réduire la hauteur du panelSlider ?
    Nom : Screenshot from 2015-12-19 01:52:23.png
Affichages : 353
Taille : 6,2 Ko

    Merci,

    J'ai essayé ce code mais je n'obtiens pas quelque chose de propre, youjours beaucoup d'espace sous le slider :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    panelSlider = new JPanel();
    panelSlider.setPreferredSize(new Dimension(1000, 50));
    System.out.println(panelSlider.getWidth());
    slider.setPreferredSize(new Dimension(800, 40));
    panelSlider.add(slider);
    A suivre !
    OS : LinuxMint 20

Discussions similaires

  1. Récupérer la taille d'un JFrame dans un JPanel
    Par Raikyn dans le forum Agents de placement/Fenêtres
    Réponses: 8
    Dernier message: 31/05/2015, 23h02
  2. Pb de taille dans deux JPanel d'un même JFrame
    Par harmonips dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 20/02/2011, 12h21
  3. la taille d.un bouton dans un JPanel
    Par toasty666 dans le forum Débuter
    Réponses: 1
    Dernier message: 08/03/2010, 07h39
  4. Taille des composants dans un JPanel
    Par vodasan dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 17/11/2009, 13h08
  5. [langage] Problème de taille de fichier à mettre dans
    Par And_the_problem_is dans le forum Langage
    Réponses: 10
    Dernier message: 13/08/2002, 09h41

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