1. Create a normal form
First, create a simple interface, which uses BorderLayout and places 5 jbuttons in it, as shown in the following code,
Public class ShapedFrame extends JFrame {
Private static final long serialVersionUID =-2291343874280454383L;
Private JButton centerButton = new JButton ("Center ");
Public ShapedFrame (){
Super ("Shaped Frame ");
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
InitUI ();
}
Private void initUI (){
Container container = getContentPane ();
Container. setLayout (new BorderLayout ());
Container. add (new JButton ("TOP"), BorderLayout. PAGE_START );
Container. add (new JButton ("RIGHT"), BorderLayout. LINE_END );
Container. add (new JButton ("BOTTOM"), BorderLayout. PAGE_END );
Container. add (new JButton ("LEFT"), BorderLayout. LINE_START );
Container. add (centerButton, BorderLayout. CENTER );
}
Public static void main (String [] args ){
SwingUtilities. invokeLater (new Runnable (){
@ Override
Public void run (){
ShapedFrame frame = new ShapedFrame ();
Frame. setSize (new Dimension (400,300 ));
Frame. setUndecorated (true );
SetAtCenter (frame );
Frame. setVisible (true );
}
});
}
// Place the Window in the center of the screen
Private static void setAtCenter (Window window ){
Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize ();
Window. setLocation (screenSize. width-window. getWidth ()/2,
(ScreenSize. height-window. getHeight ()/2 );
}
}
Shows the effect of executing the above program,
2. Create an irregular form
Create an irregular form based on the above program, so that the entire form is missing the area where RIGHT JButton is located, as shown in the following code,
Public class ShapedFrame extends JFrame {
Private static final long serialVersionUID =-2291343874280454383L;
Private static Method method = null;
Static {
Try {
Class clazz = Class. forName ("com. sun. awt. AWTUtilities ");
Method = clazz. getMethod ("setWindowShape", Window. class, Shape. class );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Private JButton centerButton = new JButton ("Center ");
Public ShapedFrame (){
Super ("Shaped Frame ");
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
InitUI ();
AddComponentListener (componentListener );
}
Private void initUI (){
Container container = getContentPane ();
Container. setLayout (new BorderLayout ());
Container. add (new JButton ("TOP"), BorderLayout. PAGE_START );
Container. add (new JButton ("RIGHT"), BorderLayout. LINE_END );
Container. add (new JButton ("BOTTOM"), BorderLayout. PAGE_END );
Container. add (new JButton ("LEFT"), BorderLayout. LINE_START );
Container. add (centerButton, BorderLayout. CENTER );
}
Private ComponentListener componentListener = new ComponentAdapter (){
@ Override
Public void componentResized (ComponentEvent evt) {// this method is called when the size of the UI component (JFrame) changes.
Rectangle frameRect = getBounds ();
Rectangle spaceRect = centerButton. getBounds ();
Point o1 = new Point (0, 0 );
Point o2 = new Point (frameRect. width, 0 );
Point o3 = new Point (frameRect. width, frameRect. height );
Point o4 = new Point (0, frameRect. height );
Point i1 = new Point (spaceRect. x + spaceRect. width, spaceRect. y );
Point i2 = new Point (frameRect. width, spaceRect. y );
Point i3 = new Point (frameRect. width, spaceRect. y
+ SpaceRect. height );
Point i4 = new Point (spaceRect. x + spaceRect. width, spaceRect. y + spaceRect. height );
Int [] xpoints = new int [] {o1.x, o2.x, i2.x, i1.x, i4.x, i3.x, o3.x, o4.x };
Int [] ypoints = new int [] {o1.y, o2.y, i2.y, i1.y, i4.y, i3.y, o3.y, o4.y };
Int npoints = 8
// Construct a hexagonal structure to open the RIGHT JButton
Shape shape = new Polygon (xpoints, ypoints, npoints );
SetWindowShape (ShapedFrame. this, shape );
}
};
// Set the Window shape
Private static void setWindowShape (Window window, Shape shape ){
Try {
Method. invoke (null, window, shape );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
SwingUtilities. invokeLater (new Runnable (){
@ Override
Public void run (){
ShapedFrame frame = new ShapedFrame ();
Frame. setSize (new Dimension (400,300 ));
Frame. setUndecorated (true );
SetAtCenter (frame );
Frame. setVisible (true );
}
});
}
// Place the Window in the center of the screen
Private static void setAtCenter (Window window ){
Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize ();
Window. setLocation (screenSize. width-window. getWidth ()/2,
(ScreenSize. height-window. getHeight ()/2 );
}
}
After the above program is executed, the result is as shown in,