11.ImportJavax.swing.ImageIcon; 22.ImportJavax.swing.JFrame; 33.ImportJavax.swing.JLabel; 44.ImportJavax.swing.JPanel; 55. Public classMainjframeextendsJFrame {66. 77. PublicMainjframe () {88.//Set Title99.Super("Jfram Set background picture (cannel_2020)"); Ten10.//Set Size OneSetSize (500, 400); A12.//Set Location -SetLocation (200, 50); -14.//the path of the background picture. (relative path or absolute path.) This example picture is placed under the "Java Project name" file) the. String Path = "Background.jpg"; -16.//Background Image -ImageIcon background =NewImageIcon (path); -18.//display the background image in a label +JLabel label =NewJLabel (background); -20.//set the size position of the label to the picture just fill the entire panel +Label.setbounds (0, 0, This. GetWidth (), This. GetHeight ()); A22.//Convert the content pane to JPanel, or you cannot use method Setopaque () to make the contents pane transparent atJPanel Imagepanel = (JPanel) This. Getcontentpane (); -Imagepanel.setopaque (false); -25.//add a background image to the bottom of the hierarchy pane as a background -26. This. Getlayeredpane (). Add (Label,NewInteger (Integer.min_value)); -27.//Set Visible -SetVisible (true); in29.//Exit When you click the Close button -30. Setdefaultcloseoperation (Jframe.exit_on_close); to31. } +32. -33. Public Static voidMain (string[] args) { the34.NewMainjframe (); *35. } $36.}
Java window Add background