简体   繁体   中英

controlling JPanels size according to JFrame Resize

I want to have two resizable Jpanels ( jpanelDarkGray and jpanelLightGray ) with exactly same Size separated by another resizable JPanel ( jpanelMidSep ) contained in a JFrame...

The initial Height for jpanelDarkGray and jpanelLightGray is 146 and for jpanelMidSep is 8 .

Sometimes when JFrame is resized jpanelDarkGray has Height greater than jpanelLightGray by 1 unit. I want to do it again exactly the same size by changing the height of jpanelMidSep by 1 unit (reducing the height jpanelDarkGray by 1 unit and increasing the height of jpanelMidSep by 1 unit).

Here all my 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);
      }
    });
  }
}

But every time you change the size of JForm, jSepMid height grows uncontrollably (using setPreferredSize method) or the goal (to change the height controllably) is not reached (using setSize method).

The output using setSize method ( when the height of JFrame is odd, the heights of jpanelDarkGray and jpanelLightGray I could not do the same ).

在此处输入图片说明

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
...........................................................

The output using setPreferredSize method.

在此处输入图片说明

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
...........................................................

Some clue?

EDIT 1

testing the solution of @chepe_lucho... work fine for me!

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
...........................................................

You must test this 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));
  }
}

You can use GridLayout for Container JFrame. Set the rows and columns. In your case it will be 2 rows and 1 column. The Grid Layout will share equal area to all the components

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

Try to set GridLayout to frame and try to resize the frame. It will resize the panels with equal area. And do not use setPrefferedSize() to panels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM