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入门到精通教程
查看: 370|回复: 0

[Swing学习]打造swing专业外观

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

    [LV.1]初来乍到

    发表于 2014-10-31 23:55:42 | 显示全部楼层 |阅读模式
    运行图:
       


    1. 代码1:Demo.java
    2. package demo.swing;
    3. import javax.swing.ImageIcon;
    4. public class Demo {
    5.         /**
    6.          * Launch the application
    7.          *
    8.          * @param args
    9.          */
    10.    public static void main(String args[]) {
    11.     try {
    12.           DemoWindow win = new DemoWindow();
    13.           win.setBounds(100, 100, 400, 300);
    14.           win.setTitle("打造专业外观-Swing");
    15. win.setIcon(new ImageIcon(DemoWindow.class.getResource("title.png")));
    16.       win.setVisible(true);
    17.                 } catch (Exception e) {
    18.                         e.printStackTrace();
    19.                 }
    20.         }
    21. }
    复制代码

      
       
       
         
       

         
       
      
    1. 代码2:DemoWindow.java
    2. package demo.swing;
    3. import java.awt.Color;
    4. import java.awt.Cursor;
    5. import java.awt.Dimension;
    6. import java.awt.FontMetrics;
    7. import java.awt.GradientPaint;
    8. import java.awt.Graphics;
    9. import java.awt.Graphics2D;
    10. import java.awt.Point;
    11. import java.awt.event.ActionEvent;
    12. import java.awt.event.ActionListener;
    13. import java.awt.event.ComponentEvent;
    14. import java.awt.event.ComponentListener;
    15. import java.awt.event.MouseEvent;
    16. import java.awt.event.MouseListener;
    17. import java.awt.event.MouseMotionListener;
    18. import javax.swing.BorderFactory;
    19. import javax.swing.Icon;
    20. import javax.swing.ImageIcon;
    21. import javax.swing.JButton;
    22. import javax.swing.JComponent;
    23. import javax.swing.JWindow;
    24. public class DemoWindow extends JWindow implements MouseListener,
    25.                 MouseMotionListener, ComponentListener, ActionListener {
    26.         private Icon northwestIcon;
    27.         private Icon northeastIcon;
    28.         private Icon northIcon;
    29.         private Icon southwestIcon;
    30.         private Icon southeastIcon;
    31.         private Icon southIcon;
    32.         private Icon westIcon;
    33.         private Icon eastIcon;
    34.         private Icon closeIcon;
    35.         private Icon closeoverIcon;
    36.         private Icon closedownIcon;
    37.         // point
    38.         private Point location;
    39.         private Point size;
    40.         // cursor
    41.         private Cursor sizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
    42.         private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
    43.         private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    44.         //
    45.         private ContentPane contentPane;
    46.         private JButton closeButton;
    47.         // color
    48.         private Color color1 = new Color(254, 254, 254);
    49.         private Color color2 = new Color(204, 220, 247);
    50.         private Color titleColor = new Color(128, 128, 128);
    51.         // title bar
    52.         private String title;
    53.         private Icon icon;
    54.         /**
    55.          * Create the frame
    56.          */
    57.         public DemoWindow() {
    58.                 super();
    59.                 initIcon();
    60.                 initComponent();
    61.                 contentPane.addComponentListener(this);
    62.                 contentPane.addMouseListener(this);
    63.                 contentPane.addMouseMotionListener(this);
    64.                 //
    65.         }
    66.         public void setTitle(String title) {
    67.                 this.title = title;
    68.                 contentPane.repaint();
    69.         }
    70.         public void setIcon(Icon icon) {
    71.                 this.icon = icon;
    72.         }
    73.         private void initComponent() {
    74.                 contentPane = new ContentPane();
    75.                 setContentPane(contentPane);
    76.                 closeButton = new JButton();
    77.                 closeButton.setBorder(BorderFactory.createEmptyBorder());
    78.                 closeButton.setIcon(closeIcon);
    79.                 closeButton.setRolloverIcon(closeoverIcon);
    80.                 closeButton.setPressedIcon(closedownIcon);
    81.                 closeButton.addActionListener(this);
    82.                 contentPane.add(closeButton);
    83.         }
    84.         private void initIcon() {
    85.                 northwestIcon = new ImageIcon(DemoWindow.class.getResource("northwest.png"));
    86.                 northeastIcon = new ImageIcon(DemoWindow.class.getResource("northeast.png"));
    87.                 northIcon = new ImageIcon(DemoWindow.class.getResource("north.png"));
    88.                 southwestIcon = new ImageIcon(DemoWindow.class.getResource("southwest.png"));
    89.                 southeastIcon = new ImageIcon(DemoWindow.class.getResource("southeast.png"));
    90.                 southIcon = new ImageIcon(DemoWindow.class.getResource("south.png"));
    91.                 westIcon = new ImageIcon(DemoWindow.class.getResource("west.png"));
    92.                 eastIcon = new ImageIcon(DemoWindow.class.getResource("east.png"));
    93.                 closeIcon = new ImageIcon(DemoWindow.class.getResource("close.png"));
    94.                 closeoverIcon = new ImageIcon(DemoWindow.class.getResource("closeover.png"));
    95.                 closedownIcon = new ImageIcon(DemoWindow.class.getResource("closedown.png"));
    96.         }
    97.         private class ContentPane extends JComponent {
    98.                 @Override
    99.                 protected void paintComponent(Graphics g) {
    100.                         int w = getWidth();
    101.                         int h = getHeight();
    102.                         northwestIcon.paintIcon(this, g, 0, 0);
    103.                         northeastIcon.paintIcon(this, g, w - northeastIcon.getIconWidth(),0);
    104.                         g.drawImage(((ImageIcon) northIcon).getImage(), northwestIcon
    105.                                         .getIconWidth(), 0, w - northeastIcon.getIconWidth(),
    106.                                         northIcon.getIconHeight(), 0, 0, northIcon.getIconWidth(),
    107.                                         northIcon.getIconHeight(), this);
    108.                         southwestIcon.paintIcon(this, g, 0, h- southwestIcon.getIconHeight());
    109.                         southeastIcon.paintIcon(this, g, w - southeastIcon.getIconWidth(),
    110.                                         h - southeastIcon.getIconHeight());
    111.                         g.drawImage(((ImageIcon) southIcon).getImage(), southwestIcon
    112.                                         .getIconWidth(), h - southIcon.getIconHeight(), w
    113.                                         - southeastIcon.getIconWidth(), h, 0, 0, southIcon
    114.                                         .getIconWidth(), southIcon.getIconHeight(), this);
    115.                         g.drawImage(((ImageIcon) westIcon).getImage(), 0, northeastIcon
    116.                                         .getIconHeight(), westIcon.getIconWidth(), h
    117.                                         - southwestIcon.getIconHeight(), 0, 0, westIcon
    118.                                         .getIconWidth(), westIcon.getIconHeight(), this);
    119.                         g.drawImage(((ImageIcon) eastIcon).getImage(), w
    120.                                         - eastIcon.getIconWidth(), northeastIcon.getIconHeight(),
    121.                                         w, h - southeastIcon.getIconHeight(), 0, 0, eastIcon
    122.                                         .getIconWidth(), eastIcon.getIconHeight(), this);
    123.                         GradientPaint gp = new GradientPaint(0, 0, color1, 0, h - 1, color2);
    124.                         ((Graphics2D) g).setPaint(gp);
    125.                         g.fillRect(westIcon.getIconWidth(), northIcon.getIconHeight(), w
    126.                                         - westIcon.getIconWidth() - eastIcon.getIconWidth(), h
    127.                                         - northIcon.getIconHeight() - southIcon.getIconHeight());
    128.                         // title
    129.                         if (title == null) {
    130.                                 title = "";
    131.                         }
    132.                         g.setColor(titleColor);
    133.                         FontMetrics fm = getFontMetrics(getFont());
    134.                         g.drawString(title, (getWidth() - fm.stringWidth(title)) / 2,
    135.                                         northIcon.getIconHeight() / 2 + fm.getHeight() / 4);
    136.                         if (icon != null) {
    137.                                 icon.paintIcon(contentPane, g, getWidth() / 2
    138.                                                 - fm.stringWidth(title) / 2 - icon.getIconWidth() - 10,
    139.                                                 northIcon.getIconHeight() / 2 - icon.getIconHeight()
    140.                                                                 / 2);
    141.                         }
    142.                 }
    143.         }
    144.         public void mouseClicked(MouseEvent e) {
    145.         }
    146.         public void mouseEntered(MouseEvent e) {
    147.         }
    148.         public void mouseExited(MouseEvent e) {
    149.         }
    150.         public void mousePressed(MouseEvent e) {
    151.                 int x = e.getX();
    152.                 int y = e.getY();
    153.                 int w = contentPane.getWidth();
    154.                 int h = contentPane.getHeight();
    155.                 if (x > northwestIcon.getIconWidth()
    156.                                 && x < contentPane.getWidth() - northeastIcon.getIconWidth()
    157.                                 && y > 0 && y < northIcon.getIconHeight()) {
    158.                         location = e.getLocationOnScreen();
    159.                 } else if (x > w - southeastIcon.getIconWidth() && x < w
    160.                                 && y > h - southeastIcon.getIconHeight() && y < h) {
    161.                         size = e.getLocationOnScreen();
    162.                 }
    163.         }
    164.         public void mouseReleased(MouseEvent e) {
    165.                 location = null;
    166.                 if (size != null) {
    167.                         try {
    168.                                 Point newPoint = e.getLocationOnScreen();
    169.                                 Dimension d = new Dimension(getSize().width + newPoint.x
    170.                                                 - size.x, getSize().height + newPoint.y - size.y);
    171.                                 if (d.width > 100 && d.height > 50) {
    172.                                         setSize(d);
    173.                                 }
    174.                         } finally {
    175.                                 size = null;
    176.                         }
    177.                 }
    178.         }
    179.         public void mouseDragged(MouseEvent e) {
    180.                 if (location != null) {
    181.                         Point newPoint = e.getLocationOnScreen();
    182.                         setLocation(getLocation().x + newPoint.x - location.x,
    183.                                         getLocation().y + newPoint.y - location.y);
    184.                         location = newPoint;
    185.                 }
    186.         }
    187.         public void mouseMoved(MouseEvent e) {
    188.                 int x = e.getX();
    189.                 int y = e.getY();
    190.                 int w = contentPane.getWidth();
    191.                 int h = contentPane.getHeight();
    192.                 if (x > w - southeastIcon.getIconWidth() && x < w
    193.                                 && y > h - southeastIcon.getIconHeight() && y < h) {
    194.                         if (contentPane.getCursor() != sizeCursor) {
    195.                                 contentPane.setCursor(sizeCursor);
    196.                         }
    197.                 } else if (x > northwestIcon.getIconWidth()
    198.                                 && x < contentPane.getWidth() - northeastIcon.getIconWidth()
    199.                                 && y > 0 && y < northIcon.getIconHeight()) {
    200.                         if (contentPane.getCursor() != moveCursor) {
    201.                                 contentPane.setCursor(moveCursor);
    202.                         }
    203.                 } else {
    204.                         if (contentPane.getCursor() != defaultCursor) {
    205.                                 contentPane.setCursor(defaultCursor);
    206.                         }
    207.                 }
    208.         }
    209.         //
    210.         public void componentHidden(ComponentEvent e) {
    211.         }
    212.         public void componentMoved(ComponentEvent e) {
    213.         }
    214.         public void componentResized(ComponentEvent e) {
    215.                 closeButton.setBounds(contentPane.getWidth()
    216.                                 - northeastIcon.getIconWidth() - closeIcon.getIconWidth(), 0,
    217.                                 closeIcon.getIconWidth(), closeIcon.getIconHeight());
    218.         }
    219.         public void componentShown(ComponentEvent e) {
    220.         }
    221.         public void actionPerformed(ActionEvent e) {
    222.                 dispose();
    223.         }
    224. }

    复制代码
         用Swing实现不足的地方在于顶层窗口,截至到JDK1.6发布,AWT尚未支持不规则矩形窗体和窗口半透明。
    有好消息称,JDK1.7中将实现。本程序需要在JDK1.6下编译,因为用了一些新增的API,如果要改成1.6以前版
    本实现也不难。但是在JRE1.6下运行,性能会明显提高。



      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/10/31/235542437.zip
    回复

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2024-5-18 21:29 , Processed in 0.345505 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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