2

我有一个JFrame看起来像一个典型的聊天框。为了连接和断开与服务器的连接,我实现了 aJPopupMenu并将 a 添加MouseListener到主窗口。
但是弹出菜单不会出现。

主窗口中的各种 JComponents:

  • JTextArea在一个JScrollPane
  • JTextField输入消息
  • Send按钮
  • JScrollPaneArrayList显示用户

  • 无论您右键单击何处,都应该出现弹出窗口。无论是在JTextArea输入消息的字段上还是字段上。
    我要向哪些所有组件添加侦听器,我要添加什么侦听器?

    代码

    变量:

        private static A_Chat_Client chatClient;
        public static String userName = "Anonymous";
    
    //------------------------------------------------------------------------------
        public static JFrame mainFrame = new JFrame();
        public static JTextArea textArea = new JTextArea(30,30);
        public static JScrollPane pane = new JScrollPane(textArea);
        public static JTextField message = new JTextField(10);
        public static JButton send = new JButton("Send");
    
        public static JPopupMenu popup = new JPopupMenu();
        public static JMenuItem connect = new JMenuItem("Connect");
        public static JMenuItem disconnect = new JMenuItem("Disconnect");
        public static JMenuItem help = new JMenuItem("Help");
    
        public static JList usersOnline = new JList();
        public static JScrollPane userPane = new JScrollPane(usersOnline);  
    

    主要方法

    public static void main(String[] args) {
            buildMainWindow();
            initialize();
            addListeners();
            popup.show(mainFrame, 0, 0); //forcefully popup
        }  
    

    添加监听器()

    public static void addListeners(){
    
            mainFrame.addMouseListener(new MouseHandler());
    
            send.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionSend();
                }
            });
            connect.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionConnect();
                }
            });
            disconnect.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionDisconnect();
                }
            });
            help.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionHelp();
                }
            });
        }  
    

    类 MouseHandler

    private static class MouseHandler extends MouseAdapter{
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.isPopupTrigger()){
                    popup.show(mainFrame, e.getX(), e.getY());
                }
            }
        }
    
    4

    1 回答 1

    2

    您可以使用附加AWTEventListener到主事件队列Toolkit#addAWTEventListener

    然后,您需要检查您收到的事件类型并检查它是否是弹出事件。

    在此处输入图像描述

    public class TestGlobalPopup {
    
        public static void main(String[] args) {
            new TestGlobalPopup();
        }
    
        public TestGlobalPopup() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                    @Override
                    public void eventDispatched(AWTEvent event) {
                        if (event instanceof MouseEvent) {
                            MouseEvent me = (MouseEvent) event;
                            if (me.isPopupTrigger()) {
                                Component component = me.getComponent();
                                JPopupMenu popup = new JPopupMenu();
                                popup.add(new JLabel("Clicked on " + component.getClass().getName()));
                                popup.show(component, me.getX(), me.getY());
                            }
                        }
                    }
                }, AWTEvent.MOUSE_EVENT_MASK);
    
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(new JLabel("Label: "), gbc);
                gbc.gridx++;
                add(new JTextField(10), gbc);
    
                gbc.gridy++;
                gbc.gridx = 0;
                gbc.gridwidth = 2;
                add(new JScrollPane(new JTextArea(5, 20)), gbc);
            }
    
        }
    
    }
    
    于 2013-01-15T02:27:26.110 回答