java - GridBagLayout 中的锚定约束不起作用
问题描述
import java.awt.*;
import javax.swing.*;
public class GBLClumpingExample extends JFrame{
GBLClumpingExample(){
GridBagLayout g = new GridBagLayout();
GridBagConstraints gv = new GridBagConstraints();
GridBagLayout b = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
setVisible(true);
setSize(720,720);
setLayout(g);
JPanel p = new JPanel();
p.setLayout(b);
gv.fill = GridBagConstraints.BOTH;
add(p,gv);
Label l1 = new Label("Label 1");
Label l2 = new Label("Label 2");
Label l3 = new Label("Label 3");
Label l4 = new Label("Label 4");
Label l5 = new Label("Label 5");
gc.weightx =1.0;
gc.weighty = 1.0;
gc.gridx= 1;
gc.gridy= 1;
gc.anchor = GridBagConstraints.PAGE_START;
gc.gridx= -1;
gc.gridy= 0;
p.add(l1,gc);
gc.anchor = GridBagConstraints.SOUTH;
p.add(l2,gc);
gc.anchor = GridBagConstraints.EAST;
p.add(l3,gc);
gc.anchor = GridBagConstraints.WEST;
p.add(l4,gc);
gc.anchor = GridBagConstraints.CENTER;
p.add(l5,gc);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
GBLClumpingExample e = new GBLClumpingExample();
}
}
我试图使用GridBagLayout
,但它可能无法正常工作。
这是我的代码,我不知道出了什么问题,但是锚约束GridBagConstraints
不起作用,它们都只是聚集在一起。
解决方案
您正在将组件添加到面板p
,然后使用add(p,gv);
. 中的约束gv
已用 初始化gv.fill = GridBagConstraints.BOTH;
,但其weightx
和weighty
保留在初始零处。因此,此面板将保持其首选大小并且不会获得额外空间,因此它没有额外空间来分发其自己的内容。
由于所有标签的大小相同,因此在没有额外空间时,它们的锚点无效。
当你改变线路时
gv.fill = GridBagConstraints.BOTH;
到
gv.fill = GridBagConstraints.BOTH;
gv.weightx = 1;
gv.weighty = 1;
你会看到锚的效果。或者,您可以摆脱附加面板。还有其他冗余操作。您可以将代码简化为:
import java.awt.*;
import javax.swing.*;
public class GBAnchorExample extends JFrame{
GBAnchorExample() {
Container c = super.getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = gc.weighty = 1.0;
JLabel l1 = new JLabel("Label 1");
JLabel l2 = new JLabel("Label 2");
JLabel l3 = new JLabel("Label 3");
JLabel l4 = new JLabel("Label 4");
JLabel l5 = new JLabel("Label 5");
gc.anchor = GridBagConstraints.PAGE_START;
c.add(l1,gc);
gc.anchor = GridBagConstraints.SOUTH;
c.add(l2,gc);
gc.anchor = GridBagConstraints.EAST;
c.add(l3,gc);
gc.anchor = GridBagConstraints.WEST;
c.add(l4,gc);
gc.anchor = GridBagConstraints.CENTER;
c.add(l5,gc);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GBAnchorExample e = new GBAnchorExample();
e.setSize(720,720);
e.setVisible(true);
}
}
要可视化锚点的实际效果,您可以将main
方法更改为
public static void main(String[] args){
GBAnchorExample e = new GBAnchorExample();
Component grid = new JComponent() {
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.GREEN);
int w = getWidth(), h = getHeight();
for(int i = 1; i < 5; i++) {
int x = (int)(w/5.0*i);
g.drawLine(x, 0, x, h);
}
}
};
e.setGlassPane(grid);
grid.setVisible(true);
e.setSize(720,720);
e.setVisible(true);
}
这将绘制一个绿色网格以显示包含标签的逻辑单元格,因此锚点如何影响标签在其单元格中的位置变得很明显。
推荐阅读
- javascript - JavaScript 中的 SonarQube 正则表达式漏洞问题
- html - 从动态创建的 div 元素中选择一个 div - Angular
- ios - 创建日历应用程序时如何管理我的数据?
- c - 那个条件是什么意思?
- java - BigInteger 抛出 numberFormatException
- snowflake-cloud-data-platform - 在 SnowFlake 中,我想在 sql server 中提供访问类型的 ddl_admin
- sql - 如果可能,插入临时表。通过动态查询
- javascript - 了解 JS 端的 Tokbox 视频分辨率
- android - Android Studio 3.6 总是选择第一个构建变体
- c# - 当 IXmlSerializable.ReadXml() 内部发生架构验证错误时,为什么 XmlSerializer 会引发异常并引发 ValidationEvent