无法减小网格布局中JLabel之间的垂直间隙

问题描述 投票:0回答:1

我正在为我的客户创建一个配置文件界面。但是,当我在“网格布局”中指定像素数时,它似乎不起作用。我试图降低JLabel的高度,但这也没有任何效果。

[另外,我如何增加JLabel和JPanel之间的填充。谢谢

更新:我尝试将背景图像添加到JPanel而不是添加到JLabel。现在的问题是,宽度和高度没有生效。它只是显示了我背景图片的一小部分

 public CustomerProfile()
 {
    super("Customer Profile");
    setLayout(new BorderLayout());




    //BACKGROUND IMAGE OF THE PROFILE JPANEL

     ImageIcon backgroundImg= new ImageIcon("background.jpg");

    //background = new JLabel("",background_img,JLabel.CENTER);
    //background.setLayout(new BorderLayout());


    //PROFILE DETAILS JPANEL
    jpProfile = new JPanel() {

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(backgroundImg.getImage(),0,0,450,775,null);
        }


    };


    add(jpProfile, BorderLayout.WEST);
    //jpProfile.add(background);

    /*//PROFILE ICON
    ImageIcon profImg = new ImageIcon("female.png");
    jlProfileIcon = new JLabel("",profImg,JLabel.CENTER);

    //ADDING PROFILE ICON TO JLABEL background
    background.add(jlProfileIcon,BorderLayout.NORTH);

    //DEFINING FONT
    standardFont = new Font("Serif",Font.BOLD,20);

    //JLABELS FOR PROFILE INFO
    jlCusName = new JLabel("Name: ");
    jlCusName.setFont(standardFont);
    jlCusName.setForeground(Color.WHITE);

    jlCusAddr = new JLabel("Address: ");
    jlCusAddr.setFont(standardFont);
    jlCusAddr.setForeground(Color.WHITE);   

    jlCusEmail = new JLabel("Email: ");
    jlCusEmail.setFont(standardFont);
    jlCusEmail.setForeground(Color.WHITE);

    jlCusPhone = new JLabel("Phone: ");
    jlCusPhone.setFont(standardFont);
    jlCusPhone.setForeground(Color.WHITE);

    jlCusProblem = new JLabel("Problems: ");
    jlCusProblem.setFont(standardFont);
    jlCusProblem.setForeground(Color.WHITE);


    //JPANEL FOR PROFILE INFO
    jpProfileInfo = new JPanel();
    jpProfileInfo.setLayout(new GridLayout(5,1,0,0));

    jpProfileInfo.setOpaque(false);

    //ADDING JLABELS TO JPANEL
    jpProfileInfo.add(jlCusName);
    jpProfileInfo.add(jlCusAddr);
    jpProfileInfo.add(jlCusEmail);
    jpProfileInfo.add(jlCusPhone);
    jpProfileInfo.add(jlCusProblem);


    //ADDING JPANEL TO background JLABEL
    background.add(jpProfileInfo,BorderLayout.WEST);
*/
}


public static void main(String[] args)
{
    CustomerProfile cp = new CustomerProfile();
    cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cp.setSize(900,775);
    cp.setVisible(true);

   }
}
java swing user-interface layout-manager grid-layout
1个回答
1
投票

但是,当我在“网格布局”中指定像素数时,它似乎不起作用。

正确。 GridLayout将展开/收缩以填充可用空间。

因此您可以使用包装面板:

JPanel labelPanel = news JPanel( new GridLayout(…) );
labelPanel.add(…);

JPanel wrapperPanel = new JPanel( new BorderLayout() );
wrapperPanel.add(labelPanel, BorderLayout.PAGE_START); // now the height is respected

background.add(wrapperPanel, BorderLayout.LINE_START); // now the width is respected
© www.soinside.com 2019 - 2024. All rights reserved.