0
如附件屏幕截图所示 - 黄线来自底层的BoxLayout
,面向JPanel
。更改为BorderLayout
删除黄线:BoxLayout在底部留下像素线
代码示例如下:
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
JPanels设置边界,但它似乎并没有做太多。 Swing为什么要这样做?它可以修复吗?
备注:无论我是否使用setPreferredSize()
,它都会显示。
我会理解是否有舍入误差,但数字全部除以10.我可以理解,如果这是剩余的数量,但它实际上是1像素深。感谢代码,我会考虑它。我对这种明显的差异感到恼火。如果没有其他人提出任何问题,将会标记为已回答:) – Gorbles
@gorb,请参阅编辑。 – camickr
辉煌,固定。从现在起将铭记这一点! – Gorbles