1

请看下面的代码

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

public class GridLayoutTest2
{
    private final JDialog msgDisplayer;
    public GridLayoutTest2()
    {
        JLabel maleLabel = new JLabel("Male",JLabel.CENTER);
            maleLabel.setBorder(BorderFactory.createEmptyBorder());
            JLabel femaleLabel = new JLabel("Female",JLabel.CENTER);
            femaleLabel.setBorder(BorderFactory.createEmptyBorder());

            JLabel fmaleIcon = new JLabel();
            fmaleIcon.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-Image.gif")));
            fmaleIcon.setBorder(BorderFactory.createEmptyBorder());


            JLabel maleIcon = new JLabel();
            maleIcon.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-Image.gif")));
            maleIcon.setBorder(BorderFactory.createEmptyBorder());

            msgDisplayer = new JDialog();
            msgDisplayer.setLayout(new GridLayout(4,1,1,1));
            msgDisplayer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            msgDisplayer.setTitle("Body Fat Percentage Classification");


            msgDisplayer.add(femaleLabel);
            msgDisplayer.add(fmaleIcon);
            msgDisplayer.add(maleLabel);
            msgDisplayer.add(maleIcon);

            msgDisplayer.pack();
            msgDisplayer.setVisible(true);
            msgDisplayer.setLocationRelativeTo(null);
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new GridLayoutTest2();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

此代码包含标签和附加的图像屏幕截图之间的巨大差距(空间)。我不想在标签和图像之间有这个空间。我怎样才能消除它?我知道我可以GridBagLayout去做,但是,有什么办法GridLayout吗?请帮忙!

在此处输入图像描述

4

3 回答 3

3

GridLayout allocates an equals amount of space for all components based on the largest component in the container. If you don't wish to use a complex layout manager such as GridBagLayout, you could use BoxLayout, which uses the component's preferred sizes. A BoxLayout with Y_AXIS alignment would be suitable here.

Example

于 2013-01-18T15:27:16.323 回答
2

Thats not the gap space, its real size of JLabel's (maleLabel, fmaleLabel). Size of image determines size of parent JLabel and in GridLayout, all components will take size of largest component. Gap between components is 1 as you defined when setting layout. So solution of your problem lays in finding suitable layout manager.

Reimeus gave you an example of GridBagLayout and BoxLayout, and I would like to recommend you MiGLayout which is quite easy to use.

于 2013-01-18T15:27:18.523 回答
1

Layout重要的是!!在这里,我用我的 GUI builder 做了一个简短的 EG,以显示空白的调整(或 的大小JLabel):

在此处输入图像描述

更多空白:

在此处输入图像描述

代码:

public class udyfash extends javax.swing.JFrame {


public udyfash() {
    initComponents();
}


@SuppressWarnings("unchecked")

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images.jpg"))); // NOI18N
    jLabel1.setText("yooo!!");
    jLabel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(44, 44, 44)
            .addComponent(jLabel1)
            .addContainerGap(39, Short.MAX_VALUE))
    );

    pack();
 }

 public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new udyfash().setVisible(true);
        }
    });
 }


 private javax.swing.JLabel jLabel1;
 }

我们说 GridBag 很复杂,但是使用您可以的布局管理器。

于 2013-01-18T15:38:16.340 回答