我在弹出菜单的 JButtons 中添加了侦听器,但是当弹出菜单出现时,JButtons 消失了,我需要将光标悬停在按钮上以使它们再次出现。为什么会这样?
(这里的所有方法都在同一个类中)
public Inventory() {
setLayout(null);
setBounds(0, 0, 175, 210);
initPopupMenu(); // this just sets what is inside the popup menu
int x;
// 30 buttons
for(x = 0; x < 30; x++) {
button[x] = new JButton();
add(button[x]);
button[x].addMouseListener(this);
}
x = 0;
// it's a grid of buttons
for(int i = 0; i < 5; i++)
for(int j = 0; j < 6; j++) {
button[x].setBounds(i*35+1,j*35+1, 33,33);
x++;
}
}
public void mouseClicked(MouseEvent e) {
for(int j = 0; j < 30; j++) // i tried this one but it still disappears
button[j].repaint();
for(int i = 0; i < 30; i++) {
if(e.getSource() == button[i]) {
System.out.println("You pressed Button "+i);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
这就是发生的事情,