2016-04-17 1 views
1

Je veux avoir deux JPanels redimensionnables (jpanelDarkGray et jpanelLightGray) avec exactement la même taille séparés par un autre redimensionnables JPanel (jpanelMidSep) contenues dans un JFrame ...contrôle JPanels taille selon JFrame Redimensionner

La hauteur initiale pour jpanelDarkGray et jpanelLightGray est 146 et pour jpanelMidSep est 8.

Parfois, lorsque JFrame est redimensionné jpanelDarkGray a Hauteur supérieure à jpanelLightGray par 1 unité. Je veux le faire exactement la même taille en changeant la hauteur de jpanelMidSep par 1 unité (en réduisant la hauteur jpanelDarkGray par 1 unité et en augmentant la hauteur de jpanelMidSep par 1 unité).

Ici tout mon code:

package thePack; 

import java.awt.Dimension; 
import java.awt.Color; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 
import java.awt.EventQueue; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class JF_EqualSizeMovingPanels extends JFrame { 

    private JPanel jpanelDarkGray; 
    private JPanel jpanelLightGray; 
    private JPanel jpanelMidSep; 

    public JF_EqualSizeMovingPanels() { 
    initComponents(); 
    } 


    @SuppressWarnings("unchecked") 
    private void initComponents() { 

    jpanelDarkGray = new JPanel(); 
    jpanelMidSep = new JPanel(); 
    jpanelLightGray = new JPanel(); 

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    addComponentListener(new ComponentAdapter() { 
     public void componentResized(ComponentEvent evt) { 
     formComponentResized(evt); 
     } 
    }); 

    jpanelDarkGray.setBackground(new Color(96, 96, 96)); 

    GroupLayout jpanelDarkGrayLayout = new GroupLayout(jpanelDarkGray); 
    jpanelDarkGray.setLayout(jpanelDarkGrayLayout); 
    jpanelDarkGrayLayout.setHorizontalGroup(
     jpanelDarkGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    jpanelDarkGrayLayout.setVerticalGroup(
     jpanelDarkGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 146, Short.MAX_VALUE) 
    ); 

    jpanelMidSep.setBackground(new Color(128, 128, 128)); 

    GroupLayout jpanelMidSepLayout = new GroupLayout(jpanelMidSep); 
    jpanelMidSep.setLayout(jpanelMidSepLayout); 
    jpanelMidSepLayout.setHorizontalGroup(
     jpanelMidSepLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jpanelMidSepLayout.setVerticalGroup(
     jpanelMidSepLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 8, Short.MAX_VALUE) 
    ); 

    jpanelLightGray.setBackground(new Color(160, 160, 160)); 

    GroupLayout jpanelLightGrayLayout = new GroupLayout(jpanelLightGray); 
    jpanelLightGray.setLayout(jpanelLightGrayLayout); 
    jpanelLightGrayLayout.setHorizontalGroup(
     jpanelLightGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jpanelLightGrayLayout.setVerticalGroup(
     jpanelLightGrayLayout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGap(0, 146, Short.MAX_VALUE) 
    ); 

    GroupLayout layout = new GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addComponent(jpanelDarkGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addComponent(jpanelMidSep, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addComponent(jpanelLightGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
     .addComponent(jpanelDarkGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     .addGap(0, 0, 0) 
     .addComponent(jpanelMidSep, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
     .addGap(0, 0, 0) 
     .addComponent(jpanelLightGray, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 

    pack(); 
    } 

    private void formComponentResized(java.awt.event.ComponentEvent evt) { 
    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); 
    System.out.println(this.getName()+ " - Height:" + this.getHeight() 
     + ", Width:" + this.getWidth()); 
    System.out.println(); 
    System.out.println("jpanelDarkGray - Height:" + jpanelDarkGray.getHeight() 
     + ", Width:" + jpanelDarkGray.getWidth()); 
    System.out.println("jpanelMidSep - Height:" + jpanelMidSep.getHeight() 
     + ", Width:" + jpanelMidSep.getWidth()); 
    System.out.println("jpanelLightGray - Height:" + jpanelLightGray.getHeight() 
     + ", Width:" + jpanelLightGray.getWidth()); 
    int adding = (jpanelDarkGray.getHeight() + jpanelMidSep.getHeight() + jpanelLightGray.getHeight()); 
    System.out.println("Adding:" + adding + ", diff:" + (this.getHeight() - adding)); 
    System.out.println("..........................................................."); 
    if (jpanelDarkGray.getHeight() != jpanelLightGray.getHeight()) { 
     int diff; 
     if (jpanelDarkGray.getHeight() > jpanelLightGray.getHeight()) { 
     diff = jpanelDarkGray.getHeight() - jpanelLightGray.getHeight(); 
     jpanelDarkGray.setSize(jpanelDarkGray.getWidth(), jpanelLightGray.getHeight()); 
//  jpanelDarkGray.setPreferredSize(new Dimension(jpanelDarkGray.getWidth(), 
//   jpanelLightGray.getHeight())); 
     jpanelMidSep.setSize(jpanelMidSep.getWidth(), jpanelMidSep.getHeight() + diff); 
//  jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 
//   jpanelMidSep.getHeight() + diff)); 
     } 
     if (jpanelLightGray.getHeight() > jpanelDarkGray.getHeight()) { 
     diff = jpanelLightGray.getHeight() - jpanelDarkGray.getHeight(); 
     jpanelLightGray.setSize(jpanelLightGray.getWidth(), jpanelDarkGray.getHeight()); 
//  jpanelLightGray.setPreferredSize(new Dimension(jpanelLightGray.getWidth(), 
//   jpanelDarkGray.getHeight())); 
     jpanelMidSep.setSize(jpanelMidSep.getWidth(), jpanelMidSep.getHeight() + diff); 
     jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 
      jpanelMidSep.getHeight() + diff)); 
     } 
    } 
    } 

    public static void main(String args[]) { 
    try { 
     for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
     if ("Nimbus".equals(info.getName())) { 
      UIManager.setLookAndFeel(info.getClassName()); 
      break; 
     } 
     } 
    } catch (ClassNotFoundException | InstantiationException | 
      IllegalAccessException | UnsupportedLookAndFeelException ex) { 
     Logger.getLogger(JF_EqualSizeMovingPanels.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
     new JF_EqualSizeMovingPanels().setVisible(true); 
     } 
    }); 
    } 
} 

Mais chaque fois que vous changez la taille de JForm, la hauteur jSepMid pousse de façon incontrôlable (en utilisant setPreferredSize méthode) ou le but (pour changer la hauteur contrôlée) n'est pas atteint (en utilisant la méthode setSize).

La sortie en utilisant méthode setSize (lorsque la hauteur de DFrame est impair, les hauteurs de jpanelDarkGray et jpanelLightGray I ne pouvaient pas faire de même).

enter image description here

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:354, Width:416 

jpanelDarkGray - Height:154, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:154, Width:400 
Adding:316, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:416 

jpanelDarkGray - Height:143, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:142, Width:400 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:438 

jpanelDarkGray - Height:143, Width:422 
jpanelMidSep - Height:8, Width:422 
jpanelLightGray - Height:142, Width:422 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:331, Width:457 

jpanelDarkGray - Height:143, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:142, Width:441 
Adding:293, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:322, Width:457 

jpanelDarkGray - Height:138, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:138, Width:441 
Adding:284, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:326, Width:457 

jpanelDarkGray - Height:140, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:140, Width:441 
Adding:288, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:457 

jpanelDarkGray - Height:133, Width:441 
jpanelMidSep - Height:8, Width:441 
jpanelLightGray - Height:132, Width:441 
Adding:273, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:439 

jpanelDarkGray - Height:133, Width:423 
jpanelMidSep - Height:8, Width:423 
jpanelLightGray - Height:132, Width:423 
Adding:273, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:311, Width:408 

jpanelDarkGray - Height:133, Width:392 
jpanelMidSep - Height:8, Width:392 
jpanelLightGray - Height:132, Width:392 
Adding:273, diff:38 
........................................................... 

La sortie en utilisant procédé setPreferredSize.

enter image description here

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:342, Width:416 

jpanelDarkGray - Height:148, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:148, Width:400 
Adding:304, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:145, Width:400 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:420 

jpanelDarkGray - Height:145, Width:404 
jpanelMidSep - Height:9, Width:404 
jpanelLightGray - Height:145, Width:404 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:442 

jpanelDarkGray - Height:145, Width:426 
jpanelMidSep - Height:9, Width:426 
jpanelLightGray - Height:145, Width:426 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:337, Width:430 

jpanelDarkGray - Height:145, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:145, Width:414 
Adding:299, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:313, Width:430 

jpanelDarkGray - Height:133, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:133, Width:414 
Adding:275, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:317, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:135, Width:414 
Adding:279, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:315, Width:430 

jpanelDarkGray - Height:134, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:134, Width:414 
Adding:277, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:317, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:135, Width:414 
Adding:279, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:318, Width:430 

jpanelDarkGray - Height:135, Width:414 
jpanelMidSep - Height:9, Width:414 
jpanelLightGray - Height:136, Width:414 
Adding:280, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:318, Width:447 

jpanelDarkGray - Height:140, Width:431 
jpanelMidSep - Height:10, Width:431 
jpanelLightGray - Height:130, Width:431 
Adding:280, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:320, Width:447 

jpanelDarkGray - Height:129, Width:431 
jpanelMidSep - Height:20, Width:431 
jpanelLightGray - Height:133, Width:431 
Adding:282, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:322, Width:447 

jpanelDarkGray - Height:130, Width:431 
jpanelMidSep - Height:24, Width:431 
jpanelLightGray - Height:130, Width:431 
Adding:284, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:447 

jpanelDarkGray - Height:133, Width:431 
jpanelMidSep - Height:24, Width:431 
jpanelLightGray - Height:133, Width:431 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:457 

jpanelDarkGray - Height:133, Width:441 
jpanelMidSep - Height:24, Width:441 
jpanelLightGray - Height:133, Width:441 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:133, Width:385 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:328, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:133, Width:385 
Adding:290, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:327, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:132, Width:385 
Adding:289, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:327, Width:401 

jpanelDarkGray - Height:133, Width:385 
jpanelMidSep - Height:24, Width:385 
jpanelLightGray - Height:132, Width:385 
Adding:289, diff:38 
........................................................... 

Certains indices?

EDIT 1

tester la solution de @chepe_lucho ... excellent travail pour moi!

run: 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:338, Width:416 

jpanelDarkGray - Height:146, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:146, Width:400 
Adding:300, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:343, Width:416 

jpanelDarkGray - Height:148, Width:400 
jpanelMidSep - Height:8, Width:400 
jpanelLightGray - Height:149, Width:400 
Adding:305, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:343, Width:422 

jpanelDarkGray - Height:148, Width:406 
jpanelMidSep - Height:9, Width:406 
jpanelLightGray - Height:148, Width:406 
Adding:305, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:292, Width:422 

jpanelDarkGray - Height:123, Width:406 
jpanelMidSep - Height:9, Width:406 
jpanelLightGray - Height:122, Width:406 
Adding:254, diff:38 
........................................................... 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
frame0 - Height:292, Width:460 

jpanelDarkGray - Height:123, Width:444 
jpanelMidSep - Height:8, Width:444 
jpanelLightGray - Height:123, Width:444 
Adding:254, diff:38 
........................................................... 
+0

Vous essayez de construire votre propre volet de Split comme le volet Split Swing où l'utilisateur peut modifier la taille du panneau ou un code fait le changement? – DevilsHnd

+0

Je veux toujours avoir deux panneaux avec exactement la même taille (hauteur et largeur).Le conteneur (dans ce cas JFrame) est redimensionnable librement, mais la taille de jpanelDarkGray et jpanelLightGray n'est pas libre, est contrôlée. –

+0

'GridBagLayout' a des problèmes d'arrondi, ce qui arrive parfois à un point où il doit décider comment garder tous les composants visibles dans l'espace disponible – MadProgrammer

Répondre

1

Vous devez tester ce code:

if (jpanelDarkGray.getHeight() != jpanelLightGray.getHeight()) { 
    if (jpanelMidSep.getHeight() == 8) { 
    jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 9)); 
    } 
    if (jpanelMidSep.getHeight() == 9) { 
    jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 8)); 
    } 
} 
0

Vous pouvez utiliser GridLayout pour le conteneur JFrame. Définissez les lignes et les colonnes. Dans votre cas, ce sera 2 lignes et 1 colonne. La mise en page Grille partagera superficie égale à tous les composants

jframe.setLayout(new GridLayout(2,1)); 

Essayez de mettre GridLayout à encadrer et à essayer de redimensionner le cadre. Il va redimensionner les panneaux à surface égale. Et n'utilisez pas setPrefferedSize() pour les panneaux.

+1

Etant donné que l'OP veut trois paramètres, avec deux ont été de taille égale et le troisième (je suppose être variable), 'GridLayout' n'aidera pas – MadProgrammer