GridBagLayout没有将按钮锚定到PAGE_END

时间:2013-09-15 10:25:02

标签: java swing anchor gridbaglayout

我在这个网站上找到了一个不同的答案,那个人有同样的问题,但我的按钮仍然没有锚定到底部。有任何想法吗 ?

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

public class InstructionsPanel {



 JComponent InstructionsPanel() throws IOException {

        JComponent iPanel = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png"))));   

        iPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        BufferedImage button1icon = ImageIO.read(new File("res/MainMenu.png"));
        JButton button1 = new JButton("",new ImageIcon(button1icon));

        gbc = new GridBagConstraints();
        iPanel.add(button1);
        gbc.weighty = 1.0;
        gbc.gridx = 0; 
        gbc.anchor = GridBagConstraints.PAGE_END;
        button1.setBorder(BorderFactory.createEmptyBorder());
        //button1.setContentAreaFilled(false);

        return iPanel;  

    }

}  

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:3)

在添加GridBagConstraints时及在定义后gbc使用JButton iPanel.add(button1, gbc);

{{1}}

阅读How to Use GridBagLayout