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 : http://fabiendupre.free.fr/try/test.JPG
Et j'aimerai avoir : http://fabiendupre.free.fr/try/test2.JPG 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:
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;
}
} |
:cry:
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.
http://fabiendupre.free.fr/try/test3.JPG
Code:
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;
}
} |