我有一个带有 TitledBorder 的 JPanel,但面板的内容比边框中的标题窄,并且标题被截断。 我正在将 BoxLayout 用于 JPanel,如图所示 here注意手动设置宽度。我尝试根据 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);
}
最佳答案
如何计算所需的宽度:
JRadioButton jrb = new JRadioButton();
int width = (int) SwingUtilities2.getFontMetrics( jrb, jrb.getFont() ).getStringBounds( cat.getDescription(), null ).getWidth();
for (Case c : cases) {
JRadioButton jrbCase = new JRadioButton();
jrbCase.setText(c.getDescription());
jrbCase.setToolTipText(c.getText());
jrbCase.setPreferredSize( new Dimension( width, jrbCase.getPreferredSize().height ) );
bgCases.add(jrbCase);
jp.add(jrbCase);
}
关于java - JPanel TitledBorder 中的标题截断 - Java swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6407897/