Setting a background image on a JFrame using Swing

无人久伴 提交于 2019-11-29 16:52:46

As to your question as it's titled

You will want to setup a custom Component (such as a JPanel) and override the paintComponent method

Take a look at

Now, you're going to have some issues, the note pane will block most of the background, obscuring the image. This shouldn't discourage from trying though ;)

You'll want to familiarise yourself with how Swing components can be made transparent as well.

Some feedback ;)

I don't think you need to keep a reference to the Classloader, for what you're using it for, it's just adding weight and complexity to your code. It's not a massive issue, but my first question was, "What's he using the class loader for??"

I'd probably adopt some kind of naming convention as well. Generally been a little more verbose will make it easier to understand the code.

close ?? Is that an action? Maybe something like "closeButton", at least I know it's some type of button

Better to use:

ImageIcon closeImg = new ImageIcon(getClass().getResource("\\images\\buttons\\closeBtn.png"));

Make sure your image is located in the same location relative to where your class file is output.

Also your close button calls setVisible(false) but does not dispose of any resources or exit the application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!