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

AWT/Swing Java Discussion :

Problème lors de l'affichage d'un rectangle fin.


Sujet :

AWT/Swing Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2016
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2016
    Messages : 9
    Points : 7
    Points
    7
    Par défaut Problème lors de l'affichage d'un rectangle fin.
    Bonjour, j'ai créé une fenêtre affichant 3 boutons (classe personnalisé étendu du JButton).
    Je dessine un rectangle fin à la droite de chacun d'entre eux via un "Graphics.fillRect()" :

    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
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
     
    public class FlatButton extends JButton
    {
        private Color hidleColor = new Color(0x4773B9);
        private Color hoverColor = new Color(0x416BAA);
        private Color pressColor = new Color(0x35549B);
     
        boolean isPressed = false;
     
        private ArrayList<FlatButton> otherButtons = new ArrayList<FlatButton>();
     
        public FlatButton(String text)
        {
            super(text);
            super.setBorderPainted(false);
            super.setFocusPainted(false);
            super.setContentAreaFilled(false);
        }
     
        @Override
        protected void paintComponent(Graphics g)
        {
            if (getModel().isPressed())
            {
                isPressed = true;
            }
            if (isPressed)
            {
                g.setColor(pressColor);
            }
            else if (getModel().isRollover())
            {
                g.setColor(hoverColor);
            }
            else
            {
                g.setColor(hidleColor);
            }
            g.fillRect(0, 0, getWidth(), getHeight());
     
            // ICI :
            g.setColor(new Color(0xFFFFFF));
            g.fillRect(0,0,1,getHeight());
            // AU DESSUS ^^
     
            super.paintComponent(g);
        }
     
        @Override
        public void setContentAreaFilled(boolean b) {
        }
     
        public Color getHoverBackgroundColor() {
            return hoverColor;
        }
     
        public void setHoverBackgroundColor(Color hoverBackgroundColor) {
            this.hoverColor = hoverBackgroundColor;
        }
     
        public Color getPressedBackgroundColor() {
            return pressColor;
        }
     
        public void setPressedBackgroundColor(Color pressedBackgroundColor) {
            this.pressColor = pressedBackgroundColor;
        }
    }
    Et je l'utilise ici :

    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
    FlatButton b1 = new FlatButton("Mon parc");
            FlatButton b2 = new FlatButton("Mes véhicules");
            FlatButton b3 = new FlatButton("Mes clients");
     
            b1.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            //b1.setBackground(new Color(0x4773B9));
            b1.setForeground(new Color(255,255,255));
            b1.setPreferredSize(new Dimension(175,50));
            b1.setHorizontalAlignment(SwingConstants.CENTER);
            b1.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b1);
     
            b2.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            //b2.setBackground(new Color(0x4773B9));
            b2.setForeground(new Color(255,255,255));
            b2.setPreferredSize(new Dimension(175,50));
            b2.setHorizontalAlignment(SwingConstants.CENTER);
            b2.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b2);
     
            b3.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            b3.setForeground(new Color(255,255,255));
            b3.setPreferredSize(new Dimension(175,50));
            b3.setHorizontalAlignment(SwingConstants.CENTER);
            b3.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b3);
    Le problème c'est que j'obtient des traits de taille différentes :
    Nom : Capture.PNG
Affichages : 102
Taille : 7,5 Ko


    Auriez-vous une idée d'où cela peut venir ? Merci d'avance

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Chez moi ça marche.

    Nom : Exemple.png
Affichages : 81
Taille : 4,7 Ko

    Le problème se situe peut être ailleurs, dans le code que tu ne montres 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
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Font;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.border.EmptyBorder;
     
    public class named extends JFrame
    {
     
    	private final JPanel contentPane;
     
    	/**
             * Launch the application.
             */
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				try
    				{
    					named frame = new named();
    					frame.setVisible(true);
    				} catch (Exception e)
    				{
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
             * Create the frame.
             */
    	public named()
    	{
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
     
    		contentPane.setBackground(Color.BLUE);
    		JPanel MenuLeft = new JPanel();
    		MenuLeft.setLayout(new FlowLayout());
    		MenuLeft.setBackground(Color.BLUE);
    		contentPane.add(MenuLeft);
     
    		FlatButton b1 = new FlatButton("Mon parc");
            FlatButton b2 = new FlatButton("Mes véhicules");
            FlatButton b3 = new FlatButton("Mes clients");
     
            b1.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            //b1.setBackground(new Color(0x4773B9));
            b1.setForeground(new Color(255,255,255));
            b1.setPreferredSize(new Dimension(175,50));
            b1.setHorizontalAlignment(SwingConstants.CENTER);
            b1.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b1);
     
            b2.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            //b2.setBackground(new Color(0x4773B9));
            b2.setForeground(new Color(255,255,255));
            b2.setPreferredSize(new Dimension(175,50));
            b2.setHorizontalAlignment(SwingConstants.CENTER);
            b2.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b2);
     
            b3.setFont(new Font("Montserrat",Font.ROMAN_BASELINE, 20));
            b3.setForeground(new Color(255,255,255));
            b3.setPreferredSize(new Dimension(175,50));
            b3.setHorizontalAlignment(SwingConstants.CENTER);
            b3.setVerticalAlignment(SwingConstants.CENTER);
            MenuLeft.add(b3);
     
     
     
    	}
     
    }
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Réponses: 3
    Dernier message: 04/02/2010, 12h00
  2. problème lors d'un affichage!
    Par skorpio dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 20/07/2007, 16h04
  3. problème lors de l'affichage des images
    Par cari dans le forum Langage
    Réponses: 24
    Dernier message: 10/11/2006, 11h34
  4. [CSS] Problème lors de l'affichage d'une infobulle
    Par nais_ dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 14/09/2006, 09h59

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