The following articles mainly describe how to add beautiful background images to the actual Oracle Java-related applications. The actual application code that can be executed after sorting is as follows: I hope this article will help you in its practical application.
- Import java. awt .*;
- Import javax. swing .*;
- Public class TestBackgroundColor extends JFrame
- {
- Public static void main (String [] args)
- {
- TODO Auto-generated method stub
- TestBackgroundColor tbc = new TestBackgroundColor ();
- Tbc. setVisible (true );
- }
- Private JPanel imagePanel;
- Private ImageIcon background;
- Public TestBackgroundColor ()
- {
- Background = new ImageIcon ("gradient background 14.png ");
Background Image
- JLabel label = new JLabel(background);
Display the background image in a tag
Set the label size and position to the image to fill the entire panel.
- label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
Convert the content pane to JPanel. Otherwise, you cannot use the setOpaque () method to make the content pane transparent.
- imagePanel = (JPanel)this.getContentPane();
- imagePanel.setOpaque(false);
The default layout manager of the content pane is BorderLayout.
- ImagePanel. setLayout (new FlowLayout ());
- ImagePanel. add (new JButton ("test button "));
- This. getLayeredPane (). setLayout (null );
Add the background image to the bottom layer of the layered pane as the background
- this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(background.getIconWidth(),background.getIconHeight());
- this.setVisible(true);
- }
- }
The above content is an introduction to adding beautiful background images to the Oracle Java program. I hope you will have some gains.