Des largeurs fixes dans un GridBagLayout

Des largeurs fixes dans un GridBagLayout

Je suis en utilisant le GridBagLayout pour faire une Barre d'état qui ressemble à l'image. J'ai 4 zones, donc, j'ai un bouton dans la première, puis de messages d'information dans le second, et puis je veux deux de plus (et j'ai aussi un cinquième de faire le coin).

La zone du bouton s'adapte parfaitement, parce que le contenu est toujours un bouton avec la même largeur. De même avec le coin de la zone. La zone d'informations doit obtenir tout l'espace disponible. 3ème et 4ème régions doivent avoir une valeur fixe, indépendant de la taille de l'écran.

Comment puis-je le faire?

Mon code actuel est:

public MyStatusBar() {
setLayout(new GridBagLayout());
setPreferredSize(new Dimension(getWidth(), 23));
GridBagConstraints c = new GridBagConstraints();
botonDispositivo = new JButton("");
this.setText(0, "Disconnected");
URL imageUrl = getClass().getResource("resources/22x22/dispositivo01.png");
this.setButtonImg(imageUrl);
this.setButtonEnabled(false);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 1;
c.gridheight = 1;
this.add(botonDispositivo, c);
c.insets= new Insets(0,10,0,0);
c.gridx ++;
c.gridy = 0;
c.weightx = 0.7;
c.fill = GridBagConstraints.HORIZONTAL;
msgLabel = new JLabel("");
msgLabel.setPreferredSize(new Dimension(500, msgLabel.getHeight()));
this.add(msgLabel, c);
this.setText(1, "Waiting for potentiostat conection");
c.gridx ++;
c.gridy = 0;
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);
c.gridx ++;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
c.weightx = 0.0;
overErrorLabel = new JLabel("");
overErrorLabel.setSize(new Dimension(150, overErrorLabel.getHeight()));
this.add(overErrorLabel, c);
//this.setText(2, "");
c.gridx ++;
c.gridy = 0;
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);
c.gridx ++;
c.gridy = 0;
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.EAST;
timeLabel = new JLabel("");
timeLabel.setMinimumSize(new Dimension(150, timeLabel.getHeight()));
//timeLabel.setMaximumSize(new Dimension(150, timeLabel.getHeight()));
this.add(timeLabel, c);
//this.setText(3, "");
JPanel rightPanel = new JPanel(new GridBagLayout());
c.gridx ++;
c.gridy = 0;
c.weightx = 0.0;
c.fill = GridBagConstraints.BOTH;
rightPanel.add(new JLabel(new AngledLinesWindowsCornerIcon()), c);
rightPanel.setOpaque(false);
c.gridx ++;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
this.add(rightPanel, c);
setBackground(SystemColor.control);
}

OriginalL'auteur Roman Rdgz | 2011-07-27