Java SWT 删除父级间距

Java SWT remove spacing of parent

有人知道如何删除父内容和父内容之间的间距吗?

例如:

GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;

shell.setText("Layout");
shell.setMaximized(true);
shell.setImage(imghandler.getCornerimg());
shell.setLayout(layout);
      
Composite mainComp = new Composite(shell, SWT.NONE);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
layout.numColumns = 2;
mainComp.setLayout(layout);
mainComp.setLayoutData(data);
mainComp.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

Composite subComp1 = new Composite(mainComp, SWT.NONE);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
layout.numColumns = 1;
subComp1.setLayout(layout);
subComp1.setLayoutData(data);
subComp1.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

Composite subComp2 = new Composite(mainComp, SWT.NONE);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
subComp2.setLayout(layout);
subComp2.setLayoutData(data);
subComp2.setBackground(display.getSystemColor(SWT.COLOR_GREEN));

Composite subComp3 = new Composite(mainComp, SWT.NONE);
data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
layout.numColumns = 2;
subComp3.setLayout(layout);
subComp3.setLayoutData(data);
subComp3.setBackground(display.getSystemColor(SWT.COLOR_MAGENTA));

shell.pack();
shell.open();

我想删除 shell 和 mainComp 之间以及 mainComp 和 subComps 之间的间距。有什么想法吗?

我建议为此使用 Netbeans 表单设计器。我已经使用 Netbeans 做了一个例子,你会注意到父面板有一个黑色背景并且根本不可见

public class GridSpacing extends javax.swing.JPanel {


public GridSpacing() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    pnlParent = new javax.swing.JPanel();
    pnlTurquoise = new javax.swing.JPanel();
    pnlPink = new javax.swing.JPanel();
    pnlGreen = new javax.swing.JPanel();

    setLayout(new java.awt.GridBagLayout());

    pnlParent.setBackground(new java.awt.Color(0, 0, 0));
    pnlParent.setLayout(new java.awt.GridBagLayout());

    pnlTurquoise.setBackground(new java.awt.Color(0, 255, 204));

    javax.swing.GroupLayout pnlTurquoiseLayout = new javax.swing.GroupLayout(pnlTurquoise);
    pnlTurquoise.setLayout(pnlTurquoiseLayout);
    pnlTurquoiseLayout.setHorizontalGroup(
        pnlTurquoiseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 200, Short.MAX_VALUE)
    );
    pnlTurquoiseLayout.setVerticalGroup(
        pnlTurquoiseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    pnlParent.add(pnlTurquoise, gridBagConstraints);

    pnlPink.setBackground(new java.awt.Color(255, 0, 255));

    javax.swing.GroupLayout pnlPinkLayout = new javax.swing.GroupLayout(pnlPink);
    pnlPink.setLayout(pnlPinkLayout);
    pnlPinkLayout.setHorizontalGroup(
        pnlPinkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    pnlPinkLayout.setVerticalGroup(
        pnlPinkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    pnlParent.add(pnlPink, gridBagConstraints);

    pnlGreen.setBackground(new java.awt.Color(51, 255, 51));

    javax.swing.GroupLayout pnlGreenLayout = new javax.swing.GroupLayout(pnlGreen);
    pnlGreen.setLayout(pnlGreenLayout);
    pnlGreenLayout.setHorizontalGroup(
        pnlGreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 200, Short.MAX_VALUE)
    );
    pnlGreenLayout.setVerticalGroup(
        pnlGreenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    pnlParent.add(pnlGreen, gridBagConstraints);

    add(pnlParent, new java.awt.GridBagConstraints());
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JPanel pnlGreen;
private javax.swing.JPanel pnlParent;
private javax.swing.JPanel pnlPink;
private javax.swing.JPanel pnlTurquoise;
// End of variables declaration                   

}

GridLayout:

中设置边距的高度和宽度
layout.marginWidth = 0;
layout.marginHeight = 0;

注意:您还应该为每个 Composite

使用一个新的 GridLayout