//setting the size of the window, regardless of the Windows theme you use, is flexible, showing the appropriate window size, and must be called before jframe.setvisible (true) .//Alternative to traditional frame.setsize (width,height); Public Static voidSetSize (JFrame frame,intWidthintheight) { //be sure to call before the JFrame displayFrame.pack (); //in the default setresizable (true); titledefaultheight = 27, but if setresizable (false); titledefaultheight = intTitledefaultheight = Frame.isresizable ()? 27:25;//The default development stage uses the Windows Classic theme, title bar height = intContainerheight =Frame.getcontentpane (). GetHeight (); //System.out.println ("containerheight:" + containerheight);//Windows Classic theme or XP under theme = 1//int rootpaneheight = Frame.getrootpane (). GetHeight ();//The height obtained is the same as the height of the container. intFrameheight =frame.getheight (); //System.out.println ("frameheight:" + frameheight);//Windows Classic Theme under =, Windows XP under topic = intTitleheight = Frameheight-containerheight;//Here you can get the actual height of the current title bar if(Titleheight >titledefaultheight) { inth = titleheight-titledefaultheight;//get the currently used theme, title bar-title bar height under Classic theme = DifferenceFrame.setsize (width, height + h);//Finalize The height of the window } Else{frame.setsize (width, height); } }
Java: A solution that sets the best height for jframe windows under different Windows themes