我正在尝试使用 Java Swing 为小型电话簿应用程序编写布局。我遇到了无法解决的尺寸问题。我希望过滤器(显示在搜索结果下)足够宽,以便在搜索短语很短时显示整个标题。它看起来像这样:
如您所见,名称已缩短。姓氏带有更长的文本,并且按照我想要的方式显示。
过滤器类别:
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
public class Filter
{
private JLabel label;
private JPanel filterPane;
Filter(String name)
{
// Filter text
label = new JLabel();
label.setForeground(new Color(0xAA0000));
// Closing button
JButton closeButton = new JButton("X");
closeButton.setFont(new Font("Dialog", Font.BOLD, 20));
closeButton.setMargin(new Insets(0, 0, 0, 0));
filterPane = new JPanel();
filterPane.setLayout(new BoxLayout(filterPane, BoxLayout.X_AXIS));
// Frame with title + 50px padding on the right side for the next filter
filterPane.setBorder(new CompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 50),
BorderFactory.createTitledBorder(name)));
filterPane.add(closeButton);
filterPane.add(Box.createRigidArea(new Dimension(5, 0)));
filterPane.add(label);
filterPane.setVisible(false);
}
public void setValue(String value) {
if (value == null || value.isEmpty())
filterPane.setVisible(false);
else
{
this.label.setText(value);
filterPane.setVisible(true);
}
}
public JPanel getFilterPane() {
return filterPane;
}
}
主类:
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
public class TestWindow
{
private static void init()
{
JFrame frame = new JFrame("Stackoverflow test window");
frame.setSize(new Dimension(480, 320));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// Main panel - 20 px padding from each side
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Create panel for filters
JPanel filterPane = new JPanel();
filterPane.setLayout(new BoxLayout(filterPane, BoxLayout.X_AXIS));
filterPane.setPreferredSize(new Dimension(0, 60));
// Add sample filters
Filter filter = new Filter("name");
filter.setValue("i");
filter.getFilterPane().setAlignmentY(Component.BOTTOM_ALIGNMENT);
filterPane.add(filter.getFilterPane());
filter = new Filter("surname");
filter.setValue("loooooong text");
filter.getFilterPane().setAlignmentY(Component.BOTTOM_ALIGNMENT);
filterPane.add(filter.getFilterPane());
// Add everything to main panel
pane.add(new JTextArea("Nothing fancy here, just to fill out space"), BorderLayout.CENTER);
pane.add(filterPane, BorderLayout.SOUTH);
frame.setContentPane(pane);
frame.setVisible(true);
}
public static void main (String [] args) throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeLater(TestWindow::init);
}
}
我在 Filter 构造函数中为 label
和 filterPane
尝试了 setMinimumSize
和 setPreferredSize
,但没有成功帮助。你能帮我一下吗?
最佳答案
这个问题实际上是由两个问题组成的。
- 使用
TitledBorder
时,组件在计算其首选大小时不会考虑标题的大小。 - 使用
BoxLayout
时,首选宽度将被忽略,除非您在组件后面添加“水平粘合”。
因此,要解决第一个问题,您需要重写 JPanel 的 getPreferredSize()
方法。但要实现这一点,您还需要通过添加胶水来绕过第二个问题。
要覆盖 getPreferredSize()
,您可以使用类似以下内容:
class MinSizePanel extends JPanel {
public Dimension getPreferredSize() {
Dimension original = super.getPreferredSize();
TitledBorder titleBorder = (TitledBorder)((CompoundBorder)getBorder()).getInsideBorder();
int width = (int)Math.max( original.getWidth(), 60 + (int)titleBorder.getMinimumSize(this).getWidth());
return new Dimension( width, (int)original.getHeight() );
}
}
这从CompoundBorder
获取TitledBorder
,获取其最小尺寸(这是一种考虑标题宽度的方法),为空边框添加一些额外的内容和插图(您可以添加适当的计算,我做了一个快捷方式,只使用了 60
...),并使用它(如果它大于组件的宽度)。
而不是
filterPane = new JPanel();
使用
filterPane = new MinSizePanel();
(您确实应该将面板的所有其余构建也移到该类中)。
在 TestWindow
类中,在最后一个之后
filterPane.add(filter.getFilterPane());
同时添加
filterPane.add(Box.createHorizontalGlue());
关于java - 强制最小 JPanel 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359859/