有一个面板包含4个组件; 1个JLabel,1个JButton,1个JTextField和1个JPopupMenu
有一个方法“testMethod()”,其中首先从面板中获取组件的数量,然后获取组件的类型(JButton,JLabel等)。
现在,当控制流转到testMethod()时,会发生两种情况:
我不知道导致此行为的原因。我在互联网上搜索了很多,还阅读了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);
}
}
}
任何帮助将不胜感激。 : - )
答案 0 :(得分:2)
您不需要将弹出窗口添加到组件中。首选方法是使用
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());
}
}
});
这样,组件计数将始终为3,因为它永远不会添加到面板中。