【问题标题】:Set a background Image on GridBagConstraints container [duplicate]在 GridBagConstraints 容器上设置背景图像 [重复]
【发布时间】:2013-10-04 15:00:47
【问题描述】:

我得到了这个代码:

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)时,它永远不会起作用。我想是因为这个布局。所以我的问题是,如何为我的框架设置背景图像?

谢谢帮助

【问题讨论】:

  • 希望answer 能够在这方面为您提供帮助。再发一封detailed answer 了解更多信息。第二个example,在这个链接上也有助于这个方向:-)
  • @nIcEcOw 感谢这个,它就像一个字符 :) 如果你的评论是一个答案,我会接受这个。
  • 很高兴,它对您有所帮助。我希望我可以将其作为答案发布,但这会导致论坛上的冗余,这被认为是一种不好的做法 :-) 尽管对于其余的人来说,你最欢迎并保持微笑:-)

标签: java swing jpanel frame


【解决方案1】:

但是当我想添加图像背景 (image.jpeg) 时,它永远不会工作

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

请参阅Background Panel,了解您可以根据需要使用的几种方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 2019-09-14
    • 2014-01-19
    • 2011-09-02
    • 2023-03-20
    • 2011-01-09
    • 2021-03-14
    • 2014-07-22
    相关资源
    最近更新 更多