如何为 window 应用程序设置 setBorder?

How to setting setBorder for window application?

我尝试创建一些用于监控网络的应用程序。我要创建 4 JPanels 并插入,例如,面板 insert_panel_0insert_panel_2panel_2。我无法在 insert_panel_0 之间设置边框, insert_panel_2。我有垂直间隙,但没有水平间隙。

你能帮我解决一下吗?

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);
    }

}

GridLayout 有设置水平和垂直间隙的方法:setHgap(hgap)setVGap(vgap)。或者您可以在构造函数中创建布局时提供它们:new GridLayout(rows, cols, hgap, vgap)

希望对您有所帮助。

您可以使用 GridBagLayout 并利用 Insets。然而这带来了一个新问题:

在计算 insert_panel_0insert_panel_1 的维度时,您需要考虑 插图的大小,因为如果总大小 (insert_panel_0_size + insert_panel_1_size + Insets) 大于parent (panel_2_size), insert_panel_0 and insert_panel_1 不会大小正确。

构造函数和描述:(来自java-docs):

Insets(int top, int left, int bottom, int right) Creates and initializes a new Insets object with the specified top, left, bottom, and right insets.

与您的问题无关,但在 Java GUI 中,您应该始终从事件调度线程 调用您的 GUI。当您开始在 GUI 中获得更多 complex/dynamically 更新值时,这将帮助您避免不必要的行为。

您修改后的代码(您将不得不使用您的 Dimension 公式,我将其注释掉是因为:懒惰):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }

    public static void createGUI() {

        //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 GridBagLayout());
        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);

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(0,5,0,5); // Horizontal padding only

        JPanel insert_panel_0 = new JPanel();
        //insert_panel_0.setPreferredSize( new Dimension((width/4), (int) Math.round( hight*0.05)));
        insert_panel_0.setPreferredSize(new Dimension(200, 100));
        insert_panel_0.setBackground(Color.black);

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel_2.add(insert_panel_0, gbc);

        JPanel insert_panel_1 = new JPanel();
        //insert_panel_1.setPreferredSize( new Dimension(width/4 - 10, (int) Math.round( hight*0.05)));
        insert_panel_1.setPreferredSize(new Dimension(200, 100));
        insert_panel_1.setBackground(Color.black);

        gbc.gridx = 1;
        gbc.gridy = 0;
        panel_2.add(insert_panel_1, gbc);    

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

问题出在你的宽度计算上。当您除以 4 时,每个黑色子面板的宽度就是整个父面板的宽度。这会导致两个问题:

  1. 子面板无法放入具有 20 像素边框的父面板,所以它被绘制在面板边缘
  2. FlowLayout 随后会自动将第二个面板换到下一行。所以面板显示在两行上。

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

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

现在这两个面板将出现在同一行。

如果你想要两个黑色面板之间的间隙,那么你还需要:

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