Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 466|回复: 0

[Swing学习]swing构建不规则窗体

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-17 23:58:07 | 显示全部楼层 |阅读模式
    java从JDK 6 update 10开始将内建支持构建指定形状的窗体,类com.sun.awt.AWTUtilities中的方法setWindowShape会根据不同的Shape实现去构造相应形状的窗体。AWTUtilities类是放在SUN的包中,在使用该方法时应该通过反射去进行调用,如下代码所示,
    Class<?> clazz = Class.forName("com.sun.awt.AWTUtilities");
    Method method = clazz.getMethod("setWindowShape", Window.class, Shape.class); 1. 创建正常窗体
    先创建一个简单的界面,它使用BorderLayout,在其中安放5个JButton,如下代码所示
    1. import java.awt.*;
    2. import javax.swing.*;
    3. public class ShapedFrame extends JFrame {
    4.     private static final long serialVersionUID = -2291343874280454383L;
    5.     private JButton centerButton = new JButton("Center");
    6.     public ShapedFrame() {
    7.         super("Shaped Frame");
    8.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    9.         initUI();
    10.     }
    11.     private void initUI() {
    12.         Container container = getContentPane();
    13.         container.setLayout(new BorderLayout());
    14.         container.add(new JButton("TOP"), BorderLayout.PAGE_START);
    15.         container.add(new JButton("RIGHT"), BorderLayout.LINE_END);
    16.         container.add(new JButton("BOTTOM"), BorderLayout.PAGE_END);
    17.         container.add(new JButton("LEFT"), BorderLayout.LINE_START);
    18.         container.add(centerButton, BorderLayout.CENTER);
    19.     }
    20.     public static void main(String[] args) {
    21.         SwingUtilities.invokeLater(new Runnable() {
    22.             @Override
    23.             public void run() {
    24.                 ShapedFrame frame = new ShapedFrame();
    25.                 frame.setSize(new Dimension(400, 300));
    26.                 frame.setUndecorated(true);
    27.                 setAtCenter(frame);
    28.                 frame.setVisible(true);
    29.             }
    30.         });
    31.     }
    32.     // 将Window置于屏幕正中
    33.     private static void setAtCenter(Window window) {
    34.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    35.         window.setLocation((screenSize.width - window.getWidth()) / 2,
    36.                 (screenSize.height - window.getHeight()) / 2);
    37.     }
    38. }
    复制代码
    效果图:


    2. 创建不规则窗体
    基于上述程序创建不规则窗体,使整个窗体正好缺失掉RIGHT JButton所在的区域,如下代码所示

    1. import java.awt.*;
    2. import java.awt.event.*;
    3. import javax.swing.*;
    4. import java.lang.reflect.Method;
    5. public class ShapedFrame extends JFrame {
    6.     private static final long serialVersionUID = -2291343874280454383L;
    7.     private static Method method = null;
    8.     static {
    9.         try {
    10.             Class< ?> clazz = Class.forName("com.sun.awt.AWTUtilities");
    11.             method = clazz.getMethod("setWindowShape", Window.class, Shape.class);
    12.         } catch (Exception e) {
    13.             e.printStackTrace();
    14.         }
    15.     }
    16.     private JButton centerButton = new JButton("Center");
    17.     public ShapedFrame() {
    18.         super("Shaped Frame");
    19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    20.         initUI();
    21.         addComponentListener(componentListener);
    22.     }
    23.     private void initUI() {
    24.         Container container = getContentPane();
    25.         container.setLayout(new BorderLayout());
    26.         container.add(new JButton("TOP"), BorderLayout.PAGE_START);
    27.         container.add(new JButton("RIGHT"), BorderLayout.LINE_END);
    28.         container.add(new JButton("BOTTOM"), BorderLayout.PAGE_END);
    29.         container.add(new JButton("LEFT"), BorderLayout.LINE_START);
    30.         container.add(centerButton, BorderLayout.CENTER);
    31.     }
    32.     private ComponentListener componentListener = new ComponentAdapter() {
    33.         @Override
    34.         public void componentResized(ComponentEvent evt) { // 当UI组件(JFrame)的尺寸发生改变时,调用该方法
    35.             Rectangle frameRect = getBounds();
    36.             Rectangle spaceRect = centerButton.getBounds();
    37.             Point o1 = new Point(0, 0);
    38.             Point o2 = new Point(frameRect.width, 0);
    39.             Point o3 = new Point(frameRect.width, frameRect.height);
    40.             Point o4 = new Point(0, frameRect.height);
    41.             Point i1 = new Point(spaceRect.x + spaceRect.width, spaceRect.y);
    42.             Point i2 = new Point(frameRect.width, spaceRect.y);
    43.             Point i3 = new Point(frameRect.width, spaceRect.y
    44.                     + spaceRect.height);
    45.             Point i4 = new Point(spaceRect.x + spaceRect.width, spaceRect.y + spaceRect.height);
    46.             int[] xpoints = new int[] { o1.x, o2.x, i2.x, i1.x, i4.x, i3.x, o3.x, o4.x };
    47.             int[] ypoints = new int[] { o1.y, o2.y, i2.y, i1.y, i4.y, i3.y, o3.y, o4.y };
    48.             int npoints = 8;
    49.             // 构建一个六边形,将RIGHT JButton所处的位置空缺出来
    50.             Shape shape = new Polygon(xpoints, ypoints, npoints);
    51.             setWindowShape(ShapedFrame.this, shape);
    52.         }
    53.     };
    54.     // 设置Window的形状
    55.     private static void setWindowShape(Window window, Shape shape) {
    56.         try {
    57.             method.invoke(null, window, shape);
    58.         } catch (Exception e) {
    59.             e.printStackTrace();
    60.         }
    61.     }
    62.     public static void main(String[] args) {
    63.         SwingUtilities.invokeLater(new Runnable() {
    64.             @Override
    65.             public void run() {
    66.                 ShapedFrame frame = new ShapedFrame();
    67.                 frame.setSize(new Dimension(400, 300));
    68.                 frame.setUndecorated(true);
    69.                 setAtCenter(frame);
    70.                 frame.setVisible(true);
    71.             }
    72.         });
    73.     }
    74.     // 将Window置于屏幕正中
    75.     private static void setAtCenter(Window window) {
    76.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    77.         window.setLocation((screenSize.width - window.getWidth()) / 2,
    78.                 (screenSize.height - window.getHeight()) / 2);
    79.     }
    80. }
    复制代码
    效果图:


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/17/235807593.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:51 , Processed in 0.293306 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表