我正在建立一个更大的程序中的面板;下面的程序仍然说明了我的问题,但它看起来比绝对要复杂一些,因为有些地方我以后会添加一些东西。
package sandbox;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SpacingPlay extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
SpacingPlay sp = new SpacingPlay();
sp.setVisible(true);
}
public SpacingPlay()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new DragNDropPanel();
add(panel);
pack();
}
class DragNDropPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public DragNDropPanel()
{
JPanel currentImagePanel = getCurrentImagePanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(currentImagePanel);
// other things will go here; I cut them out to make this simpler.
setLayout(new BorderLayout());
// put things in a containing panel so that they aren't stretched being in the WEST part of a borderlayout.
JPanel leftContainingPanel = new JPanel();
leftContainingPanel.add(leftPanel);
add(leftContainingPanel, BorderLayout.WEST);
}
private Component createStandardSpace()
{
return Box.createRigidArea(new Dimension(0, 15));
}
private JPanel getCurrentImagePanel()
{
JPanel currentImagePanel = new JPanel();
currentImagePanel.setLayout(new BoxLayout(currentImagePanel, BoxLayout.PAGE_AXIS));
JLabel currentImageLabel = new JLabel("none");
currentImageLabel.setBorder(BorderFactory.createDashedBorder(Color.BLUE));
currentImageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setSize(defaultLabelSize);
JButton clearButton = new JButton("Clear");
clearButton.setAlignmentX(Component.CENTER_ALIGNMENT);
clearButton.setBorder(BorderFactory.createLineBorder(Color.GREEN));
currentImagePanel.add(currentImageLabel);
currentImagePanel.add(createStandardSpace());
currentImagePanel.add(clearButton);
currentImagePanel.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
return currentImagePanel;
}
}
}我希望currentImageLabel是一个标准大小;我打算让它在程序中得到不同的图像,并希望它在不改变大小的情况下得到这些图像。我的想法是设置一个大小和首选的大小,然后缩放我放在那里的图像到那个大小。
然而,defaultLabelSize并没有我想的那样的效果。标签进入boxLayout面板;它被添加,然后是一个刚性的空间,然后是一个按钮。我期望标签是默认大小,而不是缩小到最小允许的大小。为了更好地理解正在发生的事情,我已经设置了彩色边框;看起来,对于整个boxLayout面板来说,首选的大小是很大的,而不是在标签下面放置按钮。编辑:换句话说,当标签被迫变大时,我希望标签下面的按钮被放置在标签下面。但我贴在标签上的尺寸似乎不起作用。
要修复currentImageLabel的大小,我需要做什么?
发布于 2020-11-06 15:53:25
不是100%为什么标签只与文本大小,而不是(硬编码)首选大小。我无法使用面板和布局管理器的其他组合复制这种行为。
您正在使用pack,所以所有组件都应该按照它们的首选大小进行调整。
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setSize(defaultLabelSize);几点意见:
BoxLayout的情况下,确实尝试使用首选的大小,但将尊重最小和最大的大小(取决于父面板中可用的空间)。,我需要做什么来修复currentImageLabel的大小?
因此,要实现为JLabel设置固定首选大小的目标,可以使用:
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setMinimumSize(defaultLabelSize);
currentImageLabel.setMaximumSize(defaultLabelSize);
//currentImageLabel.setSize(defaultLabelSize);编辑:
在寻找为什么这似乎行不通
要进一步澄清,请将原始代码更改为:
currentImageLabel.setPreferredSize(defaultLabelSize);
System.out.println(currentImageLabel.getPreferredSize());
System.out.println(currentImageLabel.getMinimumSize());
System.out.println(currentImageLabel.getMaximumSize());您将看到标签的最小/最大尺寸不受影响。
从上面的第2点,您将看到BoxLayout尊重最大的大小。
因此,也可以重写最大大小,从而允许标签以“is首选大小”显示。
但是,当您计算"currentImagePanel“的首选大小时,标签的(硬编码)首选大小将用于面板的首选尺寸计算,因此面板将以首选大小显示。
又一个音符。不需要"leftContainingPanel“。您只需将"leftPanel“添加到BorderLayout.WEST中,因为BorderLayout将尊重您添加的组件的宽度。
https://stackoverflow.com/questions/64716081
复制相似问题