如何将组件放在java swing中的图像上? [英] How to put components over an image in java swing?

查看:40
本文介绍了如何将组件放在java swing中的图像上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在整个 GUI 框架的背景中添加一张图片,然后将其他组件(按钮、文本字段等)放在上面.Java Swing 是否可以?

I want to add a picture in the background of my entire GUI frame, and then put other components (buttons, text fields etc.) over it. Is it possible with Java Swing?

我的问题主要是关于让顶层 () 组件位于背景中的图片上(如标题所示).这是一种层次结构,其中底部有一个容器,容器中有一个图像(拉伸以填充容器),然后按钮位于其上.

My question is mainly about making top-level () components sit on the picture in the background (as the title suggests). That is getting a kind of hierarchy in which there is a container at the bottom, an image in the container (stretched to fill the container) and then buttons sitting on it.

有没有办法做到这一点?任何提示或建议将不胜感激.

Is there a way to do that? Any tips or suggestions will be greatly appreciated.

推荐答案

  1. 使用包含图标的 JLabel 作为框架的背景组件.
  2. 设置 JLabel 的布局管理器
  3. 将组件添加到 JLabel.

如果需要,您还可以使用拉伸图标图像随着帧调整大小而动态缩放.

You can also use the Stretch Icon, if you want the image to dynamically scale as the frame resizes.

或者您也可以使用 背景面板为您定制绘画.它允许您以实际大小绘制图像,缩放或平铺.

Or you can also use the Background Panel which will do custom painting for you. It allows you to paint the image at its actual size, scaled or tiled.

这篇关于如何将组件放在java swing中的图像上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆