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
|
package swing.tests;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
public class GridBagTest extends JPanel {
private JLabel label1;
private JTextField textField1;
private JButton button1;
private JButton button2;
private JButton button3;
private JTextArea textArea;
private JProgressBar progressBar1;
public GridBagTest() {
label1 = new JLabel("label1");
textField1 = new JTextField("Textfield1");
textField1.setColumns(50);
button1 = new JButton("button1");
button2 = new JButton("button2");
button3 = new JButton("button3");
textArea = new JTextArea("textarea1");
progressBar1 = new JProgressBar();
progressBar1.setValue(58);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
//Top-left Label - pas de redimensionnement
constraints.gridy=0; //Ligne du composant
constraints.gridy=0; //Position du composant sur la ligne
constraints.fill=GridBagConstraints.VERTICAL; //Precise que le composant doit remplir l'espace disponible afin d'avoir une taille homogène avec les autres composants de la ligne
constraints.anchor = GridBagConstraints.BASELINE_LEADING; //Sert a aligner le texte des composants sur la baseline, c'est à dire que le texte de tous les composants en BASELINE_XXX sera aligné sur une même ligne horizontale
this.add(label1,constraints);
constraints = new GridBagConstraints();
//Top center textfields - redimensionnement horizontal
constraints.gridy=0;
constraints.gridx=1;
constraints.weightx=1.0f; //Précise la politique de redimensionnement horizontal: 1.0 se redimensionner autant que possible, 0.0 (valeur par défaut) ne redimensionne pas, ne marche que si le fill est HORIZONTAL ou BOTH
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.BASELINE_LEADING;
this.add(textField1,constraints);
constraints = new GridBagConstraints();
//Top right button - pas de redimensionnement
constraints.gridy=0;
constraints.gridx=2;
constraints.fill=GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.BASELINE_LEADING;
this.add(button1,constraints);
//Top right button - pas de redimensionnement
constraints.gridy=0;
constraints.gridx=3;
constraints.fill=GridBagConstraints.VERTICAL;
constraints.anchor = GridBagConstraints.BASELINE_LEADING;
this.add(button2,constraints);
constraints = new GridBagConstraints();
//Second line button, centre, pas de redimensionnement, on occuppe toute la ligne
constraints.gridy=1;
constraints.gridx=0;
constraints.gridwidth=4; //On précise que le composant occupe 3 cases (ce qui correspond à l'intégralité de la ligne)
constraints.fill=GridBagConstraints.VERTICAL; //On lui précise de ne pas occuper l'espace horizontal
constraints.anchor = GridBagConstraints.BASELINE; //On précise que le composant doit se centrer sur la ligne
this.add(button3,constraints);
constraints = new GridBagConstraints();
//Texarea, centre, redimensionnement global dans scrollPanel
constraints.gridy=2;
constraints.gridx=0;
constraints.gridwidth=4;
constraints.weightx=1.0f;
constraints.weighty=1.0f; //Précise la politique de redimensionnement vertical: 1.0 se redimensionner autant que possible, 0.0 (valeur par défaut) ne redimensionne pas, ne marche que si le fill est VERTICAL ou BOTH
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.BASELINE_LEADING;
this.add(scrollPane,constraints);
constraints = new GridBagConstraints();
//progressBar, centre, redimensionnement horizontal
constraints.gridy=3;
constraints.gridx=0;
constraints.gridwidth=4;
constraints.weightx=1.0f;
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.BASELINE_LEADING;
this.add(progressBar1,constraints);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
// TODO Bloc catch auto-généré
e.printStackTrace();
}
JFrame f = new JFrame();
f.add(new GridBagTest());
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
} |
Partager