<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    John Jiang

    a cup of Java, cheers!
    https://github.com/johnshajiang/blog

       :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理 ::
      131 隨筆 :: 1 文章 :: 530 評(píng)論 :: 0 Trackbacks
    構(gòu)建不規(guī)則窗體
    在開發(fā)一個(gè)新浪微博Swing客戶端的過程中希望能展現(xiàn)不規(guī)則的窗體界面,原來JDK 6 update 10提供了創(chuàng)建指定形狀窗體的特性,簡(jiǎn)單易用,記于此處。(2010.05.31最后更新)

    Java從JDK 6 update 10開始將內(nèi)建支持構(gòu)建指定形狀的窗體,類com.sun.awt.AWTUtilities中的方法setWindowShape會(huì)根據(jù)不同的Shape實(shí)現(xiàn)去構(gòu)造相應(yīng)形狀的窗體。AWTUtilities類是放在SUN的包中,在使用該方法時(shí)應(yīng)該通過反射去進(jìn)行調(diào)用,如下代碼所示,
    Class<?> clazz = Class.forName("com.sun.awt.AWTUtilities");
    Method method 
    = clazz.getMethod("setWindowShape", Window.class, Shape.class);

    1. 創(chuàng)建正常窗體
    先創(chuàng)建一個(gè)簡(jiǎn)單的界面,它使用BorderLayout,在其中安放5個(gè)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(400300));
                    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);
        }
    }

    執(zhí)行上述程序的效果如下圖所示,


    2. 創(chuàng)建不規(guī)則窗體
    基于上述程序創(chuàng)建不規(guī)則窗體,使整個(gè)窗體正好缺失掉RIGHT JButton所在的區(qū)域,如下代碼所示,
    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) { // 當(dāng)UI組件(JFrame)的尺寸發(fā)生改變時(shí),調(diào)用該方法
                Rectangle frameRect = getBounds();
                Rectangle spaceRect 
    = centerButton.getBounds();

                Point o1 
    = new Point(00);
                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
                
    // 構(gòu)建一個(gè)六邊形,將RIGHT JButton所處的位置空缺出來
                Shape shape = new Polygon(xpoints, ypoints, npoints);

                setWindowShape(ShapedFrame.
    this, shape);
            }
        };

        
    // 設(shè)置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(400300));
                    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);
        }
    }

    執(zhí)行上述程序后,會(huì)有如下圖所示的效果,


    posted on 2011-05-31 20:46 John Jiang 閱讀(1988) 評(píng)論(0)  編輯  收藏 所屬分類: JavaSEJavaSwingGUI原創(chuàng)
    主站蜘蛛池模板: 免费看无码自慰一区二区| 窝窝影视午夜看片免费| 亚洲免费一级视频| 久久亚洲一区二区| 亚洲AV综合色区无码一区| 五月天国产成人AV免费观看| 精品国产精品久久一区免费式| 亚洲精品免费网站| 免费A级毛片无码A| 青草久久精品亚洲综合专区| 无码人妻精品中文字幕免费| 亚洲精品天天影视综合网| 亚洲综合亚洲国产尤物| 中文字幕在线免费观看| 亚洲国产成人久久精品影视| 8x8x华人永久免费视频| 亚洲1234区乱码| 亚洲国产精品无码专区在线观看| 最新精品亚洲成a人在线观看| 日韩免费无码一区二区三区 | 亚洲爆乳成av人在线视菜奈实| 在线亚洲97se亚洲综合在线 | 免费在线观看一区| 男女男精品网站免费观看| 一区二区三区免费电影| 人人揉揉香蕉大免费不卡| 久久国产精品成人片免费| 久久久高清免费视频| 国产高清免费的视频| 亚洲色一色噜一噜噜噜| 久久久久亚洲AV片无码| 亚洲国产超清无码专区| 亚洲A∨精品一区二区三区下载 | 亚洲一区二区高清| 亚洲av无码潮喷在线观看| 亚洲国产美女精品久久| 青青视频免费在线| 99在线视频免费观看| 91免费国产在线观看| 四虎影视永久免费观看| 久久亚洲AV午夜福利精品一区 |