JPopupMenu 没有出现?

发布于 2024-11-06 22:12:03 字数 1804 浏览 4 评论 0原文

我有一个 Java 小程序,其中包含几个用户必须与之交互的弹出菜单。但是,添加后 JPopupMenu 不会显示。这是我的代码:

public class Parser extends JApplet implements ActionListener {
    private static final long serialVersionUID = 1L;
    JPopupMenu deviceMenu;
    JButton downloadButton;
    Map <String, Object> deviceDict;

    public void init () {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } 
        catch (Exception e) { 
            System.err.println("createGUI didn't successfully complete");
        }
    }
    public void createGUI() {
        try {
            URL url = new URL("[URL]");
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    conn.getInputStream()));
            String inputLine;
            String xml = "";
            while ((inputLine = in.readLine()) != null) 
                xml = xml + inputLine;
        deviceDict = Plist.fromXml(xml);
        System.out.print(deviceDict);
        } 

        catch (XmlParseException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(480, 360));
        setSize(480, 360);
        Iterator <String> deviceIterator = deviceDict.keySet().iterator();
        deviceMenu = new JPopupMenu("Test");
        while (deviceIterator.hasNext()) {
            JMenuItem item = new JMenuItem(deviceIterator.next());
            deviceMenu.add(item);
        }
        add(deviceMenu);
    }
}

有什么想法吗?

I have a Java applet that will consist of several popup menus that the user will have to interact with. However, the JPopupMenu won't show up when added. Here is my code:

public class Parser extends JApplet implements ActionListener {
    private static final long serialVersionUID = 1L;
    JPopupMenu deviceMenu;
    JButton downloadButton;
    Map <String, Object> deviceDict;

    public void init () {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } 
        catch (Exception e) { 
            System.err.println("createGUI didn't successfully complete");
        }
    }
    public void createGUI() {
        try {
            URL url = new URL("[URL]");
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    conn.getInputStream()));
            String inputLine;
            String xml = "";
            while ((inputLine = in.readLine()) != null) 
                xml = xml + inputLine;
        deviceDict = Plist.fromXml(xml);
        System.out.print(deviceDict);
        } 

        catch (XmlParseException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(480, 360));
        setSize(480, 360);
        Iterator <String> deviceIterator = deviceDict.keySet().iterator();
        deviceMenu = new JPopupMenu("Test");
        while (deviceIterator.hasNext()) {
            JMenuItem item = new JMenuItem(deviceIterator.next());
            deviceMenu.add(item);
        }
        add(deviceMenu);
    }
}

Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-11-13 22:12:03

您希望它什么时候出现?
您需要调用 show() 如果你想显示弹出菜单。
请参阅此示例来自 Oracle 站点的一个

顺便说一句 - 从你的问题来看,似乎 JDialog

When do you want it to show up?
You need to call show() if you want to display the popup menu.
See this example and the one from oracle site.

BTW - from your question it seems JDialog

旧梦荧光笔 2024-11-13 22:12:03

必须使用 JComboBox 而不是 JPopupMenu

Had to use a JComboBox instead of JPopupMenu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文