English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The default window in Swing cannot meet our application requirements, so we need to create a JFrame frame with any image and shape, such as the figure below:
You can also set the transparency of the window background image
The following explains how to achieve the effect shown in the figure above:
(1)Firstly, you need a nice image, for example, parchment. However, the downloaded image is a square rectangle, and the shape of the parchment is inside the image, so we use the cutout function in BeautyPlus or Photoshop to extract the parchment, as shown below:
(2)Save the image as a transparent background.
(3)Next, write a myJFrame that inherits from JFrame, the code is as follows:
import com.sun.awt.AWTUtilities; import javax.swing.*; import java.awt.*; public class myJFrame extends JFrame{ private float alpha; public myJFrame(String bgPath,float alpha){ super(); myContentPane rp = new myContentPane(bgPath); rp.setOpaque(false);//Set the content panel to transparent this.setContentPane(rp); this.setUndecorated(true); this.setSize(rp.img.getIconWidth(), rp.img.getIconHeight()); AWTUtilities.setWindowOpaque(this, false);//Set JFrame to transparent this.alpha = alpha; } private class myContentPane extends JPanel{ public ImageIcon img; public myContentPane(String bgPath) { super(); img = new ImageIcon(Test.class.getResource(bgPath)); } @Override protected void paintComponent(Graphics g) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); Composite old = ((Graphics2D) g).getComposite(); ((Graphics2D) g).setComposite(ac); if(img!=null){ g.drawImage(img.getImage(), 0, 0, getWidth(), getHeight(), this); } ((Graphics2D) g).setComposite(old); super.paintComponent(g); } } }
The main code of the above program is: set JFrame to transparent, remove the border of JFrame, set the content panel to transparent, and then draw the image on the content panel.
(4) write a test class Test:
import javax.swing.*; import java.awt.*; public class Test { public static void main(String[] args) { /** * Set the background image and its opacity, 0 for fully transparent,1.0f as opaque. */ myJFrame f = new myJFrame("ab.png",0.7f); f.setLayout(null); Font font = new Font("宋体", Font.PLAIN,30); JLabel user = new JLabel("Username"); user.setFont(font); user.setBounds(100,150,100,30); JTextField userInput = new JTextField(); userInput.setFont(font); userInput.setBounds(200,145,250,40); JLabel ps = new JLabel("Password"); ps.setFont(font); ps.setBounds(110,200,90,30); JTextField psInput = new JTextField(); psInput.setFont(font); psInput.setBounds(200,195,250,40); f.add(user); f.add(userInput); f.add(ps); f.add(psInput); f.setLocation(300,100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
(5)The window cannot be dragged and resized due to the removal of the border. The implementation of the drag and resize function can be seen in this article: Swing Implementation of Window Dragging and Resizing
That's all for this article. I hope it will be helpful to everyone's learning and I also hope everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to report abuse at codebox.com (replace # with @ when sending email), and provide relevant evidence. Once verified, this site will immediately delete the infringing content.