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

[Swing学习]学习EventQueue二例

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

    [LV.1]初来乍到

    发表于 2014-11-9 23:56:59 | 显示全部楼层 |阅读模式
    一、在EventQueue中检索事件
    1. import java.awt.AWTEvent;   
    2. import java.awt.Container;   
    3. import java.awt.EventQueue;   
    4. import java.awt.Frame;   
    5. import java.awt.Graphics;   
    6. import java.awt.Point;   
    7. import java.awt.Toolkit;   
    8. import java.awt.event.ActionEvent;   
    9. import java.awt.event.ActionListener;   
    10. import java.awt.event.MouseEvent;   
    11. import java.awt.event.WindowAdapter;   
    12. import java.awt.event.WindowEvent;   
    13.    
    14. import javax.swing.JButton;   
    15. import javax.swing.JFrame;   
    16. import javax.swing.JPanel;   
    17.    
    18. public class EventQueuePanel extends JPanel implements ActionListener {   
    19.   EventQueuePanel() {   
    20.     JButton button = new JButton("Draw line");   
    21.     add(button);   
    22.     button.addActionListener(this);   
    23.   }   
    24.    
    25.   public void actionPerformed(ActionEvent evt) {   
    26.     Graphics g = getGraphics();   
    27.    
    28.     displayPrompt(g, "Click to chooose the first point");   
    29.     Point p = getClick();   
    30.     g.drawOval(p.x - 2, p.y - 2, 4, 4);   
    31.     displayPrompt(g, "Click to choose the second point");   
    32.     Point q = getClick();   
    33.     g.drawOval(q.x - 2, q.y - 2, 4, 4);   
    34.     g.drawLine(p.x, p.y, q.x, q.y);   
    35.     displayPrompt(g, "Done! Press button the start again.");   
    36.     g.dispose();   
    37.   }   
    38.    
    39.   public void displayPrompt(Graphics g, String s) {   
    40.     y += 20;   
    41.     g.drawString(s, 0, y);   
    42.   }   
    43.    
    44.   public Point getClick() {   
    45.     EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();   
    46.     while (true) {   
    47.       try {   
    48.         AWTEvent evt = eq.getNextEvent();   
    49.         if (evt.getID() == MouseEvent.MOUSE_PRESSED) {   
    50.           MouseEvent mevt = (MouseEvent) evt;   
    51.           Point p = mevt.getPoint();   
    52.           Point top = getRootPane().getLocation();   
    53.           p.x -= top.x;   
    54.           p.y -= top.y;   
    55.           return p;   
    56.         }   
    57.       } catch (InterruptedException e) {   
    58.       }   
    59.     }   
    60.   }   
    61.    
    62.   private int y = 60;   
    63.   public static void main(String[] args) {   
    64.     JFrame frame = new JFrame();   
    65.     frame.setTitle("EventQueueTest");   
    66.     frame.setSize(300, 200);   
    67.     frame.addWindowListener(new WindowAdapter() {   
    68.       public void windowClosing(WindowEvent e) {   
    69.         System.exit(0);   
    70.       }   
    71.     });   
    72.    
    73.     Container contentPane = frame.getContentPane();   
    74.     contentPane.add(new EventQueuePanel());   
    75.    
    76.     frame.setVisible(true);
    77.   }   
    78.    
    79. }   
    80. [img]http://img.javaxxz.com/2014/11/9/235659171.gif[/img]
    81. 二、批派任务
    82. import java.awt.EventQueue;
    83. import java.awt.event.*;
    84. import java.io.*;
    85. import javax.swing.*;
    86. /**
    87. * A program for viewing images.
    88. * @version 1.22 2007-05-21
    89. * @author Cay Horstmann
    90. */
    91. public class ImageViewer
    92. {
    93.    public static void main(String[] args)
    94.    {
    95.       EventQueue.invokeLater(new Runnable()
    96.          {
    97.             public void run()
    98.             {
    99.                JFrame frame = new ImageViewerFrame();
    100.                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    101.                frame.setVisible(true);
    102.             }
    103.          });
    104.    }
    105. }
    106. /**
    107. * A frame with a label to show an image.
    108. */
    109. class ImageViewerFrame extends JFrame
    110. {
    111.    public ImageViewerFrame()
    112.    {
    113.       setTitle("ImageViewer");
    114.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    115.       // use a label to display the images
    116.       label = new JLabel();
    117.       add(label);
    118.       // set up the file chooser
    119.       chooser = new JFileChooser();
    120.       chooser.setCurrentDirectory(new File("."));
    121.       // set up the menu bar
    122.       JMenuBar menuBar = new JMenuBar();
    123.       setJMenuBar(menuBar);
    124.       JMenu menu = new JMenu("File");
    125.       menuBar.add(menu);
    126.       JMenuItem openItem = new JMenuItem("Open");
    127.       menu.add(openItem);
    128.       openItem.addActionListener(new ActionListener()
    129.          {
    130.             public void actionPerformed(ActionEvent event)
    131.             {
    132.                // show file chooser dialog
    133.                int result = chooser.showOpenDialog(null);
    134.                // if file selected, set it as icon of the label
    135.                if (result == JFileChooser.APPROVE_OPTION)
    136.                {
    137.                   String name = chooser.getSelectedFile().getPath();
    138.                   label.setIcon(new ImageIcon(name));
    139.                }
    140.             }
    141.          });
    142.       JMenuItem exitItem = new JMenuItem("Exit");
    143.       menu.add(exitItem);
    144.       exitItem.addActionListener(new ActionListener()
    145.          {
    146.             public void actionPerformed(ActionEvent event)
    147.             {
    148.                System.exit(0);
    149.             }
    150.          });
    151.    }
    152.    private JLabel label;
    153.    private JFileChooser chooser;
    154.    private static final int DEFAULT_WIDTH = 300;
    155.    private static final int DEFAULT_HEIGHT = 400;
    156. }
    复制代码



       
         
         
          
          

            
          

            
          
         
       

      


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

    使用道具 举报

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

    本版积分规则

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

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

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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