开发者

Get rid of the gap between JPanels

Here is my problem

Get rid of the gap between JPanels

How can I get rid of the gap from the inner rectangl开发者_C百科e box (it is actually the border of the middle JPanel)?

The outter rectangle is a extends JCompoment. Inside it contains three JPanels. Each of them use GridLayout. I even try to setHgap to a negative value in the big rectangle, but it doesn't change anything.

Edit: Sorry the question is not clear. I do want the border, but I don't want the gap between the inner border and outer border. If there is no gap in between, the whole rectangle will represent a class diagram class.


You may mean this way:

Get rid of the gap between JPanels

import java.awt.*;
import javax.swing.*;

public class GridBadFrame {

    private JFrame frame;
    private JPanel pnlCenter;
    private JPanel pnl1;
    private JPanel pnl2;
    private JPanel pnl3;

    public GridBadFrame() {
        pnl1 = new JPanel();
        pnl1.setBackground(Color.red);
        pnl2 = new JPanel();
        pnl2.setBackground(Color.blue);
        pnl3 = new JPanel();
        pnl3.setBackground(Color.red);
        JLabel lblWest = new JLabel();
        lblWest.setPreferredSize(new Dimension(50, 150));
        JLabel lblEast = new JLabel();
        lblEast.setPreferredSize(new Dimension(50, 150));
        JLabel lblNorth = new JLabel();
        lblNorth.setPreferredSize(new Dimension(600, 50));
        JLabel lblSouth = new JLabel();
        lblSouth.setPreferredSize(new Dimension(600, 50));
        pnlCenter = new JPanel();
        pnlCenter.setBackground(Color.black);
        pnlCenter.setLayout(new java.awt.GridLayout(3, 0, 10, 10));
        pnlCenter.setPreferredSize(new Dimension(600, 400));
        pnlCenter.add(pnl1);
        pnlCenter.add(pnl2);
        pnlCenter.add(pnl3);
        frame = new JFrame();
        frame.add(pnlCenter, BorderLayout.CENTER);
        frame.add(lblNorth, BorderLayout.NORTH);
        frame.add(lblSouth, BorderLayout.SOUTH);
        frame.add(lblWest, BorderLayout.WEST);
        frame.add(lblEast, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                GridBadFrame gridBadFrame = new GridBadFrame();
            }
        });
    }
}


it is actually the border of the middle JPanel

If it is the border, remove that (setBorder(null)).


I think if you try setBorderPainted(false) it may get rid of the unwanted border.


The issue is probably to do with the preferred height and width set by the GridLayout on the JPanel.

Excerpt from the documentation:

The preferred width of a grid layout is the largest preferred width of any of the components in the container times the number of columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.

The preferred height of a grid layout is the largest preferred height of any of the components in the container times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜