如何为窗口应用程序设置setBorder? [英] How to setting setBorder for window application?
问题描述
我尝试创建一些用于监视网络的应用程序.我将创建4个JPanels
并插入例如insert_panel_0
,insert_panel_2
到panel_2
的面板.我无法在insert_panel_0
,insert_panel_2
之间设置边框.我有垂直缝隙,但没有水平缝隙.
I tried create some application for monitoring network. I'm going to create 4 JPanels
and insert, for example, panels insert_panel_0
, insert_panel_2
to panel_2
.I can't setting my borders between insert_panel_0
, insert_panel_2
. I have vertical gaps, but i don't have horizontal gaps.
您能帮我解决吗?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class setting {
public static void main(String[] args) {
//get size window
Dimension tk = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) tk.width;
int hight = (int) tk.height;
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1,4, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// set panels for window
JPanel panel_0 = new JPanel();
panel_0.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel_0.setBorder(new EmptyBorder(20, 20,20, 20));
panel_0.setBackground(Color.white);
frame.add(panel_0);
JPanel panel_1 = new JPanel();
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel_1.setBackground(Color.yellow);
panel_1.setBorder(new EmptyBorder(20, 20,20, 20));
frame.add(panel_1);
JPanel panel_2 = new JPanel();
panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel_2.setBorder(new EmptyBorder(20, 20,20, 20));
panel_2.setBackground(Color.green);
frame.add(panel_2);
JPanel panel_3 = new JPanel();
panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel_3.setBorder(new EmptyBorder(20, 20,20, 20));
panel_3.setBackground(Color.blue);
frame.add(panel_3);
JPanel insert_panel_0 = new JPanel();
insert_panel_0.setPreferredSize( new Dimension(width/4, (int) Math.round( hight*0.05)));
insert_panel_0.setBackground(Color.black);
panel_2.add(insert_panel_0);
JPanel insert_panel_1 = new JPanel();
insert_panel_1.setPreferredSize( new Dimension(width/4, (int) Math.round( hight*0.05)));
insert_panel_1.setBackground(Color.black);
panel_2.add(insert_panel_1);
frame.setVisible(true);
}
}
推荐答案
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:
- 子面板无法与20像素边框的父面板配合,因此它会在面板边缘绘制
- 然后,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));
这篇关于如何为窗口应用程序设置setBorder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!