如标题所述,我需要知道如何确定是否单击了一个菜单项,然后运行相关的功能。例如,我有一个带有JMenuItem“exit”的JMenu,点击它时应该运行一个close form方法。
import javax.swing.*;
public class selector_form extends JFrame {
/**
*
*/
private static final long serialVersionUID = -5963842156289770842L;
public selector_form(String name)
{
super.setTitle(name);
setupComponents(this);
super.setVisible(true);
}
private void setupComponents(JFrame frame)
{
JMenuItem file_items = new JMenuItem("Exit");
JMenuItem config_items = new JMenuItem("Preferences");
JMenuItem[] machine_items = {new JMenuItem("Refresh"),
new JMenuItem("Add Dynamically"), new JMenuItem("Remove Dynamically")
};
JMenuItem[] emulator_items = {new JMenuItem("Start Emulator"),
new JMenuItem("Stop Emulator"), new JMenuItem("Pause Emulator"),
new JMenuItem("Reset Emulator"), new JMenuItem("Crash Emulator")
};
JMenuItem[] memory_items = {new JMenuItem("View Emulator Memory"),
new JMenuItem("System Cheats"), new JMenuItem("Dump Emulator Memory"),
new JMenuItem("Edit Specific Address"),
new JMenuItem("Show Allocations"), new JMenuItem("Allocate Memory"),
new JMenuItem("DeAllocate Memory")
};
JMenuItem[] cpu_items = {new JMenuItem("Show Host Specs"),
new JMenuItem("Show Emulator Specs"),
new JMenuItem("Enable HyperThreadding")
};
JMenuItem[] about_items = {new JMenuItem("Help Contents"),
new JMenuItem("About")
};
frame.setSize(800, 600);
JMenuBar jmb = new JMenuBar();
JMenu jm_a = new JMenu("File");
JMenu jm_c = new JMenu("Config");
JMenu jm_d = new JMenu("Machines");
JMenu jm_e = new JMenu("Emulator");
JMenu jm_f = new JMenu("Memory");
JMenu jm_g = new JMenu("CPU");
JMenu jm_h = new JMenu("About");
jm_a.add(file_items);
jm_c.add(config_items);
for(JMenuItem item : machine_items)
jm_d.add(item);
for(JMenuItem item : emulator_items)
jm_e.add(item);
for(JMenuItem item : memory_items)
jm_f.add(item);
for(JMenuItem item : cpu_items)
jm_g.add(item);
for(JMenuItem item : about_items)
jm_h.add(item);
jmb.add(jm_a);
jmb.add(jm_c);
jmb.add(jm_d);
jmb.add(jm_e);
jmb.add(jm_f);
jmb.add(jm_g);
jmb.add(jm_h);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(jmb);
}
}
答案 0 :(得分:4)
只需添加ActionListener
即可。
menu_item.addActionListener(this);
并实现ActionListener
界面。
答案 1 :(得分:3)
can do this ActionListener {{3}}。您可以创建自己的ActionListener类。
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + e.getActionCommand());
}
}
将其添加到JMenuItem。
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
答案 2 :(得分:-1)
感谢所有答案。这就是我所做的。
我使用了totymedli的示例并编写了一个名为MenuActionHelper的类,它调用了一个MenuEventHandler专业类,它将ID传递给调用所需函数的函数。
更新了selector-form:
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class selector_form extends JFrame {
public static MenuEventHandler mehandler;
/**
*
*/
private static final long serialVersionUID = -5963842156289770842L;
public selector_form(String name)
{
super.setTitle(name);
setupComponents(this);
super.setVisible(true);
mehandler = new MenuEventHandler(this);
}
private void setupComponents(JFrame frame)
{
JMenuItem file_items = new JMenuItem("Exit");
JMenuItem config_items = new JMenuItem("Preferences");
JMenuItem[] machine_items = {new JMenuItem("Refresh"),
new JMenuItem("Add Dynamically"), new JMenuItem("Remove Dynamically")
};
JMenuItem[] emulator_items = {new JMenuItem("Start Emulator"),
new JMenuItem("Stop Emulator"), new JMenuItem("Pause Emulator"),
new JMenuItem("Reset Emulator"), new JMenuItem("Crash Emulator")
};
JMenuItem[] memory_items = {new JMenuItem("View Emulator Memory"),
new JMenuItem("System Cheats"), new JMenuItem("Dump Emulator Memory"),
new JMenuItem("Edit Specific Address"),
new JMenuItem("Show Allocations"), new JMenuItem("Allocate Memory"),
new JMenuItem("DeAllocate Memory")
};
JMenuItem[] cpu_items = {new JMenuItem("Show Host Specs"),
new JMenuItem("Show Emulator Specs"),
new JMenuItem("Enable HyperThreadding"),
new JMenuItem("Show Disassembly in real time")
};
JMenuItem[] about_items = {new JMenuItem("Help Contents"),
new JMenuItem("About")
};
frame.setSize(800, 600);
JMenuBar jmb = new JMenuBar();
JMenu jm_a = new JMenu("File");
JMenu jm_c = new JMenu("Config");
JMenu jm_d = new JMenu("Machines");
JMenu jm_e = new JMenu("Emulator");
JMenu jm_f = new JMenu("Memory");
JMenu jm_g = new JMenu("CPU");
JMenu jm_h = new JMenu("About");
jm_a.add(file_items);
jm_c.add(config_items);
for(JMenuItem item : machine_items)
jm_d.add(item);
for(JMenuItem item : emulator_items)
jm_e.add(item);
for(JMenuItem item : memory_items)
jm_f.add(item);
for(JMenuItem item : cpu_items)
jm_g.add(item);
for(JMenuItem item : about_items)
jm_h.add(item);
jmb.add(jm_a);
jmb.add(jm_c);
jmb.add(jm_d);
jmb.add(jm_e);
jmb.add(jm_f);
jmb.add(jm_g);
jmb.add(jm_h);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(jmb);
//Menu Action Helpers
file_items.addActionListener(new MenuActionHelper(1));
}
}
/* EFFECTIVE ACTIONLISTENER
*
* passes an integral argument to MeunEventHandler
*/
class MenuActionHelper implements ActionListener
{
public static int digitalIdentifier;
public MenuActionHelper(int i)
{
MenuActionHelper.digitalIdentifier = i;
}
@Override
public void actionPerformed(ActionEvent arg0)
{
try
{
if(!(selector_form.mehandler.parseEvent(MenuActionHelper.digitalIdentifier)))
{
throw new Exception("Invalid Menu Event ID Parsed!");
}
else return;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
和新的MenuEventHandler类:
package application;
import javax.swing.JFrame;
public class MenuEventHandler {
/* have a static number which is the max an id can be */
public static final int maxActionId = 1000; //good large number
public static JFrame frame;
public MenuEventHandler(JFrame frame) //import jframe functionality
{
MenuEventHandler.frame = frame;
}
public boolean parseEvent(int i)
{
boolean success = false;
switch(i)
{
case 1:
{
MenuEventHandler.frame.dispose();
success = true;
}
break;
}
return success;
}
}
现在如果有人点击退出MenuItem,表单将关闭,应用程序结束。