【问题标题】:Adding a background image to JFrame using JLabel使用 JLabel 向 JFrame 添加背景图像
【发布时间】:2015-08-21 13:56:30
【问题描述】:

我使用 JLabel 向 JFrame 添加了背景图像。但是在特定 JFrame 中添加的面板中还有其他 JLabel。在整个 JFrame 中添加此背景图像后,其他 JLabel 不可见。如何让它们可见?

【问题讨论】:

  • 你能提供一个minmal, working example吗?
  • @Pahan Madusha 您可以考虑不使用 JLabel 添加背景图像。如果我是你,我会将它添加到 JPanel 上。在 JPanel 上显示图像。之后,将其他组件添加到 JPanel。之后,将该 JPanel 添加到您的 JFrame 中。

标签: java swing user-interface background-image jlabel


【解决方案1】:

几个选项:

  1. 使 JLabel 不透明并使其成为 contentPane,给它一个体面的布局,给它一个体面的 getPreferredSize() 覆盖,这在你的问题的上下文中是有意义的(JLabel 自己的这个方法的覆盖对你不起作用,因为它取决于标签包含的文本和图标,而不是添加的组件)。
  2. 然后将其他组件添加到它 - 到 JLabel。

或者:

  1. 使用 JPanel 作为背景图像显示器
  2. 在其paintComponent(...) 方法覆盖中显示图像。
  3. 给它合适的布局管理器,如果需要,一个getPreferredSize() 覆盖。

还有:

  • 请注意将某些添加的组件(例如其他 JPanel)设置为不透明。

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 2015-02-12
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多