如何在java borderlayout中删除南北面板之间的差距

时间:2013-03-07 06:06:45

标签: java swing jpanel border-layout gaps-in-visuals

我使用BorderLayout,我在北方放东西,在南方放东西,但这两个面板(北方和南方)之间总是有一条白线,如何删除这条线?

我已尝试setVGap并为HGap设置BorderLayout,我尝试设置父框架的背景颜色,这两种颜色都不起作用

感谢您的帮助,现在我在这里放了一些代码,也许您可​​以看到错误。

公共类MediaSelector扩展了JPanel {

public MediaSelector() {

    setBorder(new EmptyBorder(-1, -1, 0, -2));
    setBackground(Color.red);
    setLayout(new BorderLayout());
    JPanel panel1 = new JPanel();
    panel1.setBackground(new Color(home_screen_r,home_screen_g,home_screen_b));
    panel1.setLayout(null);

    JPanel buttonPane = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    buttonPane.setLayout(gridbag);
    buttonPane.setBackground(new Color(footer_r, footer_g, footer_b));

    add("North", panel1);
    add("South", buttonPane);
}
main(){
    mediaSelectorPane = new MediaSelector();
    mediaSelectorPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(mediaSelectorPane);
    if (isFullScreen) {
        frame.dispose();
        frame.setUndecorated(true);
        frame.getGraphicsConfiguration().getDevice()
                .setFullScreenWindow(frame);
        frame.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:3)

实际上,对我来说很好。必须是你做的其他事情。

enter image description here

public class TestLayout19 {

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

    public TestLayout19() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel north = new TestPane(Color.RED);
                JPanel center = new TestPane(Color.GREEN);
                JPanel south = new TestPane(Color.BLUE);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(north, BorderLayout.NORTH);
//                frame.add(center, BorderLayout.CENTER);
                frame.add(south, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane(Color bg) {
            setBackground(bg);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

    }

}

在您提出要求之前,您需要先分享您的代码示例,然后才能提供进一步的帮助