JButtons 在弹出菜单显示后消失

问题描述 投票:0回答:1

我向弹出菜单的 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());
        }
        
    }
    
}

事情就是这样,
(来源:uploadir.com


(来源:uploadir.com

java swing popup jbutton popupmenu
1个回答
3
投票

停止使用

null
布局,似乎这可能是与此相关的问题之一。你的
JFrame
对我来说有点
BLACK
,这是一些
THEME
还是你正在使用的新
LOOK AND FEEL
,这也可能是这件事的原因。在这里检查一下,使用这段代码它可以完美地工作:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonPopUp
{
    private static final int SIZE = 30;
    private JButton[] button = new JButton[SIZE];
    private JPopupMenu popup = new JPopupMenu("Hello World");

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Button POP UP Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);

        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5));

        JMenuItem menuItem1 = new JMenuItem("Menu Item 1");
        JMenuItem menuItem2 = new JMenuItem("Menu Item 2");
        //popup.add(greetings);
        popup.insert(menuItem1, 0);
        popup.insert(menuItem2, 1);

        for (int i = 0; i < SIZE; i++)
        {
            button[i] = new JButton();
            button[i].setBorder(BorderFactory.createEtchedBorder());
            button[i].addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent me)
                {
                    System.out.println("I am WORKING!!");
                    popup.show((JComponent)me.getSource(), me.getX(), me.getY());
                }
            });
            contentPane.add(button[i]);
        }

        frame.getContentPane().add(contentPane);
        frame.setSize(175, 250);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonPopUp().createAndDisplayGUI();
            }
        });
    }
}

这是输出:

Before EventAfter Event

© www.soinside.com 2019 - 2024. All rights reserved.