`
jiasudu1649
  • 浏览: 719611 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Using Swing and AWT in Swt

阅读更多
一个awt做的模拟时钟的例子(代码来源Java2程序设计150例)
很简单,应该很容易看懂,
第二个例子在第一例子的基础上进行了小修改在swt上运行awt,swing。
java 代码
 
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.awt.geom.*;  
  4. import java.util.Calendar;  
  5. import javax.swing.*;  
  6.   
  7. public class Clock extends JPanel implements ActionListener  
  8. {  
  9.     // 创建时钟的外形  
  10.     protected static Ellipse2D face = new Ellipse2D.Float(339494);  
  11.     // 创建时钟的标记  
  12.     protected static GeneralPath tick = new GeneralPath();  
  13.     static  
  14.     {  
  15.         tick.moveTo(100100);  
  16.         tick.moveTo(490);  
  17.         tick.lineTo(510);  
  18.         tick.lineTo(516);  
  19.         tick.lineTo(496);  
  20.         tick.lineTo(490);  
  21.     }  
  22.     // 创建时针  
  23.     protected static GeneralPath hourHand = new GeneralPath();  
  24.     static  
  25.     {  
  26.         hourHand.moveTo(5015);  
  27.         hourHand.lineTo(5350);  
  28.         hourHand.lineTo(5053);  
  29.         hourHand.lineTo(4750);  
  30.         hourHand.lineTo(5015);  
  31.     }  
  32.     // 创建分针  
  33.     protected static GeneralPath minuteHand = new GeneralPath();  
  34.     static  
  35.     {  
  36.         minuteHand.moveTo(502);  
  37.         minuteHand.lineTo(5350);  
  38.         minuteHand.lineTo(5058);  
  39.         minuteHand.lineTo(4750);  
  40.         minuteHand.lineTo(502);  
  41.     }  
  42.     // 创建秒针  
  43.     protected static GeneralPath secondHand = new GeneralPath();  
  44.     static  
  45.     {  
  46.         secondHand.moveTo(495);  
  47.         secondHand.lineTo(515);  
  48.         secondHand.lineTo(5162);  
  49.         secondHand.lineTo(4962);  
  50.         secondHand.lineTo(495);  
  51.     }  
  52.     // 设置时钟的颜色  
  53.     protected static Color faceColor = new Color(220220220);  
  54.     protected static Color hourColor = Color.red.darker();  
  55.     protected static Color minuteColor = Color.blue.darker();  
  56.     protected static Color secondColor = new Color(1801800);  
  57.     protected static Color pinColor = Color.gray.brighter();  
  58.     // 创建时钟的枢纽  
  59.     protected Ellipse2D pivot = new Ellipse2D.Float(474766);  
  60.     protected Ellipse2D centerPin = new Ellipse2D.Float(494922);  
  61.   
  62.     // 创建绕时钟枢纽转的变换  
  63.     protected AffineTransform hourTransform =  
  64.                     AffineTransform.getRotateInstance(05050);  
  65.     protected AffineTransform minuteTransform =  
  66.                     AffineTransform.getRotateInstance(05050);  
  67.     protected AffineTransform secondTransform =  
  68.                     AffineTransform.getRotateInstance(05050);  
  69.     // 创建每秒触发一次的Timer  
  70.     protected Timer timer = new Timer(1000this);  
  71.     protected Calendar calendar = Calendar.getInstance();  
  72.   
  73.     public Clock()  
  74.     {  
  75.         setPreferredSize(new Dimension(100100));  
  76.     }  
  77.     // 当plane加入container中时发生  
  78.     public void addNotify()  
  79.     {  
  80.         super.addNotify();  
  81.         timer.start();  
  82.     }  
  83.     // 当plane从container中移处时发生  
  84.     public void removeNotify()  
  85.     {  
  86.         timer.stop();  
  87.         super.removeNotify();  
  88.     }  
  89.   
  90.     public void actionPerformed(ActionEvent event)  
  91.     {  
  92.         // 更新calender的时间  
  93.         this.calendar.setTime(new java.util.Date());  
  94.         int hours = this.calendar.get(Calendar.HOUR);  
  95.         int minutes = this.calendar.get(Calendar.MINUTE);  
  96.         int seconds = this.calendar.get(Calendar.SECOND);  
  97.   
  98.         //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度  
  99.         hourTransform.setToRotation(((double) hours) *  
  100.                                             (Math.PI / 6.0), 5050);  
  101.         minuteTransform.setToRotation(((double) minutes) *  
  102.                                             (Math.PI / 30.0), 5050);  
  103.         secondTransform.setToRotation(((double) seconds) *  
  104.                                             (Math.PI / 30.0), 5050);  
  105.         repaint();  
  106.     }  
  107.   
  108.     public void paint(Graphics g)  
  109.     {  
  110.         super.paint(g);  
  111.   
  112.         // 得到图形上下文和抗锯齿处理  
  113.         Graphics2D g2 = (Graphics2D) g;  
  114.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  115.                             RenderingHints.VALUE_ANTIALIAS_ON);  
  116.         g2.setPaint(faceColor);  
  117.         g2.fill(face);  
  118.         g2.setPaint(Color.black);  
  119.         g2.draw(face);  
  120.   
  121.         // 产生钟面上12个滴答位置  
  122.         for (double p = 0.0; p < 12.0; p += 1.0)  
  123.         {  
  124.             //利用变换画出同心的滴答的标线  
  125.             g2.fill(tick.createTransformedShape(  
  126.                 AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,  
  127.                         5050)));  
  128.         }  
  129.         g2.setPaint(hourColor);  
  130.         g2.fill(hourHand.createTransformedShape(hourTransform));  
  131.         g2.setPaint(minuteColor);  
  132.         g2.fill(minuteHand.createTransformedShape(minuteTransform));  
  133.         g2.setPaint(secondColor);  
  134.         g2.fill(secondHand.createTransformedShape(secondTransform));  
  135.         g2.fill(pivot);  
  136.         g2.setPaint(pinColor);  
  137.         g2.fill(centerPin);  
  138.     }  
  139.   
  140.     public static void main(String[] args)  
  141.     {  
  142.         JFrame frame = new JFrame();  
  143.         frame.setLocation(700400);  
  144.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  145.         frame.getContentPane().add(new Clock());  
  146.         frame.pack();  
  147.         frame.show();  
  148.     }  
  149. }  


我把例子做了一些修改让他在swt的基础上运行。
java 代码
 
  1. import java.awt.Color;  
  2. import java.awt.Dimension;  
  3. import java.awt.Graphics;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.RenderingHints;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.geom.AffineTransform;  
  9. import java.awt.geom.Ellipse2D;  
  10. import java.awt.geom.GeneralPath;  
  11. import java.util.Calendar;  
  12.   
  13. import javax.swing.JPanel;  
  14. import javax.swing.Timer;  
  15.   
  16. import org.eclipse.swt.SWT;  
  17. import org.eclipse.swt.awt.SWT_AWT;  
  18. import org.eclipse.swt.layout.FillLayout;  
  19. import org.eclipse.swt.widgets.Composite;  
  20. import org.eclipse.swt.widgets.Display;  
  21. import org.eclipse.swt.widgets.Shell;  
  22.   
  23. public class Swt_awtClock extends JPanel implements ActionListener{  
  24.   
  25.         // 创建时钟的外形  
  26.         protected static Ellipse2D face = new Ellipse2D.Float(339494);  
  27.         // 创建时钟的标记  
  28.         protected static GeneralPath tick = new GeneralPath();  
  29.         static  
  30.         {  
  31.             tick.moveTo(100100);  
  32.             tick.moveTo(490);  
  33.             tick.lineTo(510);  
  34.             tick.lineTo(516);  
  35.             tick.lineTo(496);  
  36.             tick.lineTo(490);  
  37.         }  
  38.         // 创建时针  
  39.         protected static GeneralPath hourHand = new GeneralPath();  
  40.         static  
  41.         {  
  42.             hourHand.moveTo(5015);  
  43.             hourHand.lineTo(5350);  
  44.             hourHand.lineTo(5053);  
  45.             hourHand.lineTo(4750);  
  46.             hourHand.lineTo(5015);  
  47.         }  
  48.         // 创建分针  
  49.         protected static GeneralPath minuteHand = new GeneralPath();  
  50.         static  
  51.         {  
  52.             minuteHand.moveTo(502);  
  53.             minuteHand.lineTo(5350);  
  54.             minuteHand.lineTo(5058);  
  55.             minuteHand.lineTo(4750);  
  56.             minuteHand.lineTo(502);  
  57.         }  
  58.         // 创建秒针  
  59.         protected static GeneralPath secondHand = new GeneralPath();  
  60.         static  
  61.         {  
  62.             secondHand.moveTo(495);  
  63.             secondHand.lineTo(515);  
  64.             secondHand.lineTo(5162);  
  65.             secondHand.lineTo(4962);  
  66.             secondHand.lineTo(495);  
  67.         }  
  68.         // 设置时钟的颜色  
  69.         protected static Color faceColor = new Color(220220220);  
  70.         protected static Color hourColor = Color.red.darker();  
  71.         protected static Color minuteColor = Color.blue.darker();  
  72.         protected static Color secondColor = new Color(1801800);  
  73.         protected static Color pinColor = Color.gray.brighter();  
  74.         // 创建时钟的枢纽  
  75.         protected Ellipse2D pivot = new Ellipse2D.Float(474766);  
  76.         protected Ellipse2D centerPin = new Ellipse2D.Float(494922);  
  77.   
  78.         // 创建绕时钟枢纽转的变换  
  79.         protected AffineTransform hourTransform =  
  80.                         AffineTransform.getRotateInstance(05050);  
  81.         protected AffineTransform minuteTransform =  
  82.                         AffineTransform.getRotateInstance(05050);  
  83.         protected AffineTransform secondTransform =  
  84.                         AffineTransform.getRotateInstance(05050);  
  85.         // 创建每秒触发一次的Timer  
  86.         protected Timer timer = new Timer(1000this);  
  87.         protected Calendar calendar = Calendar.getInstance();  
  88.   
  89.         public Swt_awtClock()  
  90.         {  
  91.             setPreferredSize(new Dimension(100100));  
  92.         }  
  93.         // 当plane加入container中时发生  
  94.         public void addNotify()  
  95.         {  
  96.             super.addNotify();  
  97.             timer.start();  
  98.         }  
  99.         // 当plane从container中移处时发生  
  100.         public void removeNotify()  
  101.         {  
  102.             timer.stop();  
  103.             super.removeNotify();  
  104.         }  
  105.   
  106.         public void actionPerformed(ActionEvent event)  
  107.         {  
  108.             // 更新calender的时间  
  109.             this.calendar.setTime(new java.util.Date());  
  110.             int hours = this.calendar.get(Calendar.HOUR);  
  111.             int minutes = this.calendar.get(Calendar.MINUTE);  
  112.             int seconds = this.calendar.get(Calendar.SECOND);  
  113.   
  114.             //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度  
  115.             hourTransform.setToRotation(((double) hours) *  
  116.                                                 (Math.PI / 6.0), 5050);  
  117.             minuteTransform.setToRotation(((double) minutes) *  
  118.                                                 (Math.PI / 30.0), 5050);  
  119.             secondTransform.setToRotation(((double) seconds) *  
  120.                                                 (Math.PI / 30.0), 5050);  
  121.             repaint();  
  122.         }  
  123.   
  124.         public void paint(Graphics g)  
  125.         {  
  126.             super.paint(g);  
  127.   
  128.             // 得到图形上下文和抗锯齿处理  
  129.             Graphics2D g2 = (Graphics2D) g;  
  130.             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  131.                                 RenderingHints.VALUE_ANTIALIAS_ON);  
  132.             g2.setPaint(faceColor);  
  133.             g2.fill(face);  
  134.             g2.setPaint(Color.black);  
  135.             g2.draw(face);  
  136.   
  137.             // 产生钟面上12个滴答位置  
  138.             for (double p = 0.0; p < 12.0; p += 1.0)  
  139.             {  
  140.                 //利用变换画出同心的滴答的标线  
  141.                 g2.fill(tick.createTransformedShape(  
  142.                     AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,  
  143.                             5050)));  
  144.             }  
  145.             g2.setPaint(hourColor);  
  146.             g2.fill(hourHand.createTransformedShape(hourTransform));  
  147.             g2.setPaint(minuteColor);  
  148.             g2.fill(minuteHand.createTransformedShape(minuteTransform));  
  149.             g2.setPaint(secondColor);  
  150.             g2.fill(secondHand.createTransformedShape(secondTransform));  
  151.             g2.fill(pivot);  
  152.             g2.setPaint(pinColor);  
  153.             g2.fill(centerPin);  
  154.         }  
  155.   
  156.     public static void main(String[] args) {  
  157.         final Display display = new Display( );  
  158.         final Shell shell = new Shell(display);  
  159.         shell.setText("Using Swing and AWT in Swt");  
  160.   
  161.         shell.setSize(135145);  
  162.         shell.setLayout(new FillLayout());  
  163.   
  164.         Composite composite = new Composite(shell, SWT.EMBEDDED);  
  165.   
  166.          java.awt.Frame frame = SWT_AWT.new_Frame(composite);  
  167.   
  168.         frame.setLocation(700400);  
  169.   
  170. //        frame.getContentPane().add(new Swt_awtClock());  
  171.         java.awt.Panel panel = new java.awt.Panel( );  
  172.         frame.add(panel);  
  173.         panel.add(new Swt_awtClock());  
  174.         frame.pack();  
  175.         frame.show();  
  176.   
  177.         shell.open( );  
  178.         while(!shell.isDisposed( )) {  
  179.             if (!display.readAndDispatch( )) display.sleep( );  
  180.         }  
  181.         display.dispose( );  
  182.     }  
  183. }  
分享到:
评论
1 楼 rEloaD_cn 2008-01-11  
恩,关键就只有两行代码:
#         Composite composite = new Composite(shell, SWT.EMBEDDED); 
#  
#          java.awt.Frame frame = SWT_AWT.new_Frame(composite); 

相关推荐

    在eclipse下搭建swing/awt/swt开发环境的方法和插件

    在Java编程领域,Swing、AWT和SWT都是用于构建图形用户界面(GUI)的框架。本篇文章将详细介绍如何在Eclipse集成开发环境中(IDE)搭建这些框架的开发环境,以及可以使用的相关插件。 首先,让我们从AWT(Abstract ...

    浅析Java中Swing与SWT和AWT的区别及实现_王亚南

    Java是一种广泛使用的编程语言,在图形用户界面(GUI)设计领域提供了多种开发包,其中比较著名的有Swing、SWT和AWT。这三种技术各有特点和适用场景,在开发Java图形界面应用时,开发者需要根据实际需求选择最合适的...

    SWT Swing与AWT区别

    【SWT、Swing、AWT 之间的差异】 在Java GUI开发中,SWT(Standard Widget Toolkit)、Swing和AWT(Abstract Window Toolkit)是三种常见的工具包。它们各自拥有不同的特性和优缺点,适用于不同场景。 **AWT...

    swt swing awt的区别

    swt swing awt的区别

    Java界面(Swing And AWT)非常非常好的一份资料

    ### Java界面(Swing And AWT)非常非常好的一份资料 #### 图形用户界面(GUI)概览 在当今的应用程序开发领域中,图形用户界面(Graphical User Interface, GUI)已成为用户与计算机交互的主要方式之一。它通过...

    java作业 简单的摇骰子游戏、使用线程和swing、awt.zip

    简单的摇骰子游戏、使用线程和swing、awt.zip java作业。简单的摇骰子游戏、使用线程和swing、awt.zip java作业。简单的摇骰子游戏、使用线程和swing、awt.zip java作业。简单的摇骰子游戏、使用线程和swing、awt....

    图形界面开发--AWT,Swing,SWT

    ### 图形界面开发——AWT、Swing、SWT #### 18.1 图形界面简介 **18.1.1 概述** 在介绍AWT、Swing、SWT之前,我们先来简单了解一下图形用户界面(GUI)的概念及其在Java开发中的重要性。随着计算机技术的发展,用户...

    Java GUI SWT/Swing/AWT的介绍及比较

    它提供了三个主要的工具包:SWT(Standard Widget Toolkit)、Swing和AWT(Abstract Window Toolkit)。这三个工具包各有特点,适用于不同的开发场景。 AWT是Java最早的GUI工具包,它是Java平台的基础,包含了基本...

    java计算器,swing和awt

    ### Java计算器:Swing与AWT的综合应用 在探讨如何使用Java开发一个具备基本运算功能(加、减、乘、除以及开平方根)的计算器之前,我们首先需要了解Swing与AWT这两个用于图形用户界面(GUI)设计的框架。 #### AWT ...

    java swing\awt开发的坦克游戏

    【Java Swing 和 AWT 坦克游戏开发详解】 在Java编程领域,Swing和AWT是用于构建图形用户界面(GUI)的两个主要库。本项目“java swing\awt开发的坦克游戏”旨在通过实际操作,帮助开发者了解和掌握这两个库在游戏...

    Swing,AWT.J2SE 学习帮助文档

    Swing和AWT是Java Standard Edition (J2SE)中的两个关键图形用户界面(GUI)工具包,用于创建桌面应用程序。这两个组件库都是Java平台的一部分,但它们在设计和功能上有所不同,为开发者提供了不同的选择。 AWT...

    swt_awt桥接 swt_awt桥接

    今天遇到一个问题,就是要在一个Eclipse插件里显示JFreeChart的图形,因为后者是基于Java2D的,要把图形显示在SWT应用程序里需要利用SWT-AWT桥接器来实现,虽说桥接的方式多半会伴随着性能下降,但总归是一个解决...

    AWT与Swing使用区别

    - **Swing**:Swing组件可以通过自定义外观(LookAndFeel)来实现跨平台的统一界面风格,同时提供了更多装饰和动画效果,使应用程序看起来更现代。 8. **编程注意事项** - **线程操作**:对Swing组件的线程操作...

    swing_awt_sqlserver陶瓷工厂进销存管理系统的设计与实现(源码+数据库sql+lun文+视频齐全).zip

    cs架构swing_awt_sqlserver陶瓷工厂进销存管理系统的设计与实现(源码+数据库sql+lun文+视频齐全)cs架构swing_awt_sqlserver陶瓷工厂进销存管理系统的设计与实现(源码+数据库sql+lun文+视频齐全)cs架构swing_awt_sql...

    如何在SWT中使用AWT、SWING组件

    在Java编程环境中,SWT(Standard Widget Toolkit)和AWT(Abstract Window Toolkit)以及SWING是三种常用的图形用户界面(GUI)开发库。SWT主要为Eclipse IDE提供支持,而AWT和SWING则是Java标准库的一部分。这篇...

    java 图形界面 java awt swt swing gui

    在Java中,有三个主要的GUI工具包:AWT(Abstract Window Toolkit),SWT(Standard Widget Toolkit)和Swing。 1. AWT(Abstract Window Toolkit): AWT是Java最早提供的GUI库,它是Java的基础GUI组件集合。AWT...

    Ecplise Swing AWT开发插件一(VE)

    在Java图形用户界面(GUI)开发中,Swing和AWT是非常重要的库,它们提供了丰富的组件和功能来创建桌面应用程序。本篇文章将围绕"Eclipse Swing AWT开发插件一(VE)"这一主题展开,详细讲解该插件及其相关知识点。 ...

    swing基础学习和awt的区别

    Swing和AWT是Java GUI(图形用户界面)编程中的两个重要框架,它们都是用于创建桌面应用程序的组件库。然而,两者在设计理念、组件类型、性能和外观方面存在显著差异。 首先,AWT(Abstract Window Toolkit)是Java...

Global site tag (gtag.js) - Google Analytics