简体   繁体   中英

Java replace JFrame with JPanel

I'm trying to 'embed' an existing Java app into my own application. The existing app is a JFrame with a number of components added to it. I copied the code into my project and replaced the JFrame with a new JPanel that I then added to my own app.

The problem is everything painted in the JPanel appears to be shifted up by a small amount (eg ~20px). I wondered if it's anything to do with window menubars etc but so far haven't been able to find a solution apart from manually adding an offset when painting (definitely not ideal!).

I know without the code it can be difficult to offer helpful suggestions, but what I want to know is, do the JFrame and JPanel behave differently in a way that would cause this? Or is there anything else obvious to fix it?

One solution is to create an empty border. Try something like this:

panelName.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));

This will shift everything in the JPanel down 20 pixels, and it should solve your problem. Try adjusting the number of pixels until you find the center.

Take a look at this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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