【问题标题】:Cannot decrease vertical gap between JLabels in Grid Layout无法减少网格布局中 JLabel 之间的垂直间隙
【发布时间】:2020-04-01 08:44:21
【问题描述】:

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

另外,我如何增加 JLabels 和 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);

   }
}

【问题讨论】:

  • 考虑改用GridBaLayout
  • 一个可行的例子会让我们更容易看到问题。如果您不熟悉使用 Swing 进行布局,我还建议您尝试图形编辑器,例如用于 eclipse 或 Netbeans 设计器的免费“Window Builder”。使用可视化编辑器玩弄布局要容易得多。
  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。硬编码数据替换数据库。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。例如。 This answer 指向嵌入在 this question 中的图像的热链接。 3) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度 - ..
  • 要实现第 5 点,请在自定义绘制的 JPanel 中绘制背景图像。实施第 1-4 点以获得更多帮助。
  • 那我该如何解决这个问题? - 你在上一个问题中已经给出了这个建议:stackoverflow.com/a/60959643/131872。您是否点击了“背景面板”的链接来了解为什么它是更好的方法?

标签: java swing user-interface layout-manager grid-layout


【解决方案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

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 2021-02-22
    • 2017-10-03
    • 2016-09-26
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 2014-08-25
    • 2011-02-27
    相关资源
    最近更新 更多