为什么 JLabel 在使用 SpringLayout 时高度为零?
Why JLabel has zero height when using SpringLayout?
我是 Java UI 设计的新手,我尝试使用 SpringLayout
来创建表格。
我把 JLabel
放在 JFrame
中,添加了一些约束。我需要延伸到 JPanel
宽度的 JLabel
。但它的高度为零。而且宽度太小。
这是代码片段:
imageContainer = new JLabel();
imageContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
filterButton = new JButton("Filter");
filterButton.addActionListener(filter);
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.EAST, imageContainer, 5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, filterButton, 5, SpringLayout.SOUTH, imageContainer);
layout.putConstraint(SpringLayout.SOUTH, filterButton, 5, SpringLayout.SOUTH, this);
这是 result
我哪里错了?如何将 JLabel
大小拉伸到父容器?
我的问题不在 JLablel 中。
我试图在约束中使用 this 但实际上这不是正确的方法。您需要这样获取容器:
Container contentPane = this.getContentPane();
之后就可以在constraints中使用contentPane了:
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, imageContainer, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, imageContainer, -5, SpringLayout.NORTH, filterButton);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, filterButton, -5, SpringLayout.SOUTH, contentPane);
还有这项工作。
我是 Java UI 设计的新手,我尝试使用 SpringLayout
来创建表格。
我把 JLabel
放在 JFrame
中,添加了一些约束。我需要延伸到 JPanel
宽度的 JLabel
。但它的高度为零。而且宽度太小。
这是代码片段:
imageContainer = new JLabel();
imageContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
filterButton = new JButton("Filter");
filterButton.addActionListener(filter);
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.EAST, imageContainer, 5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, filterButton, 5, SpringLayout.SOUTH, imageContainer);
layout.putConstraint(SpringLayout.SOUTH, filterButton, 5, SpringLayout.SOUTH, this);
这是 result
我哪里错了?如何将 JLabel
大小拉伸到父容器?
我的问题不在 JLablel 中。 我试图在约束中使用 this 但实际上这不是正确的方法。您需要这样获取容器:
Container contentPane = this.getContentPane();
之后就可以在constraints中使用contentPane了:
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, imageContainer, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, imageContainer, -5, SpringLayout.NORTH, filterButton);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, filterButton, -5, SpringLayout.SOUTH, contentPane);
还有这项工作。