边框布局未填满整个框架

时间:2016-04-15 20:49:01

标签: java swing layout-manager cardlayout border-layout

我在“Panel01”类中加载了1024x768像素的图像大小。此图像表示背景,但边框布局未填充整个帧。周围有一个空间,我不知道为什么。当然,图像被“panelButtons”面板切割到底部,但这不是问题(我可以将图像分成两部分)。

我该如何解决?我应该使用不同的布局吗?

public class MainClass {

private static final String CARD1 = "Card 1";
private static final String CARD2 = "Card 2";    

private static void GUI()
{
    JFrame frame = new JFrame("Funkcje");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1024, 768);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);

    final JPanel panelMain = new JPanel();
    panelMain.setLayout(new CardLayout(20, 20));

    Panel01 win1 = new Panel01();
    panelMain.add(win1, CARD1);
    Panel02 win2 = new Panel02();
    panelMain.add(win2, CARD2);

    JPanel panelButtons = new JPanel();

    final JButton buttonPrevious = new JButton("Cofnij");
    buttonPrevious.setBackground(Color.BLACK);
    buttonPrevious.setForeground(Color.WHITE);
    panelButtons.add(buttonPrevious);

    buttonPrevious.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cardLayout = (CardLayout) panelMain.getLayout();
            cardLayout.previous(panelMain);
        }
    });

    final JButton buttonNext = new JButton("Dalej");
    buttonNext.setBackground(Color.BLACK);
    buttonNext.setForeground(Color.WHITE);
    panelButtons.add(buttonNext);

    buttonNext.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cardLayout = (CardLayout) panelMain.getLayout();
            cardLayout.next(panelMain);   
        }
    });

    frame.add(panelMain, BorderLayout.CENTER);
    frame.add(panelButtons, BorderLayout.PAGE_END);

    frame.setVisible(true);
}

public static void main(String... args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            GUI();
        }
    });
}
}

0 个答案:

没有答案