2010-04-19 7 views
8

J'essaie de créer une fenêtre graphique dans mon application. Ce que j'essaie de faire est d'avoir une fenêtre, avec quelques boutons en haut, et une grande zone de texte. Quelque chose comme ceci:Alignement mixte avec GroupLayout de Java Swing

+--------------------------------------------------+ 
| [button1] [button2]     [button3] | 
| +----------------------------------------------+ | 
| | text area         | | 
| |            | | 
| |            | | 
| |            | | 
| +----------------------------------------------+ | 
+--------------------------------------------------+ 

Je suis presque, en utilisant GroupLayout:

layout.setHorizontalGroup(
    layout.createParallelGroup() 
     .addGroup(layout.createSequentialGroup() 
     .addComponent(button1) 
     .addComponent(button2)) 
     .addComponent(closeWindow)) 
     .addComponent(textarea1) 
); 

    layout.setVerticalGroup(
    layout.createSequentialGroup() 
     .addGroup(layout.createParallelGroup() 
     .addComponent(button1) 
     .addComponent(button2) 
     .addComponent(button3)) 
     .addComponent(textarea) 
); 

Le problème est que cela se retrouve avec button3 aligné sur la gauche, avec les deux autres. Je n'arrive pas à comprendre comment je peux spécifier l'alignement sur ce seul bouton. Je peux faire GroupLayout.Alignment.TRAILING sur toute la barre de boutons, mais cela touche tous les 3 boutons, ce qui n'est pas tout à fait correct.

Alors, quelle est la bonne approche? Puisque l'alignement ne s'applique qu'aux Groupes Parallèles, je ne pense pas qu'un HorizontalGroup avec deux Groupes Séquentiels puisse aider.

Qu'est-ce qui me manque?

Répondre

11

Ajoutez un espace dans votre groupe séquentiel. En quittant votre groupe horizontal est:

layout.setVerticalGroup(
    layout.createSequentialGroup() 
     .addGroup(layout.createParallelGroup() 
     .addComponent(button1) 
     .addComponent(button2) 
     .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addComponent(button3)) 
     .addComponent(textarea) 
); 

L'écart avec ces paramters agit comme un « printemps », en prenant tout l'espace disponible.

+0

devrait être "LayoutStyle.ComponentPlacement.RELATED", mais à part ça, fonctionne très bien, merci :) – zigdon

+0

Whoops, vous avez raison - j'ai mélangé versions. Fixé. – Etaoin

+0

Salut, devriez-vous seulement ajouter cette ligne au groupe vertical ou aussi au groupe horizontal? – Timmos

3

Essayez d'ajouter:

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE) 

après le deuxième bouton. Le MAX_VALUE provoquera l'expansion de l'écart autant que nécessaire.

1

Vous souhaitez utiliser addPreferredGap() uniquement disponible pour les groupes séquentiels. Le code ci-dessous vous donne la disposition souhaitée.

layout.setHorizontalGroup(
      layout.createParallelGroup() 
        .addGroup(layout.createSequentialGroup() 
          .addComponent(button1) 
          .addComponent(button2) 
          .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE) 
          .addComponent(button3)) 
        .addComponent(textArea) 
    ); 
    layout.setVerticalGroup(
      layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup() 
          .addComponent(button1) 
          .addComponent(button2) 
          .addComponent(button3)) 
        .addComponent(textArea) 
    );