如何向带有边框的JPanel添加填充

框架堆栈:

我想给一些添加填充JPanel我找到了这个答案:https : //stackoverflow.com/a/5328475/1590323

对于没有边框的面板,它工作正常。但是,对于已经有边框的面板,我该如何做呢?TitledBorder在这种情况下为A

我试过了:

JPanel mypanel = new MyPanel(); // Panel that I am going to add a TitledBorder to, but needs padding
mypanel.setBorder(new EmptyBorder(10,10,10,10));
JPanel mypanel_container = new JPanel();
TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "My panel");
border.setTitleJustification(TitledBorder.LEADING);
mypanel_container.setBorder(border);
mypanel_container.add(mypanel);
this.add(mypanel_container);

(简而言之:在EmptyBorder应有的面板中添加一个TitledBorder,然后用制作另一个面板,并在TitledBorder其中添加第一个面板,然后使用该面板)

但是后来我得到了太大的填充,忽略了的构造函数值EmptyBorder

那么,如何为带有图形边框的JPanel添加填充?

NiziL:

你可以看看CompoundBorder

一个复合Border类,用于通过将一个内部Border对象嵌套在一个外部Border对象的插入物中来将两个Border对象组合成一个边框。例如,此类可以用于向具有现有装饰边框的组件添加空白边距空间:

Border border = comp.getBorder();
Border margin = new EmptyBorder(10,10,10,10);
comp.setBorder(new CompoundBorder(border, margin));

当然,您也可以使用BorderFactory#createCompoundBorder(border, margin)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章