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
| import java.awt.Dimension;
public class TestResize extends JFrame {
private JTextField textField;
public TestResize() {
super();
buildComponents();
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
}
private void buildComponents() {
setPreferredSize(new Dimension(230,200));
JPanel mainPanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(getSize());
}
};
mainPanel.setBorder(new EtchedBorder());
JScrollPane jsp = new JScrollPane(mainPanel);
setContentPane(jsp);
GridBagLayout gbl_mainPanel = new GridBagLayout();
gbl_mainPanel.rowWeights = new double[]{0.0, 0.0, 1.0};
gbl_mainPanel.columnWeights = new double[]{0.0, 1.0};
mainPanel.setLayout(gbl_mainPanel);
JLabel lblTitre = new JLabel("Titre");
GridBagConstraints gbc_lblTitre = new GridBagConstraints();
gbc_lblTitre.gridwidth = 2;
gbc_lblTitre.insets = new Insets(0, 0, 5, 5);
gbc_lblTitre.gridx = 0;
gbc_lblTitre.gridy = 0;
mainPanel.add(lblTitre, gbc_lblTitre);
JLabel label1 = new JLabel("New label");
GridBagConstraints gbc_label1 = new GridBagConstraints();
gbc_label1.insets = new Insets(0, 0, 5, 5);
gbc_label1.anchor = GridBagConstraints.EAST;
gbc_label1.gridx = 0;
gbc_label1.gridy = 1;
mainPanel.add(label1, gbc_label1);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 1;
mainPanel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel label2 = new JLabel("<html>JLabel contenant un texte trop long et qui pose problème lors du redimensionnement de l'IHM." +
" La JScrollbar verticale est encore affichée alors qu'il y a suffisament de place pour afficher tous les éléments du JPanel " +
"lorsqu'on agrandie la fenêtre horizontalement.</html>");
GridBagConstraints gbc_label2 = new GridBagConstraints();
gbc_label2.anchor = GridBagConstraints.NORTH;
gbc_label2.fill = GridBagConstraints.HORIZONTAL;
gbc_label2.gridwidth = 2;
gbc_label2.gridx = 0;
gbc_label2.gridy = 2;
mainPanel.add(label2, gbc_label2);
mainPanel.setPreferredSize(new Dimension(200,200));
}
} |
Partager