BoxLayout在底部留下像素线

时间:2014-01-22 17:17:09

标签: java swing boxlayout

如附带的屏幕截图所示 - 黄线来自基础BoxLayout - 面向JPanel。更改为BorderLayout 会删除黄线:

Basic BorderLayout display, coloured for convenience

代码示例如下:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class FrameDemo {

    private String[] suits = {"hearts", "spades", "diamonds", "clubs", "joker"};

    public static void main(String[] args) {
        new FrameDemo();
    }

    public FrameDemo() {
        JFrame mainframe = new JFrame();
        mainframe.setPreferredSize(new Dimension(800, 600));
        mainframe.setLocationByPlatform(true);
        mainframe.setTitle("Playing Card Game! v0.1");

        mainframe.getContentPane().setLayout(new BorderLayout());

        JPanel top = new JPanel();
        top.setBackground(Color.BLUE);
        top.setPreferredSize(new Dimension(800, 50));
        JPanel left = new JPanel();
        left.setBackground(Color.RED);
        left.setPreferredSize(new Dimension(50, 500));
        JPanel centre = new JPanel();
        centre.setBackground(Color.YELLOW);
        centre.setLayout(new BoxLayout(centre, BoxLayout.Y_AXIS));
        JPanel right = new JPanel();
        right.setBackground(Color.GREEN);
        right.setPreferredSize(new Dimension(50, 500));
        JPanel bot = new JPanel();
        bot.setBackground(Color.GRAY);
        bot.setPreferredSize(new Dimension(800, 50));

        for(String suit : suits) {
            if(!(suit.equals("joker"))) {
                JPanel layer = new JPanel();
                layer.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
                layer.setBackground(Color.BLACK);
                centre.add(layer);
            }
        }

        mainframe.add(top, BorderLayout.NORTH);
        mainframe.add(left, BorderLayout.WEST);
        mainframe.add(centre, BorderLayout.CENTER);
        mainframe.add(right, BorderLayout.EAST);
        mainframe.add(bot, BorderLayout.SOUTH);

        mainframe.setDefaultCloseOperation(
                WindowConstants.DO_NOTHING_ON_CLOSE);

        mainframe.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }       
        });

        mainframe.pack();
        mainframe.setVisible(true);
    }

}

我已尝试在Color.BLACK JPanel上设置边框,但它似乎没有做太多。为什么Swing这样做?可以修复吗?

注意:无论我是否使用setPreferredSize(),都会显示。

1 个答案:

答案 0 :(得分:3)

  

为什么Swing这样做?

添加到中心面板的每个面板的首选高度为20. BoxLayout需要缩放 每个面板填充然后可用的整个空间。在完成此缩放时,看起来存在某种舍入问题。

  

可以修复吗?

不确定您的具体要求是什么?

如果您尝试将每个组件设置为相同的高度,则可以使用Relative Layout。它有一个属性,允许您指定如何舍入剩余像素。

编辑:

仔细查看您的代码,问题是:

//mainframe.setPreferredSize(new Dimension(800, 600));

永远不要设置框架的大小。这是布局管理器的工作。您手动设置的大小包括标题栏和边框,因此所有面板都比您希望的小,并且它们不能被10整除。

// mainframe.setPreferredSize(new Dimension(800,600));

相关问题