gpt4 book ai didi

java - 我应该使用什么设置来解决这个 GridBagLayout 大小调整问题?

转载 作者:行者123 更新时间:2023-11-29 04:52:34 25 4
gpt4 key购买 nike

我一直在寻找并尝试解决我在 GridBagLayout 调整大小时遇到​​的问题,但无济于事。问题是:

  1. 我将 JDialog 窗口的底部向下拖动以使其变高;搜索面板(下面屏幕截图中的面板上方)不断变高,只是添加了它的内容不需要的空白(我不希望这个浪费的空间占用它下面的滚动表的高度)。

  2. 如果我移动窗口的右侧使其变窄一点,即使我的屏幕和窗口可以显示其完整高度,滚动面板也会变得太短;出现垂直滚动条(我不喜欢面板垂直滚动,只是水平滚动。)

我的 JDialog 在右上部分包含以下组件,使用以下布局管理器和约束:

JPanel      level 1         uses GridBagLayout Setting 1 (see below)
JScrollPane level 2 uses GridBagLayout Setting 1
JPanel level 3 uses GridBagLayout Setting 2
JPanel level 4 uses GridBagLayout Setting 2
JPanel level 5 uses GridBagLayout Setting 2
JPanel level 5 uses GridBagLayout Setting 2

GridBagLayout.GridBagConstraints (c)

设置 1:

c.fill = GridBagConstraints.BOTH;
c.weightx = 99.0;
c.weighty = 99.0;

设置 2:

c.fill = GridBagConstraints.NONE;
c.weightx = 1.0;
c.weighty = 1.0;

本来,如果我不使用JScrollPane,这是我的JDialog:

enter image description here

如果我拖动窗口使其变大,JPanel 的大小(这是我在使用 JScrollPane 使其水平滚动之前使用的大小)保持不变这是。我想实现相同的行为,只是我想让它水平滚动。

我应该使用什么布局管理器来解决这个大小调整问题?如果我真的必须使用 GridBagLayout,我应该使用什么约束/设置?我将不胜感激任何建议。谢谢!

更新 1 ---------------------------------------- --------------

请检查下面的示例代码场景。两个可滚动面板(红色和蓝色)都使用 GridBagConstraints.BOTHweightxweighty 等于 99.0。我希望红色面板的高度足以包含文本字段。我还希望当对话框被向下拖动以使窗口变大时,红色面板的高度保持不变。请注意,在实际屏幕中,两个面板的内容数量可能会发生变化(存在加/减按钮),因此,设置最小面板大小(用于调整大小)将无济于事。请建议我应该如何处理这个案子。谢谢!

package cfr.view.search;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class GBLSample extends JDialog {

private JPanel contentPane;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GBLSample frame = new GBLSample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GBLSample() {
setBounds(100, 100, 350, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
contentPane.setLayout(gbl_contentPane);

// RED PANEL ------------------------------------------------
JScrollPane sp1 = new JScrollPane();
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill = GridBagConstraints.BOTH;
gbc1.weightx = 99.0;
gbc1.weighty = 99.0;
contentPane.add(sp1, gbc1);

JPanel redPanel = new JPanel();
redPanel.setBackground(Color.RED);
JTextField field = new JTextField(20);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.anchor = GridBagConstraints.PAGE_START;
gbc2.fill = GridBagConstraints.NONE;
contentPane.add(field, gbc2);

redPanel.add(field);
sp1.setViewportView(redPanel);

// BLUE PANEL ------------------------------------------------
JScrollPane sp2 = new JScrollPane();
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.fill = GridBagConstraints.BOTH;
gbc3.gridx = 0;
gbc3.gridy = 1;
gbc3.weightx = 99.0;
gbc3.weighty = 99.0;
contentPane.add(sp2, gbc3);

JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.BLUE);
sp2.setViewportView(bluePanel);
}
}

更新 2 ---------------------------------------- --------------

JPanel 的大小调整仍然存在与上述 #1 相同的问题。问题一

enter image description here

最佳答案

试试这段代码:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class GridBagLayoutSample extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutSample frame = new GridBagLayoutSample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public GridBagLayoutSample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{50, 0, 0};
//the 1.0 value in columnWeights let redPanel and scrollPane (that contain bleuPanel) grow horizontally
//the 1.0 value is in the first index so it affect the first column
//you still need to use a valid value for gbc_redPanel.fill and gbc_scrollPane.fill;
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
//the 0.0 value of rowWeights => components in first row will not grow
//the 0.0 value of rowWeights => components in second row will grow
gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

JPanel redPanel = new JPanel();
redPanel.setBackground(Color.RED);
GridBagConstraints gbc_redPanel = new GridBagConstraints();
gbc_redPanel.insets = new Insets(0, 0, 5, 0);
gbc_redPanel.fill = GridBagConstraints.BOTH;
gbc_redPanel.gridx = 0;
gbc_redPanel.gridy = 0;
contentPane.add(redPanel, gbc_redPanel);

JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
contentPane.add(scrollPane, gbc_scrollPane);

JPanel bleuPanel = new JPanel();
bleuPanel.setBackground(Color.BLUE);
//scrollpane work with the preferred size of the component in the viewportView
//try to change this values
bleuPanel.setPreferredSize(new Dimension(400, 400));
scrollPane.setViewportView(bleuPanel);
}

将此添加到您的代码中

gbl_contentPane.rowHeights = new int[]{40, 0, 0};
gbl_contentPane.rowWeights = new double[]{0.0,0.0, 1.0, Double.MIN_VALUE};
gbc1.gridx = 0;
gbc1.gridy = 0;

并删除这个

gbc1.weightx = 99.0;
gbc1.weighty = 99.0;

关于java - 我应该使用什么设置来解决这个 GridBagLayout 大小调整问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com