JPanel TitledBorder 中的标题截断 - Java swing
我有一个带有 TitledBorder 的 JPanel,但面板的内容比边框中的标题窄,并且标题被截断。 我正在将 BoxLayout 用于 JPanel,如此处所示,请注意手动设置宽度。我尝试根据 TitledBorder getMinimumSize() 函数及其组件的宽度设置面板的最小、最大和首选宽度,但都不起作用。唯一有效的方法是使用盒子填充物,但这会产生不需要的压痕。
有什么方法可以显示完整的标题,无论其包含的内容如何?
this.jpCases.setLayout(new javax.swing.BoxLayout(this.jpCases, javax.swing.BoxLayout.LINE_AXIS));
List<Category> categories = db.getCategories();
for (Category cat : categories) {
JPanel jp = new JPanel();
TitledBorder tb = BorderFactory.createTitledBorder(cat.getDescription());
jp.setBorder(tb);
jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
jp.setAlignmentY(Component.TOP_ALIGNMENT);
jp.setAlignmentX(Component.LEFT_ALIGNMENT);
List<Case> cases = db.getCasesByCategoryId(cat.getId());
for (Case c : cases) {
JRadioButton jrbCase = new JRadioButton();
jrbCase.setText(c.getDescription());
jrbCase.setToolTipText(c.getText());
jrbCase.setMaximumSize(tb.getMinimumSize(jp));
bgCases.add(jrbCase);
jp.add(jrbCase);
}
//jp.add(new Box.Filler(tb.getMinimumSize(jp), tb.getMinimumSize(jp), tb.getMinimumSize(jp)));
this.jpCases.add(jp);
}
I've got a JPanel with a TitledBorder, but the contents of the panel are narrower than the title in the border and the title gets truncated.
I am using BoxLayout for the JPanel which as depicted here pays attention to manual setting of width. I tried to set the minimum, maximum and preferred width of the panel according to the TitledBorder getMinimumSize() function along with the width of its components but all don't work. The only thing that worked was using a box filler but that introduced an undesired indentation.
Any way to the show the full title irrespective to the content it contains?
this.jpCases.setLayout(new javax.swing.BoxLayout(this.jpCases, javax.swing.BoxLayout.LINE_AXIS));
List<Category> categories = db.getCategories();
for (Category cat : categories) {
JPanel jp = new JPanel();
TitledBorder tb = BorderFactory.createTitledBorder(cat.getDescription());
jp.setBorder(tb);
jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
jp.setAlignmentY(Component.TOP_ALIGNMENT);
jp.setAlignmentX(Component.LEFT_ALIGNMENT);
List<Case> cases = db.getCasesByCategoryId(cat.getId());
for (Case c : cases) {
JRadioButton jrbCase = new JRadioButton();
jrbCase.setText(c.getDescription());
jrbCase.setToolTipText(c.getText());
jrbCase.setMaximumSize(tb.getMinimumSize(jp));
bgCases.add(jrbCase);
jp.add(jrbCase);
}
//jp.add(new Box.Filler(tb.getMinimumSize(jp), tb.getMinimumSize(jp), tb.getMinimumSize(jp)));
this.jpCases.add(jp);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
计算所需的宽度怎么样:
What about calculating the needed width:
这对我来说效果很好:
This Works well for me:
我通过在面板的顶部(或底部)添加一个标签(BoxLayout.Y_AXIS)来“解决”它。标签仅包含导致标题宽度相同(或更大)的空格。 (使具有与标题完全相同的文本的标签不可见并不能解决问题。)
I "solved" it by adding a label (BoxLayout.Y_AXIS) to the top (or bottom) of the panel. The label contains only spaces leading to the same (or greater) width of the title. (Making the label with the very same text as the title invisible does not solve it.)