Home>

Custom jpanel panel background

I. Introduction

1.gui stands for Graphical User Interface.Java's GUI is widely used in our lives.Many applications use this gui programming design,Click the qq icon to pop up the corresponding login form.

The interaction between a general program and a user is based on the running interface of the corresponding program.

2.jpanel panel is a panel container class under swing.The panel supports nesting.You can set the layout.Set different layout managers to add other controls like jbutton buttons, jtextfield text boxes, etc.To design and improve a program interface window.

The support for setbackground () as the drawing panel to set the background color is far from enough.Here you can customize the image background for jpanel.

Platform tools

1.myeclipse

Demonstrated here using myeclipse2014

Other platforms that support java awt + swing are also available

Graphic display

1. The effect of different jpanel processing under the same form

(1) First create an unmodified form,The general common default jpanel interface effect is as follows:

(2) Simply set the background color effect:

(3) Form effects under jpanel after custom processing:

2. Code implementation

Custom jpanel background processing,The image is bg.png, in the same path as the test class,Use pictures. Note the use of relative paths

import java.awt.graphics;
import javax.swing.imageicon;
import javax.swing.jframe;
import javax.swing.jpanel;
public class guitest {
   private static jframe jframe;// declare a form
   private jpanel jpanel;// declare an artboard
   public guitest () {// constructor
     jframe=new jframe ();
     init ();
   }
   private void init () {
     jframe.settitle ("test");
     jpanel=new jpanel () {// Key code,Is a method that overrides paint
        @override
        protected void paintcomponent (graphics g) {
          super.paintcomponent (g);
          imageicon img=new imageicon (guitest.class.getresource ("bg.png"));
         / **
          * bg.png replace this place with your own picture
          * Relative path used here,bg.png is in the same path as the test class
          * However, it is recommended to use relative paths to avoid absolute paths
          * /
          img.painticon (this, g, 0, 0);
        }
     };
     jpanel.setopaque (true);
     jframe.setbounds (200, 200, 500, 400);// Set the display position 200 pixels from the left 200 pixels from the top 200 pixels and the screen size 500 * 400
     jframe.add (jpanel);// Add artboard to the form
     jframe.setvisible (true);// Set the display interface
   }
   public static void main (string [] args) {
      new guitest ();// instantiate the object
   }
}

Fourth, expand the layout manager

Let's write a simple login form:

A simple login form based on a custom jpanel background, setting up a gridbaglayout layout, adding button text boxes and other basic controls

(1) The code is as follows:

import java.awt.graphics;
import java.awt.gridbagconstraints;
import java.awt.gridbaglayout;
import java.awt.insets;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jpasswordfield;
import javax.swing.jtextfield;
public class guit {
  // declaration form,Panels and controls
  private static jframe jframe;
  private jlabel jlabel, jlabel1;
  private gridbaglayout gridbag;
  private gridbagconstraints constraints;
  private jtextfield jtfield1;
  private jpasswordfield jpfield1;
  private jbutton jbutton1, jbutton2, jbutton3;
  private jpanel jpanel;
  public guit () {
    jframe=new jframe ();
    jlabel=new jlabel ();
    jlabel1=new jlabel ();
    jtfield1=new jtextfield ();
    jpfield1=new jpasswordfield ();
    gridbag=new gridbaglayout ();
    jbutton1=new jbutton ();
    jbutton2=new jbutton ();
    jbutton3=new jbutton ();
    init ();
  }
   / **
   * init () initializes and displays the interface
   * /
  private void init () {
    jframe.settitle ("login");
    / **
     * Set jpanel background
     * /
    jpanel=new jpanel () {
      @override
      protected void paintcomponent (graphics g) {
        super.paintcomponent (g);
        imageicon img=new imageicon (guitest.class.getresource ("ddmbg.jpg"));
        img.painticon (this, g, 0, 0);
      }
    };
    // Init text for jlabel, jbutton
    jlabel.settext ("Username:");
    jlabel1.settext ("Password:");
    jbutton1.settext ("Login");
    jbutton2.settext ("Exit");
    jbutton3.settext ("register");
    // Set display position and screen size 500 * 400
    jframe.setbounds (450, 240, 400, 240);
    // jpanel uses gridbaglayout layout manager
    jpanel.setopaque (false);
    jpanel.setlayout (gridbag);
    // Initialize the user name label and add the control to the artboard
    constraints=getgridbagconstraints (0,0,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 0,0);
    gridbag.setconstraints (jlabel, constraints);
    jpanel.add (jlabel);
    // Initialize the username text box,And add the component to the artboard
    constraints=getgridbagconstraints (1,0,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 100,0);
    gridbag.setconstraints (jtfield1, constraints);
    jpanel.add (jtfield1);
    // Initialize password label
    constraints=getgridbagconstraints (0,1,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 0,0);
    gridbag.setconstraints (jlabel1, constraints);
    jpanel.add (jlabel1);
    // Initialize password text box
    constraints=getgridbagconstraints (1,1,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 100,0);
    gridbag.setconstraints (jpfield1, constraints);
    jpanel.add (jpfield1);
    // Initialize the registration button,And add the control to the artboard
    constraints=getgridbagconstraints (0,2,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 0,0);
    gridbag.setconstraints (jbutton3, constraints);
    jpanel.add (jbutton3);
    // Initialize the login button
    constraints=getgridbagconstraints (1,2,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 0,0);
    gridbag.setconstraints (jbutton1, constraints);
    jpanel.add (jbutton1);
    // initialize exit button
    constraints=getgridbagconstraints (2,2,1,1,0,0, gridbagconstraints.center, gridbagconstraints.none, new insets (10,0,10,0), 0,0);
    gridbag.setconstraints (jbutton2, constraints);
    jpanel.add (jbutton2);
    // Add artboard to the form
    jframe.add (jpanel);
    // Form initialization is complete
  }
   private static gridbagconstraints getgridbagconstraints (int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, insets insets, int ipadx, int ipady) {
     return new gridbagconstraints (gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady);
   }
   public static void main (string [] args) {
     new guit ();
     jframe.setvisible (true);
   }
}

Where ddmbg is the picture name

(2) The effect is shown in the figure:

Layout in gui design is the basic and very important knowledge.

Proficiency in mastering the three major layouts and other layout managers requires you to type the code yourself.

  • Previous Detailed steps of using jQuery regular expressions
  • Next ios get or modify content on a web page
  • Trends