简体   繁体   English

使用JLabel将背景图像添加到JFrame

[英]Adding a background image to JFrame using JLabel

I added a background image to a JFrame using a JLabel. 我使用JLabel将背景图像添加到JFrame。 But there are other JLabels in a panel added to the particular JFrame. 但是,面板中还有其他JLabel添加到了特定的JFrame。 After adding this background image all over the JFrame, other JLabels are not visible. 在整个JFrame中添加此背景图像之后,其他JLabel将不可见。 How do I get them be visible? 如何使它们可见?

Several options: 几种选择:

  1. Make the JLabel opaque and make it the contentPane, giving it a decent layout, give it a decent getPreferredSize() override that makes sense in the context of your problem (JLabel's own override for this method won't work for you, since it depends on text and icons that the label holds and not on components added). 使JLabel不透明并使其成为contentPane,使其具有适当的布局,使其具有适当的getPreferredSize()覆盖,该覆盖在您的问题上下文中是有意义的(JLabel对此方法的自身覆盖对您不起作用,因为这取决于标签所包含的文字和图标上,而不是添加的组件上)。
  2. And then add other components to it -- to the JLabel. 然后向其中添加其他组件-JLabel。

Or: 要么:

  1. Use a JPanel as the background image displayer 使用JPanel作为背景图像显示器
  2. Display the image in its paintComponent(...) method override. 在其paintComponent(...)方法替代中显示图像。
  3. Give it decent layout manager(s) and if needed, a getPreferredSize() override. 给它合适的布局管理器,并在需要时重写getPreferredSize()

Also: 也:

  • Take care that some added components, such as other JPanels, are set to be non-opaque. 注意将某些添加的组件(例如其他JPanels)设置为非透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM