根据JFrame Resize控制JPanels大小

时间:2016-04-17 05:16:08

标签: java jframe jpanel height resizable

我希望有两个可调整大小的Jpanel(jpanelDarkGrayjpanelLightGray),其大小完全相同,由JFrame中包含的另一个可调整大小的JPanel(jpanelMidSep)隔开...

jpanelDarkGrayjpanelLightGray的初始高度为146,而jpanelMidSep的初始高度为8

有时,在调整JFrame大小时,jpanelDarkGray的高度大于jpanelLightGray 1个单位。 我希望通过将jpanelMidSep的高度更改为1个单位(将高度jpanelDarkGray减少1个单位并将jpanelMidSep的高度增加1个单位)再次完全相同的大小。

这是我的所有代码:

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);
      }
    });
  }
}

但是每次更改JForm的大小时,jSepMid高度都会不受控制地增长(使用 setPreferredSize 方法)或者未达到目标(可控制地更改高度)(使用 setSize 方法)。

使用 setSize 方法的输出(当JFrame的高度为奇数时,jpanelDarkGrayjpanelLightGray的高度我无法做同样的)。

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

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

一些线索?

编辑1

测试@chepe_lucho的解决方案......对我来说工作正常!

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

2 个答案:

答案 0 :(得分:1)

您必须测试此代码:

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));
  }
}

答案 1 :(得分:0)

您可以将GridLayout用于Container JFrame。设置行和列。在您的情况下,它将是2行和1列。 网格布局将与所有组件共享相同的区域

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

尝试将GridLayout设置为帧并尝试调整帧的大小。它将调整面板面积相等的大小。 并且不要将setPrefferedSize()用于面板。