我想要给一些 JPanel
添加填充。我找到了这个答案:https://dev59.com/u2435IYBdhLWcg3wlRIf#5328475
对于没有边框的面板,它运行良好。但是如果面板已经有了边框,例如 TitledBorder
,我该如何添加填充呢?
我尝试了以下方法:
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);
简而言之:为应该具有TitledBorder
的面板添加EmptyBorder
,然后创建另一个带有TitledBorder
的面板,并将第一个面板添加到其中,最后使用那个面板。
但是,我得到了太大的填充,忽略了EmptyBorder
的构造器值。
那么我如何为带有图形边框的JPanel添加填充?