JList 上的 AWT-EventQueue-1

发布于 2024-10-15 03:59:13 字数 4531 浏览 3 评论 0原文

我终于得到了正确编译的代码。不过,有一个可能。我设置了 listbox 添加一个鼠标监听器,但我得到了一个巨大的错误:

我主要将其添加到代码中:listbox.addMouseListener(new MousePopupListener());

当我运行它时,它起作用了。但是,当我右键单击 JList 时,我得到:

Row: 0
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at inv$MousePopupListener.checkPopup(inv.java:91)
    at inv$MousePopupListener.mouseReleased(inv.java:84)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

这是我的代码:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class inv extends JApplet implements MouseListener
{
public JList listbox;
public JPopupMenu popup;
public JMenuItem item;

public void init()
{
    ActionListener menuListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            String invAction = event.getActionCommand();
            System.out.println("Popup menu item [" + invAction + "] was pressed.");
        }
    };

    JPopupMenu popup = new JPopupMenu();

    popup.add(item = new JMenuItem("Use"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Drop"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Cancel"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);



    String listData[] =
    {
        "Item 1","Item 2","Item 3","Item 4"
    };

    listbox = new JList( listData );
    listbox.addMouseListener( new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        {
            if ( SwingUtilities.isRightMouseButton(e) )
            {
                System.out.println("Row: " + getRow(e.getPoint()));
                listbox.setSelectedIndex(getRow(e.getPoint()));
            }
        }
    }
    );

    listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    add(listbox);
    listbox.setVisible(true);
    listbox.setFocusable(false);


    listbox.addMouseListener(new MousePopupListener());
}

class MousePopupListener extends MouseAdapter
{
    public void mousePressed(MouseEvent e)
    {
        checkPopup(e);
    }

    public void mouseClicked(MouseEvent e)
    {
        checkPopup(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        checkPopup(e);
    }

    private void checkPopup(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            popup.show(inv.this, e.getX(), e.getY());
        }
    }
}

private int getRow(Point point)
{
    return listbox.locationToIndex(point);
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseReleased(MouseEvent e)
{
}

public void mousePressed(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

}

I finally got the code to compile correctly. However, there is a probably. I set the listbox to add a mouselistener and all but I get a huge error:

I added this, primarily, to the code: listbox.addMouseListener(new MousePopupListener());

and when I run it, it works. However, when I RIGHT-CLICK on the JList I get this:

Row: 0
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at inv$MousePopupListener.checkPopup(inv.java:91)
    at inv$MousePopupListener.mouseReleased(inv.java:84)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Here's my code:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class inv extends JApplet implements MouseListener
{
public JList listbox;
public JPopupMenu popup;
public JMenuItem item;

public void init()
{
    ActionListener menuListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            String invAction = event.getActionCommand();
            System.out.println("Popup menu item [" + invAction + "] was pressed.");
        }
    };

    JPopupMenu popup = new JPopupMenu();

    popup.add(item = new JMenuItem("Use"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Drop"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Cancel"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);



    String listData[] =
    {
        "Item 1","Item 2","Item 3","Item 4"
    };

    listbox = new JList( listData );
    listbox.addMouseListener( new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        {
            if ( SwingUtilities.isRightMouseButton(e) )
            {
                System.out.println("Row: " + getRow(e.getPoint()));
                listbox.setSelectedIndex(getRow(e.getPoint()));
            }
        }
    }
    );

    listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    add(listbox);
    listbox.setVisible(true);
    listbox.setFocusable(false);


    listbox.addMouseListener(new MousePopupListener());
}

class MousePopupListener extends MouseAdapter
{
    public void mousePressed(MouseEvent e)
    {
        checkPopup(e);
    }

    public void mouseClicked(MouseEvent e)
    {
        checkPopup(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        checkPopup(e);
    }

    private void checkPopup(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            popup.show(inv.this, e.getX(), e.getY());
        }
    }
}

private int getRow(Point point)
{
    return listbox.locationToIndex(point);
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseReleased(MouseEvent e)
{
}

public void mousePressed(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-10-22 03:59:13

问题是您将 popup 声明为类变量,然后在 init 方法中创建 popup 的本地实例。结果是类级别的 popup 永远不会设置为任何内容,并且当您尝试在 checkPopup 中使用它时会导致 NPE。更改线路:

JPopupMenu popup = new JPopupMenu();

应该

popup = new JPopupMenu();

可以解决问题。

The issue is that you declare popup as a class variable and then create a local instance of popup in the init method. The result is that the class level popup is never set to anything and causes an NPE when you try to use it in checkPopup. Changing the line:

JPopupMenu popup = new JPopupMenu();

to

popup = new JPopupMenu();

should fix the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文