且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为窗口应用程序设置setBorder?

更新时间:2022-06-24 14:56:00

insert_panel_0.setPreferredSize( new Dimension(width/4, (int) Math.round( hight*0.05)));

问题是您的宽度计算.当您将其除以4时,每个黑色子面板的宽度就是整个父面板的宽度.这会导致两个问题:

The problem is your width calculation. When you divide by 4 the width of each black child panel is the width of the entire parent panel. This causes two problems:

  1. 子面板无法与20像素边框的父面板配合,因此它会在面板边缘绘制
  2. 然后,FlowLayout将自动将第二个面板包装到下一行.因此面板显示在两行上.

解决方案是改善宽度计算,例如:

The solution is to improve your width calculation for example:

insert_panel_0.setPreferredSize( new Dimension(width/12, (int) Math.round( hight*0.05)));

现在,两个面板将出现在同一行中.

Now the two panels will appear on the same row.

如果要在两个黑色面板之间留出空隙,则还需要:

If you want a gap between the two black panels then you also need:

//panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));