1. 創建正常窗體
先創建一個簡單的界面,它使用BorderLayout,在其中安放5個JButton,如下代碼所示,
public class ShapedFrame extends JFrame {
private static final long serialVersionUID = -2291343874280454383L;
private JButton centerButton = new JButton(“Center”);
public ShapedFrame() {
super(“Shaped Frame”);
setDefaultCloseOperation(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);
}
});
}
// 將Window置於屏幕正中
private static void setAtCenter(Window window) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screenSize.width – window.getWidth()) / 2,
(screenSize.height – window.getHeight()) / 2);
}
}
執行上述程序的效果如下圖所示,
2. 創建不規則窗體
基於上述程序創建不規則窗體,使整個窗體正好缺失掉RIGHT JButton所在的區域,如下代碼所示,
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”);
setDefaultCloseOperation(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) { // 當UI組件(JFrame)的尺寸發生改變時,調用該方法
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
// 構建一個六邊形,將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);
}
}
執行上述程序後,會有如下圖所示的效果,