1

我正在尝试JMenu在单击它时构建一个动态权限(我只得到一个空菜单),下面是我的代码。

    final JMenu JMWindows = new JMenu("Opened Windows");                
    JMWindows.addActionListener(new ActionListener() {          
        @Override
        public void actionPerformed(ActionEvent e) {
            for(JInternalFrame ji : desktop.getAllFrames())
            {
                JMWindows.add(ji.getTitle());
            }               
        }
    });

我已经意识到 theactionperformed永远不会被调用,JMenu它在 aJMenuBar中。可能是什么问题呢 ?

4

3 回答 3

7
  • 您添加ActionListener到此JMenu无法JMenu使用MenuListener并通过设置它JMenu#addMenuListener(..)

  • 此外,您需要在添加/删除组件后调用实例revalidate(),否则更改将不会被反映(我们也可以在容器实例上调用它,即或,但我们知道只有 1 个特定会更改,因此不需要 IMO)。repaint()JMenuJMenuBarJFrameJMenu

  • 请注意您的变量命名方案JMWindow应该是jmWindow/jMWindow变量总是以没有大写字母开头,并且此后每个新单词的第一个字母都大写(除了常量和enums)。

这是一个使用示例MenuListener

在此处输入图像描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class Test {

    private JDesktopPane jdpDesktop;
    private static int openFrameCount = 0;
    final JFrame frame = new JFrame("JInternalFrame Usage Demo");

    public Test() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // A specialized layered pane to be used with JInternalFrames
        jdpDesktop = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 500);
            }
        };


        for (int i = 0; i < 3; i++) {
            createFrame(); // Create first window
        }

        frame.setContentPane(jdpDesktop);

        frame.setJMenuBar(createMenuBar());

        // Make dragging faster by setting drag mode to Outline
        jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");

        frame.pack();
        frame.setVisible(true);
    }

    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Frame");
        menu.setMnemonic(KeyEvent.VK_N);
        JMenuItem menuItem = new JMenuItem("New IFrame");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createFrame();
            }
        });
        menu.add(menuItem);
        menuBar.add(menu);


        final JMenu jmWindows = new JMenu("Opened Windows");
        jmWindows.addMenuListener(new MenuListener() {
            @Override
            public void menuSelected(MenuEvent me) {
                jmWindows.removeAll();//remove previous opened window jmenuitems
                for (JInternalFrame ji : jdpDesktop.getAllFrames()) {
                    JMenuItem menuItem = new JMenuItem(ji.getTitle());
                    jmWindows.add(menuItem);
                }

                jmWindows.revalidate();
                jmWindows.repaint();
                jmWindows.doClick();

            }

            @Override
            public void menuDeselected(MenuEvent me) {
            }

            @Override
            public void menuCanceled(MenuEvent me) {
            }
        });
        menuBar.add(jmWindows);


        return menuBar;
    }

    protected void createFrame() {
        Test.MyInternalFrame frame = new Test.MyInternalFrame();
        frame.setVisible(true);
        // Every JInternalFrame must be added to content pane using JDesktopPane
        jdpDesktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    class MyInternalFrame extends JInternalFrame {

        static final int xPosition = 30, yPosition = 30;

        public MyInternalFrame() {
            super("IFrame #" + (++openFrameCount), true, // resizable
                    true, // closable
                    true, // maximizable
                    true);// iconifiable
            setSize(300, 300);
            // Set the window's location.
            setLocation(xPosition * openFrameCount, yPosition
                    * openFrameCount);
        }
    }
}
于 2012-12-11T14:45:26.070 回答
2

添加或删除某些内容后,您需要重新绘制和/或验证您的框架。您可以为此使用方法repaint()validate()JFrame

于 2012-12-11T13:33:20.400 回答
1

您错过了定义 JMenuItem。

    final JMenu JMWindows = new JMenu("Opened Windows");                
    JMWindows.addActionListener(new ActionListener() {          
        @Override
        public void actionPerformed(ActionEvent e) {
            for(JInternalFrame ji : desktop.getAllFrames())
            {
                JMenuItem menuItem = new JMenuItem(ji.getTitle());
                JMWindows.add(menuItem);
            }               
        }
    });
于 2012-12-11T13:48:11.760 回答