Java-jframe Set background picture __java

Source: Internet
Author: User
Tags clear screen set background
Set JFrame background picture from two ways:
1.Jpanel class Paintcomponent (Graphics g) Redraw background picture
2. Use Layeredpane to add depth to swing controls Allow the build to overlap at the required time
    from low to High level is: Default,palette,modal,popup,drag

Run the show: Upload later

API query:
Jpanel:http://docs.oracle.com/javase/7/docs/api/javax/swing/jpanel.html
Graphics:http://docs.oracle.com/javase/7/docs/api/java/awt/graphics.html
Layeredpane:http://www.apihome.cn/api/java/jlayeredpane.html

Tips:
(1) The difference between paintcomponent (Graphics g) and paint ():
Super.paintcomponent (g) is the parent class JPanel in the method, will be the entire panel with the background color redraw again, play the role of clear screen

The AWT control is drawn in the paint method, so as long as the AWT control inherits, it is possible to simply overwrite the paint method.
In a swing control, the paint method calls Paintcomponent, Paintborder,
Paintchildren three methods, the latter both generally default, so swing programming, if inherited jcomponent or its subclasses, to overwrite paintcomponent rather than paint method

(2) Description of Layeredpane
Jlayeredpanel as a Java-level panel is the best way to create multiple edit windows and implement some special display hierarchy effects
Components that currently have built-in support for Jlayeredpanel include the Jframe,jdialog,jdesktoppanel itself is a Jlayeredpanel object, and Jlayeredpanel can contain multiple JComponent component objects. And you can switch to the edit state with each other.
* It is noteworthy that:
because Jlayeredpanel does not have LayoutManager, you must complete the set SetBounds () function on each object that is added to the Jlayeredpanel, otherwise the component cannot be displayed. Because Jlayeredpanel does not have LayoutManager, you must complete the set SetBounds () function on each object that is added to the Jlayeredpanel, otherwise the component cannot be displayed.

Method 1:

Import java.awt.*;  

Import javax.swing.*; Method 1: By adding a jpanel to the JFrame, the background picture is placed on JPanel to implement the public class Java_jframe extends JFrame//default BorderLayout {//Chuang  
    Build a container Container ct;  
    Create a background panel.         Backgroundpanel BGP;  
    Custom background class//Create a button to prove that we did create a background picture, not a picture.  
    JButton JB;  
    public static void Main (string[] args) {new java_jframe ();  
        Public Java_jframe () {//Do not use any layout method.
        Ct=this.getcontentpane ();  

        This.setlayout (NULL); Redraw the background picture bgp=new Backgroundpanel ((New ImageIcon ("D:\\fox download\\editplus_ck_xp85\\5.png")). GetImage ());  
        parameter is an Image object, Bgp.setbounds (0,0,400,300);  

        Ct.add (BGP);  
        Create button jb=new JButton ("Test button");  
        Jb.setbounds (60,30,160,30);  

        Ct.add (JB);  
        This.setsize (400,300);  
        This.setlocation (400,300);  
        This.setdefaultcloseoperation (Jframe.exit_on_close); This.setvisIble (TRUE);  
    } class Backgroundpanel extends JPanel {Image im;  
        Public Backgroundpanel (Image im) {this.im=im;                    This.setopaque (TRUE); Setting the control opaque, if false, is transparent}//draw the background again, inherited from Jpanle, is a way for swing controls to inherit the implementation, not the paint () public Vo in AWT  
        ID paintcomponent (Graphics g)//drawing class, details can be seen in the blogger's Java java-graphics {super.paintcomponents (g);  G.drawimage (Im,0,0,this.getwidth (), This.getheight (), this); Draws the currently available image in the specified image. The upper-left corner of the image is (x, y) in the coordinate space of the graphics context. The transparent pixels in the image do not affect the pixels that already exist at this point}}

Method 2:

We used Jlayeredpane,jlayeredpane to add depth to the jfc/swing container, allowing components to overlap when needed. The Integer object specifies the depth of each component in the container, where the higher-numbered component is on top of other components//levels: Default,palette,modal,popup,drag public class Java_jframe extends  
    JFrame {//Create a jlayeredpane for layering.  
    JLayeredPane Layeredpane;  
    Create a panel and a label to store the picture as a background.  
    JPanel JP;  
    JLabel JL;  

    ImageIcon image;  
    Create a button for the test.  
    JButton JB;  
    public static void Main (string[] args) {new java_jframe ();  
        Public Java_jframe () {layeredpane=new jlayeredpane ();        
        Image=new ImageIcon ("D:\\fox download\\editplus_ck_xp85\\5.png");//Find a picture to see the effect.  
        Create the background of those things jp=new JPanel ();  

        Jp.setbounds (0,0,image.geticonwidth (), Image.geticonheight ());  
Jl=new JLabel (image);  
        Jl.setbounds (0,0,image.geticonwidth (), Image.geticonheight ());  

        Jp.add (JL);  
        Create a Test button jb=new JButton ("Test button"); Jb.setbounds (100,100,100,100);                                    Put JP to the bottom.  
        /* Hierarchical relationship Visible Method 2 below the comment/Layeredpane.add (jp,jlayeredpane.default_layer);  

        Place the JB in a Layeredpane.add (Jb,jlayeredpane.modal_layer);  
        This.setlayeredpane (Layeredpane);  
        This.setsize (Image.geticonwidth (), Image.geticonheight ());  
        This.setdefaultcloseoperation (Jframe.exit_on_close);  
        This.setlocation (Image.geticonwidth (), Image.geticonheight ());    
    This.setvisible (TRUE); }  
}

Any comments or questions can be raised ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.