java - 如何在 GridBagLayout 上放置背景图片

标签 java image swing jpanel gridbaglayout

我是第一次尝试与布局管理器合作,他们只是在踢我的 dentry 。我正在尝试制作背景图像,然后使用 GridBagLayout 将按钮放在顶部,如果有更好的布局管理器请告知。至于尝试学习如何使用布局管理器,它非常困难,任何学习引用也将不胜感激。 This is whats it looks like currently 这是目前的样子 我可以获得显示完整图像的框架,但是当我使用 gridlayout 管理器时,它会那样做

public void addComponentsToPane(Container pane){
    BackgroundImage image = new BackgroundImage();
    JButton button1, button2, button3, button4, button5;
    pane.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    if(shouldFill){
        c.fill = GridBagConstraints.NONE;
    }   
    button1 = new JButton("Button 1");
    if (shouldWeightX) {
    c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 0;
    button1.setOpaque(false);
    pane.add(button1, c);


    button2 = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;    
    c.gridx = 0;
    c.gridy = 0;
    button2.setOpaque(false);
    pane.add(button2, c);

    button3 = new JButton("Button 3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 0;
    button3.setOpaque(false);
    pane.add(button3, c);

    button4 = new JButton("Long-Named Button 4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 40;      //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button4, c);
    button5 = new JButton("button 1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;       //reset to default
    c.weighty = 1.0;   //request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    c.insets = new Insets(10,0,0,0);  //top padding
    c.gridx = 1;       //aligned with button 2
    c.gridwidth = 2;   //2 columns wide
    c.gridy = 2;       //third row
    pane.add(button5, c);
    c.ipadx = 800;
    c.ipady = 400;
    pane.add(image, c);

}

This is what i'm trying to get it to look like 这就是我想让它看起来像的样子

最佳答案

这是一种方法:使用 JLabel 作为带有图像的容器(这有点不寻常,但实际上效果很好):

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestBackgroundImage {

    private static final String BACKHGROUND_IMAGE_URL = "http://www.okyn.org/wp-content/uploads/2013/04/League_of_Legends.jpeg";

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame(TestBackgroundImage.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL));
        JLabel mainPanel = new JLabel(backgroundImage) {
            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                Dimension lmPrefSize = getLayout().preferredLayoutSize(this);
                size.width = Math.max(size.width, lmPrefSize.width);
                size.height = Math.max(size.height, lmPrefSize.height);
                return size;
            }
        };
        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        for (int i = 0; i < 5; i++) {
            mainPanel.add(new JButton("Button " + (i + 1)), gbc);
        }
        // Let's put a filler bottom component that will push the rest to the top
        gbc.weighty = 1.0;
        mainPanel.add(Box.createGlue(), gbc);
        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new TestBackgroundImage().initUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}

结果:

enter image description here

  • GridBagLayout 无法处理组件的分层(因此,如果您有重叠的 gridx/gridy 对,它不会将其处理为分层,我认为输出几乎是不确定的。
  • 尽量避免使用 gridx/gridy,因为它会使代码难以维护。在 gridwidth/gridheight 上使用相对值更容易维护。

关于java - 如何在 GridBagLayout 上放置背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17330908/

相关文章:

java - 在 @PostConstruct 中的 Spring Boot 中使用普通 EntityManager 持久保存 JPA 实体

asp.net - 歌剧错误 : src ="" generated by asp:image is empty

java - JSP - 如何在 HTML JSP 页面中使用从 servlet 发送的图像?

java - Swing JTextPane编译错误

java - JFormattedTextField - 如何仅允许字符?

java - 为什么该类的每个对象对其成员具有相同的值?

java - Maven 生命周期理解

java - 为什么 System 类声明为 final 并带有私有(private)构造函数?

css - 调整图像链接的响应图像无拉伸(stretch)+缩放效果

java - 多线程Swing程序中频繁调用setText()