java - 根据JFrame Resize控制JPanels大小

标签 java jframe jpanel height resizable

我想拥有两个大小完全相同的可调整大小的Jpanels(jpanelDarkGrayjpanelLightGray),并由JFrame中包含的另一个可调整大小的JPanel(jpanelMidSep)隔开。

jpanelDarkGrayjpanelLightGray的初始高度为146jpanelMidSep的初始高度为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
...........................................................

最佳答案

您必须测试以下代码:

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

关于java - 根据JFrame Resize控制JPanels大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672943/

相关文章:

Java 同时淡入和淡出两个 JPanel

java - 将 JPanel 添加到 JPanel

java - 将时间戳转换为字符串而不指定格式

java - 我应该用什么代替 FlowLayout()?

java - 是否可以从文本文件中读取多行并将其导出到各自的文本字段?

java - 从类创建 JFrame 并从中获取输入

java - 将图像拖放到Jpanel中并将其放入应用程序目录中

java - 如何在电子邮件中包含表格

java - 如何移动 Primefaces-Upload-Temp-Files 而不是进行耗时的复制过程?

java - 关于 "Java Concurrency in Practice"示例的问题