Question

As seen in the attached screenshot - the yellow line is from the underlying BoxLayout-oriented JPanel. Changing to BorderLayout removes the yellow line:

Basic BorderLayout display, coloured for convenience

Code example follows:

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);
    }

}

I've tried setting borders on the Color.BLACK JPanels, but it doesn't seem to do much. Why is Swing doing this? Can it be fixed?

NOTE: this shows up regardless of whether I use setPreferredSize() or not.

Was it helpful?

Solution

Why is Swing doing this?

The preferred height of each panel added to the centre panel is 20. The BoxLayout needs to scale each of the panels to fill then entire space available. It looks like there is some kind of rounding problem when this scaling is done.

Can it be fixed?

Not sure what your exact requirement is?

If you are attempting to make each component the same height then you can use the Relative Layout. It has a property that allows you to specify what to do with remaining pixels as a result of rounding.

Edit:

Took a closer look at your code and the problem is:

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

Never set the size of the frame. That is the job of the layout manager. The size you manually set includes the titlebar and borders so all the panels are smaller than you wish and they are not divisible by 10.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top