1

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

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;  

    }

}  

任何帮助将不胜感激,谢谢!

4

1 回答 1

3

GridBagConstraints gbc在添加JButton和定义后使用

iPanel.add(button1, gbc);

阅读如何使用 GridBagLayout

于 2013-09-15T10:33:00.763 回答