How to setting setBorder for window application?

北慕城南 提交于 2020-03-03 10:07:25

问题


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.

Could you help me solve it?

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

}

回答1:


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

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. The child panel can't fit into the parent panel with the 20 pixel border, so it is painted at the panels edge
  2. The FlowLayout will then automatically wrap the second panel to the next row. So the panels are displayed on two rows.

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



回答2:


GridLayout has methods for setting horizontal and vertical gaps: setHgap(hgap) and setVGap(vgap). Or you can provide them as you create the layout in a constructor: new GridLayout(rows, cols, hgap, vgap)

hope it helps.




回答3:


You can use GridBagLayout and utilize Insets. However this brings about a new problem:

You need to take into account the size of the insets when calculating the Dimension for insert_panel_0 and insert_panel_1, because if the total size (insert_panel_0_size + insert_panel_1_size + Insets) is greater than the parent (panel_2_size), insert_panel_0 and insert_panel_1 will not size properly.

Constructor and description: (from 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.

Unrelated to your question, but in Java GUI you should always call your GUI from the event-dispatching thread. This will help you avoid unwanted behavior when you start getting more complex/dynamically updated values in your GUI.

Your modified code (you'll have to play around with your Dimension formula, I commented it out because: lazy):

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


来源:https://stackoverflow.com/questions/59500171/how-to-setting-setborder-for-window-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!