- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 Swing 有一段时间了,我终于找到了学习 GridBadLayout 的自信。
我仍在学习它,在这种情况下,我无法理解为什么以下代码没有按预期响应:特别是我无法理解为什么布局以这种方式显示列。
通过运行代码片段,您将发现代表意大利国旗的面板位置不正确:最后一列(国旗的红色部分)与国旗的其余部分(国旗的白色部分)分离。那么,我做错了什么以及我可以修复什么才能正确表示标志?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JPanel {
private GridBagConstraints gbc;
private final int CELL_WIDTH = 90;
private final int CELL_HEIGHT = 110;
public test() {
setLayout(new GridBagLayout());
putBanner(0);
putFlagRow(1);
putFlagRow(2);
putFlagRow(3);
putFlagRow(4);
}
public JPanel getUserPanel(Color c) {
JPanel ol = new JPanel();
ol.setPreferredSize(new Dimension(CELL_WIDTH * 10, CELL_HEIGHT));
ol.setBackground(c);
return ol;
}
public JPanel gettestPanel() {
JPanel ol = new JPanel();
ol.setPreferredSize(new Dimension(CELL_WIDTH, CELL_HEIGHT));
ol.setBackground(Color.white);
return ol;
}
private void putFlagRow(int gridy) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = gridy;
JPanel p1=gettestPanel();
p1.setBackground(Color.green);
add(p1, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 5;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 7;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 8;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 9;
gbc.gridy = gridy;
JPanel p=gettestPanel();
p.setBackground(Color.red);
add(p, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
}
private void putBanner(int gridy) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = gridy;
gbc.gridwidth = 1;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = gridy;
gbc.gridwidth = 9;
add(getUserPanel(Color.black), gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridBagLayoutTest");
frame.add(new test());
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
除了了解这个特定问题及其原因之外,我还想了解这一点,它直接来自 GridBagLayout Oracle 文档:
weightx, weightySpecifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights areused to determine how to distribute space among columns (weightx) andamong rows (weighty); this is important for specifying resizingbehavior. Unless you specify at least one non-zero value for weightxor weighty, all the components clump together in the center of theircontainer. This is because when the weight is 0.0 (the default), theGridBagLayout puts any extra space between its grid of cells and theedges of the container. Generally weights are specified with 0.0 and1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should getmore space. For each column, the weight is related to the highestweightx specified for a component within that column, with eachmulticolumn component's weight being split somehow between the columnsthe component is in. Similarly, each row's weight is related to thehighest weighty specified for a component within that row. Extra spacetends to go toward the rightmost column and bottom row.
类似的表达式
is an art
和
the numbers in between are used as necessary
确实给我的印象是,即使是文档作者也认为不可能教授这种约束的用法。这可不好
最佳答案
the last column(red part of the flag) is detached from the rest of the flag(white part of the flag).
调试GridBagLayout
时要做的一件事是向组件添加Border
,以便您可以看到每个单元格的大小。我将以下内容添加到您的 getTestPanel()
方法中:
ol.setBorder( new LineBorder(Color.BLUE) );
您可以看到面板大小正确,但面板周围有额外的空间。
然后你看看你的用户面板:
ol.setPreferredSize(new Dimension(CELL_WIDTH * 10, CELL_HEIGHT));
即使您只将网格宽度设置为 9 个单元格,您也会看到大小设置为 10 个单元格。所以现在 9 个单元格必须适合用户面板的空间,看起来额外的空间被分配给了 9 列中的最后一列。
代码应该是:
//ol.setPreferredSize(new Dimension(CELL_WIDTH * 10, CELL_HEIGHT));
ol.setPreferredSize(new Dimension(CELL_WIDTH * 9, CELL_HEIGHT));
另一种选择是不为用户面板提供首选大小,而是使用“填充”约束,以便用户面板现在占用与九列相同的空间。
weightx, weighty Specifying weights is an art...
这与您的问题无关。要查看此效果,请将框架拖得更大。现在,所有整个面板都将位于框架的中心,因为您的组件都没有使用weightx/weighty约束。这意味着如果框架没有任何组件会增长来填充空白空间。
如果其中一个组件的weightx 约束为1.0,则当调整框架大小时,该列将扩展并填充额外的空间。
关于java - GridBagLayout 为列提供额外的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49031902/
我配置了我的RouteInitializer如下: class AppRouteInitializer implements RouteInitializer { init(Router rout
我正在尝试从 Android 应用程序发送短信。我正在使用 PendingIntent 以便我可以使用 Broadcast Receiver 检查它是否发送正常。由于 sendTextMessage
目录 简介 1 "额外"字段是什么 1.1 "额外"是指与业务无关 1.2 产生
应用程序读取 JSON 数据。然后它会将其放入 ListView (正确),但在按下某个项目后,我总是会得到显示的相同值。下面的代码我认为是问题所在,但我找不到。 try{ JSONArray
我正在使用以下代码 (Kotlin) 创建通知 val builder = NotificationCompat.Builder(ctx) ........ .set
我有一个问题。现在我正在使用 3 个面板,mainPanel 和其他 2 个面板(btnPanel 和 iconPanel)。所以问题是当我按下“重置”按钮时,我删除了 iconPanel 并再次添加
这是我的 html: Settings Export Import 和CSS: span.button { float:right; margin-righ
我正在尝试将一个结构编码为 JSON,然后将其插入我的 Mongo 数据库,但不断出现此错误:%!(EXTRA main.Test={575590180 Me})。我究竟做错了什么?我完全从我从事的另
嘿,我遇到了这些 latex 格式问题,有人可以提供一些帮助吗? .tex 文件: \begin{table}{} \renewcommand{\arraystretch}{1.1} \c
我在 FragmentPagerAdapter 中使用了 Fragment 的 ArrayList。 我想在 saveState() 中保存此 ArrayList 的状态,并在 restoreStat
我做了this MapKit-教程 一切正常,但如何为我的 pin 添加额外的属性? 这是我的课车: import Foundation import MapKit class Car: NSObje
关于 Android intent 将提供的附加功能有哪些文档? 更新: 我做了一些进一步的调查。我知道我们可以假设每个 Intent 都不会解析任何数据或额外内容,除非有明确记录。此外,一些(但不是
我在 python3.4.3 上使用 SqlAlchemy 来管理 MySQL 数据库。我正在创建一个表: from datetime import datetime from sqlalchemy
我正在使用 bootstrap 创建网页。我在两个 block (内容和标题)上派生了正文。在内容 block 中,我有 div 类 .container .sameTable 在里面我有 div 类
我在Windows 7上的MinGW和MSYS下使用gfortran构建了一些fortran程序。但是当我在未安装MinGW和MSYS的其他计算机上运行它们时,系统总是要求一些dll,例如libgfo
第一个元素的右侧似乎有额外的间距,我不知道它是从哪里来的。有人可以帮助我吗? 这是我使用的代码: http://jsfiddle.net/srabeat/tenx4y1c/1/ for (i = 0;
我使用 fs-extra 收到以下错误: ERROR { [Error: EPERM: operation not permitted, unlink 'C:\Projects\xxx\branche
我正在尝试在 CBC 模式下使用 AES-128 加密 320 字节的二进制数据,并将密码存储到一个文件中。输出文件应该是 320 字节,但我得到了 336 字节。这是我的代码: #include
我有一个特定的要求,我必须从我的 Activity 中触发浏览器上的 url。我可以使用以下代码执行此操作: Intent browserIntent = new Intent( Intent.A
我正在使用 JMS DI 注入(inject)带有注解的服务: use JMS\DiExtraBundle\Annotation as DI; /** * @DI\Service("foo.bar.
我是一名优秀的程序员,十分优秀!