问题
As seen in the attached screenshot - the yellow line is from the underlying BoxLayout
-oriented JPanel
. Changing to BorderLayout
removes the yellow line:
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.
回答1:
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));
来源:https://stackoverflow.com/questions/21289569/boxlayout-leaves-pixel-line-at-the-bottom