java - 在 GridBagConstraints 容器上设置背景图像

标签 java swing jpanel frame

我得到了这个代码:

JFrame frame = new JFrame("FilesComparator");

    frame.setVisible(true);

    frame.setSize(1000, 600);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height
                / 2 - frame.getSize().height / 2);
    JButton buttonStart;
    JButton buttonCancel;

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();


    buttonStart = new JButton("Start");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;

    buttonStart.addActionListener(new ActionStart());
    pane.add(buttonStart, c);
    buttonCancel = new JButton("Cancel");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weighty = 15.0;
    c.anchor = GridBagConstraints.PAGE_END;
    buttonCancel.addActionListener(new ActionCancel());
    pane.add(buttonCancel, c);
}}

一切正常。但是当我想添加图像背景(image.jpeg)时它永远不会工作。我想这是因为这个布局。所以我的问题是,如何为我的框架设置背景图像?

感谢帮助

最佳答案

But when i want to add an image background ( image.jpeg) its never work

我没有看到您尝试添加背景图像的任何代码。

参见Background Panel您可以根据自己的要求使用几种方法。

关于java - 在 GridBagConstraints 容器上设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184508/

相关文章:

java - Swing 线程安全样板

java - 如何在 Swing 中创建通知

java - JFrame 中只显示一个组件

java - JPanel 的 addMouseListener

java - 为 geckodriver 设置自定义 FireFox 位置

java - 你能动态地重命名一个类名吗?

java - 在JLabel中添加图像但不响应

java - JLabels 不显示

java - gitlab API gitlab4j Java更改默认分支

java - 具有使用相同存储库和模型类的多个数据源的 Spring Boot?