0
我是Java UI设计的新手,我尝试使用SpringLayout
来创建表单。 我把JLabel
放在JFrame
,加了一些限制。我需要JLabel
打到JPanel
宽度。但它没有高度。宽度太小。为什么JLabel在使用SpringLayout时高度为零?
这里的代码片段:
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
大小拉伸到父容器?
*“这里是代码片段:”*为了更好地提供帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。顺便说一句 - 直到标签包含图标和/或文本,其首选大小将为0x0。 –
我想稍后将图像放入JLabel。但是如果我想划分(缩放)图像,我需要知道JLabel大小。 –
MCVE在哪里?顺便说一句 - 标签会根据内容(例如图像)返回首选尺寸。可能更好的方法是通过在'JPanel'中自定义绘制图像来重写getPreferredSize()方法来产生逻辑最小值。然后1)面板将被布局分配空间。 2)自定义绘画可以根据面板的尺寸来绘制图像。 –