什么是sun.awt.windows.WToolkit? [英] What is sun.awt.windows.WToolkit?

查看:1950
本文介绍了什么是sun.awt.windows.WToolkit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段code的

 进口java.awt中的*。
java.awt.event中导入*。
导入的java.lang.reflect *。
进口的javax.swing *。
类QueueTest {
静态INT I = 0;
    公共静态无效的主要(字串[] args)抛出InterruptedException的,{的InvocationTargetException
        的EventQueue的EventQueue =。Toolkit.getDefaultToolkit()getSystemEventQueue();
        eventQueue.push(新MyEventQueue());
    架F =新帧();
    f.setSize(400,400);
    //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocation(150,150);
    f.setLayout(新的FlowLayout());
    f.setVisible(真);    按钮B =新按钮(按钮);
    //b.setEnabled(false);
    f.add(二);
/ *
    b.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的AE)
        {
        的System.out.println(按钮被点击);
        }
    });
* /
    }    私有静态类MyEventQueue扩展的EventQueue {
        公共无效事件后(AWTEvent中theEvent){
//的System.out.println(事件发布);
  的System.out.println(事件的源是+ theEvent.getSource());
            super.postEvent(theEvent);
        }    保护无效则dispatchEvent(AWTEvent中的事件)
    {
    的System.out.println(+(我+ +)+事件(之源)是+ event.getSource());
    super.dispatchEvent(事件);
    }
    }
}

在输出我可以有时会看到

事件的来源是(78)sun.awt.windows.WToolkit@77ef83

当我想我只有两个来源,在 java.awt.Button中的的java.awt.Frame 。此外,当我pressing鼠标,我可以看到正在生成两个事件对哪一个是 sun.awt.windows.WToolkit 是源和另一种是按钮(当我点击按钮)。

我的问题是


  1. 什么是sun.awt.windows.WToolkit?

  2. 为什么我能够看到一个单一的鼠标preSS两个事件?


解决方案

的名称不言自明: AWT 表示的抽象窗口工具包的这意味着工具包摘要,需要一个实际的实现。 sun.awt.windows.WToolkit 是这样的的的Microsoft Windows 的plattform因此是W 在其名称。在其他操作系统上,你会看到不同的实现,例如 sun.awt.X11.XToolkit 上的的Linux 的。如果你只是做了的System.out.println(Toolkit.getDefaultToolkit()); 您将看到工具包的字符串重新presentation匹配你从不时看到事件源。

我建议你做整个事件而不只是源的打印。然后你会看到这些事件都是。你会看到工具包会生成什么样的事件。你会看到,鼠标点击可以产生多达三个事件:一为preSS,一个用于释放一个,如果pressing在这被认为是相同的位置处释放发生了的点击

I have the following piece of code

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.swing.*;
class QueueTest {
static int i=0;
    public static void main(String[] args) throws InterruptedException, 

InvocationTargetException {
        EventQueue eventQueue = 

Toolkit.getDefaultToolkit().getSystemEventQueue();
        eventQueue.push(new MyEventQueue());


    Frame f=new Frame();
    f.setSize(400,400);
    //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocation(150,150);
    f.setLayout(new FlowLayout());
    f.setVisible(true);

    Button b=new Button("button");
    //b.setEnabled(false);
    f.add(b);
/*
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
        {
        System.out.println("button is clicked");
        }
    });
*/
    }

    private static class MyEventQueue extends EventQueue {
        public void postEvent(AWTEvent theEvent) {
//            System.out.println("Event Posted");
  System.out.println("The source of event is "+theEvent.getSource());
            super.postEvent(theEvent);
        }

    protected void dispatchEvent(AWTEvent event)
    {
    System.out.println("The source of event ("+(i++)+") is 

"+event.getSource());
    super.dispatchEvent(event);
    }
    }
}

In the output i could sometimes see

The source of event is (78) sun.awt.windows.WToolkit@77ef83

when I think i have only two sources, the java.awt.Button and the java.awt.Frame. Also when I am pressing the mouse, I could see two events being generated for which one is sun.awt.windows.WToolkit is the source and for the other is Button (when I clicked on button).

My questions are

  1. what is sun.awt.windows.WToolkit?
  2. why am I able to see two events on a single mouse press?

解决方案

The names speak for themselves: AWT stands for Abstract Window Toolkit which implies that the Toolkit is abstract and requires an actual implementation. sun.awt.windows.WToolkit is such an implementation for the Microsoft Windows plattform hence the W in its name. On other operating systems you will see different implementations, e.g. sun.awt.X11.XToolkit on Linux. If you just do a System.out.println(Toolkit.getDefaultToolkit()); you will see that the Toolkit’s string representation matches that of the event source which you see from time to time.

I suggest you do a print of the entire event instead of just the source. Then you will see what these events are for. You will see what kind of events the toolkit generates. And you will see that a mouse click can generate up to three events: one for the press, one for the release and one if pressing and releasing happened at the same location which is considered a click.

这篇关于什么是sun.awt.windows.WToolkit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆