当通过不同的事件类型时,来自 JPanel 的组件的不同值

Different value of components from JPanel when coming through different event types

有一个面板包含4个组件; 1 个 JLabel、1 个 JButton、1 个 JTextField 和 1 个 JPopupMenu
有一种方法 "testMethod()",首先我从面板中获取组件数量,然后获取组件类型(JButton、JLabel 等)。

现在当控制流转到 testMethod() 时会发生 2 种情况:

  1. 当通过 JButton click 事件时,它在面板上显示 4 个组件,并在获取所有 4 个组件后
  2. 当通过 JMenuItem 单击事件时,它在面板上显示 3 个组件(未获取 JPopupMenu)并且仅在 3 个组件之后(未获取 JPopupMenu)

我不明白是什么导致了这种行为。我在互联网上搜索了很多,也阅读了 JPopupMenu 文档,但一无所获。

代码如下:
(这是非常大的代码的一部分,所以我只把显示场景的代码放在这里)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestProject extends JFrame {

    JPanel panel;
    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestProject frame = new TestProject();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestProject() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                testMethod();
            }
        });
        panel.add(btnNewButton);

        textField = new JTextField();
        panel.add(textField);
        textField.setColumns(10);

        // Creating upper level container
        JPopupMenu editMenu = new JPopupMenu("Edit");

        JMenuItem item1 = new JMenuItem("Item 1");
        item1.setBackground(Color.WHITE);

        JMenuItem item2 = new JMenuItem("Item 2");
        item2.setBackground(Color.WHITE);
        item2.setBackground(Color.WHITE);
        // Setting Copy action listener
        item2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                testMethod();
            }
        });

        // Adding the menu items on the popup menu
        editMenu.add(item1);
        editMenu.add(item2);

        panel.addMouseListener(new MouseAdapter() {
               public void mouseClicked(MouseEvent e) {
                   // Added check to open menu only on left click
                   if (e.getButton() == MouseEvent.BUTTON3)
                       editMenu.show(panel, e.getX(), e.getY());
               }               
            });

        // Adding popup menu to the logAreaPanel
        panel.add(editMenu); 

    }

    void testMethod()
    {
        // Here getting number of components of panel
        // When coming through JButton click event, it shows 4 components on panel and after get all 4 components 
        int items = panel.getComponentCount();
        Component c;

        // Getting all the trees one by one and storing their data into StringBuilder
        for(int loopCounter = 0; loopCounter < items; loopCounter++)
        {
            // here getting different component types
            // When coming through JMenuItem click event, it shows 3 components on panel(not getting JPopupMenu) and after only 3 components(not getting JPopupMenu)
            c = panel.getComponent(loopCounter);
        }
    }
}

如有任何帮助,我们将不胜感激。 :-)

您不需要将弹出窗口添加到组件中。首选方法是只使用

panel.setComponentPopupMenu(editMenu);

根据 tutorial,您可以使用 MouseListener。

//panel.add(editMenu); 
panel.addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

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

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            editMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

这样组件数将始终为三个,因为它永远不会添加到面板中。