TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一、在EventQueue中检索事件
- import java.awt.AWTEvent;
- import java.awt.Container;
- import java.awt.EventQueue;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
-
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
-
- public class EventQueuePanel extends JPanel implements ActionListener {
- EventQueuePanel() {
- JButton button = new JButton("Draw line");
- add(button);
- button.addActionListener(this);
- }
-
- public void actionPerformed(ActionEvent evt) {
- Graphics g = getGraphics();
-
- displayPrompt(g, "Click to chooose the first point");
- Point p = getClick();
- g.drawOval(p.x - 2, p.y - 2, 4, 4);
- displayPrompt(g, "Click to choose the second point");
- Point q = getClick();
- g.drawOval(q.x - 2, q.y - 2, 4, 4);
- g.drawLine(p.x, p.y, q.x, q.y);
- displayPrompt(g, "Done! Press button the start again.");
- g.dispose();
- }
-
- public void displayPrompt(Graphics g, String s) {
- y += 20;
- g.drawString(s, 0, y);
- }
-
- public Point getClick() {
- EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
- while (true) {
- try {
- AWTEvent evt = eq.getNextEvent();
- if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
- MouseEvent mevt = (MouseEvent) evt;
- Point p = mevt.getPoint();
- Point top = getRootPane().getLocation();
- p.x -= top.x;
- p.y -= top.y;
- return p;
- }
- } catch (InterruptedException e) {
- }
- }
- }
-
- private int y = 60;
- public static void main(String[] args) {
- JFrame frame = new JFrame();
- frame.setTitle("EventQueueTest");
- frame.setSize(300, 200);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
-
- Container contentPane = frame.getContentPane();
- contentPane.add(new EventQueuePanel());
-
- frame.setVisible(true);
- }
-
- }
- [img]http://img.javaxxz.com/2014/11/9/235659171.gif[/img]
- 二、批派任务
- import java.awt.EventQueue;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.*;
- /**
- * A program for viewing images.
- * @version 1.22 2007-05-21
- * @author Cay Horstmann
- */
- public class ImageViewer
- {
- public static void main(String[] args)
- {
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- JFrame frame = new ImageViewerFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- });
- }
- }
- /**
- * A frame with a label to show an image.
- */
- class ImageViewerFrame extends JFrame
- {
- public ImageViewerFrame()
- {
- setTitle("ImageViewer");
- setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
- // use a label to display the images
- label = new JLabel();
- add(label);
- // set up the file chooser
- chooser = new JFileChooser();
- chooser.setCurrentDirectory(new File("."));
- // set up the menu bar
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- JMenu menu = new JMenu("File");
- menuBar.add(menu);
- JMenuItem openItem = new JMenuItem("Open");
- menu.add(openItem);
- openItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- // show file chooser dialog
- int result = chooser.showOpenDialog(null);
- // if file selected, set it as icon of the label
- if (result == JFileChooser.APPROVE_OPTION)
- {
- String name = chooser.getSelectedFile().getPath();
- label.setIcon(new ImageIcon(name));
- }
- }
- });
- JMenuItem exitItem = new JMenuItem("Exit");
- menu.add(exitItem);
- exitItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- System.exit(0);
- }
- });
- }
- private JLabel label;
- private JFileChooser chooser;
- private static final int DEFAULT_WIDTH = 300;
- private static final int DEFAULT_HEIGHT = 400;
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/9/235659406.zip |
|