问题描述
我试图在点击它时动态构建一个 JMenu
(我只得到一个空菜单),下面是我的代码.
I am trying to build a JMenu
dynamically right when clicking in it (I'm only getting an empty menu), below is my code.
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());
}
}
});
我意识到 actionperformed
从未被调用,JMenu
位于 JMenuBar
内.可能是什么问题?
I've realized that the actionperformed
is never called, the JMenu
is inside a JMenuBar
. What could be the problem ?
推荐答案
您将
ActionListener
添加到JMenu
这不能为JMenu
使用MenuListener
并通过JMenu#addMenuListener(..)
You add
ActionListener
toJMenu
this cannot be done forJMenu
useMenuListener
and set it viaJMenu#addMenuListener(..)
此外,在添加/删除组件后,您还需要在
JMenu
实例上调用revalidate()
和repaint()
或更改不会被反映(我们也可以在容器实例上调用它,即JMenuBar
或JFrame
,但我们知道只有 1 个特定的JMenu
会改变所以不需要 IMO).Also you need to call
revalidate()
andrepaint()
onJMenu
instance after adding/removing components from it or the changes will not be reflected (we could also call this on the containers instance i.eJMenuBar
orJFrame
, but we know only 1 specificJMenu
will change so no need IMO).请注意你的变量命名方案
JMWindow
应该是jmWindow
/jMWindow
变量总是以无大写开头和第一个字母此后的每个新单词都会大写(除了常量和enums
).Please watch your variable naming schemes
JMWindow
should bejmWindow
/jMWindow
variables always begin with no caps and the 1st letter of every new word thereafter gets capitalized (besides for constants andenums
).这是一个使用
MenuListener
的例子:Here is an example using
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); } } }
这篇关于如何动态构建 JMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!