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 :

problème de GridBagLayout


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité1
    Invité(e)
    Par défaut problème de GridBagLayout
    Bonjour bonjour,
    Avant que j'explose l'écran de mon ordi je tente de trouver une solution auprès d'experts!
    Voici ce que j'ai :
    Et j'aimerai avoir : désolé c'est beau paint!
    Le problème c'est que mon bouton1 ne veux pas apparaître, rien ne touche jamais les bords, et impossible de trouver comment étirer ou rétrécir dynamiquement l'image...
    Voici le code correspondant :

    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
     
    public class Interface extends JFrame{
        private JLabel label;
        private String adressmap = System.getProperty("user.dir")
        + File.separator + "src" + File.separator + "app" + File.separator
        + "graphical_layer" + File.separator + "usa_map.gif";
     
        public Interface(){
            super();
     
            build();//Window initialization
        }
     
        private void build(){
            JMenuBar menuBar = new JMenuBar();
     
            JMenu menu1 = new JMenu("File");
            JMenuItem connection = new JMenuItem(new ConnectionAction("Connection"));
            menu1.add(connection);
            JMenuItem quitter = new JMenuItem(new QuitterAction("Quitter"));
            menu1.add(quitter);
            menuBar.add(menu1);
     
            JMenu menu2 = new JMenu("Options");       
            menuBar.add(menu2);
     
            JMenu menu3 = new JMenu("?");
            JMenuItem version = new JMenuItem(new VersionAction(this, "Version"));
            menu3.add(version);
            JMenuItem aPropos = new JMenuItem(new AProposAction(this, "A propos"));
            menu3.add(aPropos);
            menuBar.add(menu3);
     
            setJMenuBar(menuBar);
     
            setTitle("CriSafe v1.0");
            setSize(1024,768);
            setLocationRelativeTo(null);
            //setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setContentPane(buildContentPane());
        }
     
        private JPanel buildContentPane(){
            JPanel panel = new JPanel();
            panel.setLayout (new GridBagLayout ());
            add (panel);
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
     
            JButton test1 = new JButton("coucou1");
            c.gridx = 0;
            c.gridy = 0;
            c.ipady = 20;
            c.gridheight = 1;
            c.gridwidth = 2;
            panel.add(test1, c);
     
            ImagePanel map = new ImagePanel(new ImageIcon(adressmap).getImage());
            c.gridx = 0;
            c.gridy = 1;
            //c.weightx = 0.8;
            c.ipady = 600;
            c.gridwidth = 1;
            c.gridheight = 2;
            panel.add(map, c);
     
     
            JButton test2 = new JButton("coucou2");
            c.gridx = 1;
            c.gridy = 1;
            //c.weightx = 0.8;
            //c.ipadx = 100;
            c.ipady = 550;
            c.gridwidth = 1;
            c.gridheight = 1;
            panel.add(test2, c);
     
            Legende leg = new Legende();
            c.gridx = 0;
            c.gridy = 3;
            //c.weightx = 0.8;
            c.ipady = 168;
            c.gridwidth = 1;
            c.gridheight = 1;
            panel.add(leg, c);
     
            JTextArea test4 = new JTextArea("salut");
            c.gridx = 1;
            c.gridy = 2;
            //c.weightx = 0.8;
            c.ipady = 218;
            c.gridwidth = 1;
            c.gridheight = 2;
            panel.add(test4, c);
     
            return panel;
        }
     
        class ImagePanel extends JPanel {
     
              private Image img;
     
              public ImagePanel(String img) {
                this(new ImageIcon(img).getImage());
              }
     
              public ImagePanel(Image img) {
                this.img = img;
                //Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                //setPreferredSize(size);
                //setMinimumSize(size);
                //setMaximumSize(size);
                //setSize(size);
                setLayout(null);
              }
     
              public void paintComponent(Graphics g) {
                g.drawImage(img, 0, 0, null);
              }
        }
     
        public JLabel getLabel(){
            return label;
        }
    }

  2. #2
    Invité1
    Invité(e)
    Par défaut map et legend
    les classes map et legend sont des extensions de Jpanel.

  3. #3
    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
    Bon en remplaçant ton panel perso par un JButton et en retravaillant les constraints de ton GridBag çà se fait sans problème de mon coté (le ipady faut éviter en général ^^), j'ai retravaillé çà à grand coup de FILL et de weightX/weightY

    Le code retravaillé à la va vite

    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
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
     
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.io.File;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
     
    public class Interface extends JFrame{
        private JLabel label;
        private String adressmap = "Stats Laios.png";
     
        public Interface(){
            super();
     
            build();//Window initialization
        }
     
        private void build(){
            JMenuBar menuBar = new JMenuBar();
     
            JMenu menu1 = new JMenu("File");
     
     
            menuBar.add(menu1);
     
            JMenu menu2 = new JMenu("Options");       
            menuBar.add(menu2);
     
            JMenu menu3 = new JMenu("?");
     
            menuBar.add(menu3);
     
            setJMenuBar(menuBar);
     
            setTitle("CriSafe v1.0");
     
            setLocationRelativeTo(null);
            //setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setContentPane(buildContentPane());
            setSize(800,600);
        }
     
        private JPanel buildContentPane(){
            JPanel panel = new JPanel();
            panel.setLayout (new GridBagLayout ());
            add (panel);
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
     
            JButton test1 = new JButton("coucou1");
            c.gridx = 0;
            c.gridy = 0;
            c.fill=GridBagConstraints.HORIZONTAL;
            c.gridheight = 1;
            c.gridwidth = 2;
            panel.add(test1, c);
     
            ImagePanel map = new ImagePanel(new ImageIcon(adressmap).getImage());
            c.weightx = 1.0f;
            c.weighty = 1.0f;
            c.gridx = 0;
            c.gridy = 1;
            //c.weightx = 0.8;
            c.fill=GridBagConstraints.BOTH;
     
            c.gridwidth = 1;
            c.gridheight = 2;
            panel.add(map, c);
     
     
            JButton test2 = new JButton("coucou2");
            c.gridx = 1;
            c.gridy = 1;
            //c.weightx = 0.8;
            //c.ipadx = 100;
            c.fill = GridBagConstraints.VERTICAL;
            c.weightx = 0f;
            c.weighty = 1.0f;
            c.gridwidth = 1;
            c.gridheight = 1;
            panel.add(test2, c);
     
            JButton leg = new JButton("toto");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1.0f;
            c.weighty = 0f;
            c.gridx = 0;
            c.gridy = 3;
            //c.weightx = 0.8;
     
            c.gridwidth = 1;
            c.gridheight = 1;
            panel.add(leg, c);
     
            JTextArea test4 = new JTextArea("salut");
            c.gridx = 1;
            c.gridy = 2;
            //c.weightx = 0.8;
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 0f;
            c.weighty = 0.0f;
            c.gridwidth = 1;
            c.gridheight = 2;
            panel.add(test4, c);
     
            return panel;
        }
     
        class ImagePanel extends JPanel {
     
              private Image img;
     
              public ImagePanel(String img) {
                this(new ImageIcon(img).getImage());
              }
     
              public ImagePanel(Image img) {
                this.img = img;
                //Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                //setPreferredSize(size);
                //setMinimumSize(size);
                //setMaximumSize(size);
                //setSize(size);
                setLayout(null);
              }
     
              public void paintComponent(Graphics g) {
                g.drawImage(img, 0, 0, null);
              }
        }
     
        public JLabel getLabel(){
            return label;
        }
     
        public static void main(String[] args) {
            Interface  in = new Interface();
            in.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            in.setLocationRelativeTo(null);
            in.setVisible(true);
        }
    }
    Et le screenshot qui va bien
    Images attachées Images attachées  

  4. #4
    Invité1
    Invité(e)
    Par défaut Merci
    Je teste ça dès demain, merci encore.

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2008
    Messages : 64
    Par défaut
    J'ai envie de te dire d'utiliser NetBeans 6.0 qui est gratuit et dont l'éditeur graphique est très bien fait.

    Tu pourrais alors facilement poser tes JPanel en les resizant sans te preoccuper de gérer un GridBag parce que c'est galère à la main et/ou avec un autre IDE.
    Tu n'auras pas non plus à gérer les insets, l'alignement, etc, car tout est gérer par NetBeans lui-même.

    Cà fait un peu publicité ce que j'écris mais pour avoir essayer Eclipse, JBuilder, Notepad () et enfin Netbeans, y a pas photo

  6. #6
    Invité1
    Invité(e)
    Par défaut Ha ba c'est déjà moins moche!
    Merci!
    Reste le problème de l'image qui ne se re-dimensionne pas dynamiquement avec la fenêtre.
    Et j'aurai aimé (je sais je suis difficile) avoir mon JtextArea plus haut que la legende comme sur la photo.
    Pour NetBeans c'est sans hésitation que je l'utiliserai pour mon prochain projet!

    Je renvoie le nouveau code et voici la nouvelle photo.



    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
    131
     
    public class Interface extends JFrame{
    	private JLabel label;
    	private String adressmap = System.getProperty("user.dir")
        + File.separator + "src" + File.separator + "app" + File.separator
        + "graphical_layer" + File.separator + "usa_map.gif";
     
    	public Interface(){
    		super();
     
    		build();//Window initialization
    	}
     
    	private void build(){
    		JMenuBar menuBar = new JMenuBar();
     
    		JMenu menu1 = new JMenu("File");
    		JMenuItem connection = new JMenuItem(new ConnectionAction("Connection"));
    		menu1.add(connection);
    		JMenuItem quitter = new JMenuItem(new QuitterAction("Quitter"));
    		menu1.add(quitter);
    		menuBar.add(menu1);
     
    		JMenu menu2 = new JMenu("Options");		
    		menuBar.add(menu2);
     
    		JMenu menu3 = new JMenu("?");
    		JMenuItem version = new JMenuItem(new VersionAction(this, "Version"));
    		menu3.add(version);
    		JMenuItem aPropos = new JMenuItem(new AProposAction(this, "A propos"));
    		menu3.add(aPropos);
    		menuBar.add(menu3);
     
    		setJMenuBar(menuBar);
     
    		setTitle("CriSafe v1.0");
    		setSize(1024,768);
    		setLocationRelativeTo(null);
    		//setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setContentPane(buildContentPane());
    	}
     
    	private JPanel buildContentPane(){
    		JPanel panel = new JPanel();
    		panel.setLayout (new GridBagLayout ());
    		add (panel);
    		GridBagConstraints c = new GridBagConstraints();
     
    		JButton test1 = new JButton("coucou1");
    		c.gridx = 0;
    		c.gridy = 0;
    		c.gridheight = 1;
    		c.gridwidth = 2;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		panel.add(test1, c);
     
    		ImagePanel map = new ImagePanel(new ImageIcon(adressmap).getImage());
    		c.gridx = 0;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.gridheight = 2;
    		c.weightx = 1.0f;
    		c.weighty = 1.0f;
    		c.fill = GridBagConstraints.BOTH;
    		panel.add(map, c);
     
     
    		Buttons buttons = new Buttons();
    		c.gridx = 1;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.weightx = 0f;
    		c.weighty = 1.0f;
    		c.fill = GridBagConstraints.VERTICAL;
    		panel.add(buttons, c);
     
    		Legende leg = new Legende();
    		c.gridx = 0;
    		c.gridy = 3;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.weightx = 1.0f;
            c.weighty = 0f;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		panel.add(leg, c);
     
    		JTextArea test4 = new JTextArea("salut");
    		test4.setEditable(false);
    		//test4.setEnabled(false);
    		c.gridx = 1;
    		c.gridy = 2;
    		c.ipady = 218;
    		c.gridwidth = 1;
    		c.gridheight = 2;
    		c.weightx = 0f;
    		c.weighty = 0.0f;
    		c.fill = GridBagConstraints.BOTH;
    		panel.add(test4, c);
     
    		return panel;
    	}
     
    	class ImagePanel extends JPanel {
     
    		  private Image img;
     
    		  public ImagePanel(String img) {
    		    this(new ImageIcon(img).getImage());
    		  }
     
    		  public ImagePanel(Image img) {
    		    this.img = img;
    		    //Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    		    //setPreferredSize(size);
    		    //setMinimumSize(size);
    		    //setMaximumSize(size);
    		    //setSize(size);
    		    setLayout(null);
    		  }
     
    		  public void paintComponent(Graphics g) {
    		    g.drawImage(img, 0, 0, null);
    		  }
    	}
     
    	public JLabel getLabel(){
    		return label;
    	}
    }

Discussions similaires

  1. Problème avec gridbaglayout
    Par Mike888 dans le forum AWT/Swing
    Réponses: 13
    Dernier message: 13/10/2009, 21h38
  2. 2 problèmes : Thread & GridBagLayout
    Par M4veR1K dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 01/07/2009, 09h13
  3. Problème avec GridBagLayout
    Par alexis0587 dans le forum Agents de placement/Fenêtres
    Réponses: 6
    Dernier message: 25/03/2009, 22h57
  4. Problème de GridBagLayout (disposition jscrollbar)
    Par akiri dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 17/11/2007, 11h02
  5. Problème avec GridBagLayout dans JPanel
    Par caneman dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 19/12/2006, 22h32

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