Java JFrame implementation Without Borders untitled __java

Source: Internet
Author: User
Tags java jframe

A lot of times we get an interface, do not want the default border, or do not want the top right corner of the maximum button, such as the QQ panel does not have the maximum button.

But I checked a lot of information that can not directly remove the maximum button, you must have the entire border and the title are removed, and then draw their own.


The test code is also simple:

"1" jframe no border without a title

"2" Add a background picture (there are many ways to add a background picture, mostly pictures in the JLabel, JLabel on the Panel, the Panel has a panel above the other controls)

"3" to create ImageIcon, directly with the new ImageIcon ("Img/aaa.jpg"), the creation of the picture can not be placed inside the SRC package, put inside no, I do not know why ...

"3" to implement drag. No border without a title can not be dragged, to achieve their own.


Package com.qiantu.jframe;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;

Import Java.awt.event.MouseMotionAdapter;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JLayeredPane;

Import Javax.swing.JPanel; public class Jframenoborder extends JFrame {public static void main (string[] args) {Jframenoborder j = new Jframeno
		Border ();
	J.setvisible (TRUE);
	Private static final long serialversionuid = 1L;
	Used to handle drag events, indicating the coordinates of the mouse when pressed, relative to jframe int xold = 0;
	
	int yold = 0;

		Public Jframenoborder () {this.setlayout (null); Handle drag Event This.addmouselistener (new Mouseadapter () {@Override public void mousepressed (MouseEvent e) {xold =
				E.getx ();
			Yold = E.gety ();
		}
		}); This.addmousemotionlistener (New Mousemotionadapter () {@Override public void mousedragged (MouseEvent e) {intXonscreen = E.getxonscreen ();
				int yonscreen = E.getyonscreen ();
				int xx = Xonscreen-xold;
				int yy = Yonscreen-yold;
			JFrameNoBorder.this.setLocation (xx, yy);

		}
		});
		JLayeredPane is used to add two layers, one for the background, one for the interface JLayeredPane Layeredpane = new JLayeredPane ();
		Layeredpane.setbounds (0, 0, 200, 200);
		
		This.add (Layeredpane);
		Background panel JPanel Bgpanel = new JPanel ();
		Bgpanel.setbounds (0, 0, 200, 200);
		
		Layeredpane.add (Bgpanel);
		Background picture, add to Background panel inside JLabel Bglabel = new JLabel (New ImageIcon ("Img/bgimage.png"));
		
		Bgpanel.add (Bglabel);
		The main interface, that is, the background above the layer panel JPanel mainpanel = new JPanel ();
		Mainpanel.setbounds (0, 0, 200, 200);
		Mainpanel.setlayout (NULL);

		Layeredpane.add (Mainpanel);
		Close button JButton CloseButton = new JButton ();
		Closebutton.seticon (New ImageIcon ("Img/closebutton.png"));
		Closebutton.setbounds (170, 0, 30, 30);
				Closebutton.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {System.exit (0);
		}
		});
		
		Mainpanel.add (CloseButton);
		This.setbounds (50,50,200,200);
	This.setundecorated (TRUE);
 }
}
Drag schematic:

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.