int[] ypoints = new int[] { o1.y, o2.y, i2.y, i1.y, i4.y, i3.y, o3.y, o4.y };
int npoints = 8
// 构建一个六边形,将RIGHT JButton所处的位置空缺出来
Shape shape = new Polygon(xpoints, ypoints, npoints);
setWindowShape(ShapedFrame.this, shape);
}
};
// 设置Window的形状
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);
}
});
}
// 将Window置于屏幕正中
private static void setAtCenter(Window window) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screenSize.width - window.getWidth()) / 2,
(screenSize.height - window.getHeight()) / 2);
}
}
执行上述程序后,会有如下图所示的效果,