Custom JPanel panel background
1. Preface
1. The full name of GUI is Graphical User Interface, which is the graphical user interface. JAVA's GUI is widely used in our lives and is also very common. Many applications use this GUI to program and design, such as clicking the QQ icon to pop up the corresponding login form.
Generally, the interaction between programs and users is based on the running interface of the corresponding program.
2. JPanel panel is a panel container class under SWING. This panel supports nesting, can set layout methods, and set different layout managers to add other controls such as JButton buttons, JTextField text boxes, etc. To design and improve a program interface form.
It is far from enough to support setBackground() as a drawing panel. Here we implement customization to set the image background for JPanel.
2. Platform Tools
1.MyEclipse
Demonstrate using myeclipse2014 here
Other platforms that support java awt+swing are also available
3. Picture display
1. Different processing effects of JPanel under the same form
(1) First create an unmodified form. The general default jpanel interface effect is as follows:
(2) Simple setting of background color effects:
(3) Customized the form effect under JPanel after processing:
2. Code implementation
Customize JPanel background processing, the image is bg.PNG, and it is in the same path as the test class. Please use the relative path when using the image.
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 a artboard public GUITest(){ //Construction method jframe = new JFrame(); init(); } private void init(){ jframe.setTitle("test"); jpanel = new JPanel(){//The key code is to rewritten a method of paint @Override protected void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon img = new ImageIcon(GUITest.class.getResource("bg.png")); /** * bg.PNG is replaced with its own image* The 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 using absolute paths*/ img.paintIcon(this, g, 0, 0); } }; jpanel.setOpaque(true); jframe.setBounds(200, 200, 500, 400); //Set the display position 200 pixels to the left and the upper 200 pixels and the screen size is 500*400 jframe.add(jpanel); //Add the artboard to the form jframe.setVisible(true); //Set the display interface} public static void main(String[] args) { new GUITest(); //Instantiate the object} } 4. Expand the layout manager
Here is a simple login form:
A simple login form implemented based on a custom JPanel background, setting 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 { //Declare form, panel and control 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() initialize and display 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); } }; //Initialize the text for JLabel, JButton jlabel.setText("Username:"); jlabel1.setText("Password:"); jbutton1.setText("Login"); jbutton2.setText("Exit"); jbutton3.setText("Register"); //Set the 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 username 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 the 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 form jframe.add(jpanel); //Form initialization is completed} private static GridBagConstraints getGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, 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 image name
(2) The effect is shown in the figure:
Layout is the foundation and is also very important in GUI design.
To be proficient in using and mastering the three major layouts and other layout managers, you need to practice by yourself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.