Java-JFrame底部的额外像素(GridBagLayout) [英] Java - Extra pixels at the bottom of JFrame (GridBagLayout)

查看:83
本文介绍了Java-JFrame底部的额外像素(GridBagLayout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用GridBagLayout时遇到问题,一切似乎都按预期工作,但是JFrame的底部有一个小的缝隙,我似乎无法摆脱.

I'm having an issue when using GridBagLayout, everything seems to be working as intended but there's a small gap at the bottom of the JFrame that I can't seem to get rid of.

这是我正在运行的代码...

This is the code I'm running...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

 private JFrame frame;
 private JPanel container, header, content, footer;

 public Test(){
     frame = new JFrame();
     frame.setSize(600, 400);
     frame.setLocationRelativeTo(null);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     GridBagConstraints gbc = new GridBagConstraints();

     container = new JPanel();
     container.setBackground(Color.blue);
     container.setLayout(new GridBagLayout());

     header = new JPanel();
     header.setBackground(Color.red);
     header.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
     header.add(new JButton("Test"));
     gbc.gridx = 0;
     gbc.gridy = 0;
     gbc.weightx = 1.0;
     gbc.fill = GridBagConstraints.BOTH;
     container.add(header, gbc);

     content = new JPanel();
     content.setBackground(Color.green);
     gbc.gridx = 0;
     gbc.gridy = 1;
     gbc.weightx = 1.0;
     gbc.weighty = 0.9;
     gbc.fill = GridBagConstraints.BOTH;
     container.add(content, gbc);

     footer = new JPanel();
     footer.setBackground(Color.gray);
     gbc.gridx = 0;
     gbc.gridy = 2;
     gbc.weightx = 1.0;
     gbc.weighty = 0.1;
     gbc.fill = GridBagConstraints.BOTH;
     container.add(footer, gbc);

     frame.getContentPane().add(container);
     frame.setMinimumSize(new Dimension(600,400));
 }


 public static void main(String[] args) {
     Test main = new Test();
     main.frame.pack();
     main.frame.setVisible(true);

 }
}

您会注意到,UI的底部仍显示约2-3个像素,容器的背景当然是蓝色.即使在调整大小时,这种差距仍然存在.我猜它与GridBagConstraint权重有关,因为当我尝试将weightY设置为相等的值时,问题就可以自行解决,但这显然不是我想要的样子.

You'll notice there's about 2-3 pixels still showing at the bottom of UI, the background for the container is of course blue. This gap persists even when resizing. I'm guessing it has something to do with the GridBagConstraint weights as when I tried setting the weightY to equal values the problem resolves itself, but that obviously isn't what I want it to look like.

推荐答案

看看相对布局.

布局具有一个属性,可让您控制由于舍入问题而如何分配额外的像素.

The layout has a property that allows you to control how to allocating extra pixels due to rounding problems.

基本代码可以是:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
rl.setRoundingPolicy(...); // or use the default
//container.setLayout(new GridBagLayout());
container.setLayout( rl );
...
//container.add(header, gbc);
container.add(header); // display at preferred size
...
//container.add(content, gbc);
container.add(content, new Float(0.9f));
...
//container.add(footer, gbc);
container.add(footer, new Float(0.1f));

与GridBagLayout相比,这将为您提供更好的90/10关系. GridBagLayout首先分配首选空间,然后在多余的空间上分配90/10,因为您没有真正的90/10关系.

This will give you a better 90/10 relationship than a GridBagLayout. A GridBagLayout allocates the preferred space first and then does the 90/10 on the extra space, to you don't have a true 90/10 relationship.

这篇关于Java-JFrame底部的额外像素(GridBagLayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆