在java中为菜单按钮添加功能
我是Java新手,我想知道如何为菜单项添加功能?在java中为菜单按钮添加功能,java,swing,actionlistener,jmenu,jmenuitem,Java,Swing,Actionlistener,Jmenu,Jmenuitem,我是Java新手,我想知道如何为菜单项添加功能? 我希望它做的是将两个值设置为0 这是我目前拥有的代码: JMenuItem clear = new JMenuItem("Clear"); Options.add(clear); 您需要添加一个。这是一个接口,必须实现一个名为actionPerformed的方法 例如 这将添加一个匿名的ActionListener,一旦单击JMenuItem就会调用它 希望对您有所帮助。您需要添加一个。这是一个接口,必须实现一个名为actionPerforme
我希望它做的是将两个值设置为0 这是我目前拥有的代码:
JMenuItem clear = new JMenuItem("Clear");
Options.add(clear);
您需要添加一个。这是一个接口,必须实现一个名为actionPerformed
的方法
例如
这将添加一个匿名的ActionListener
,一旦单击JMenuItem
就会调用它
希望对您有所帮助。您需要添加一个。这是一个接口,必须实现一个名为actionPerformed
的方法
例如
这将添加一个匿名的ActionListener
,一旦单击JMenuItem
就会调用它
希望有帮助
这个例子来自《Java》一书
“基础类简”,大卫·弗拉纳根写的。版权 (c) 1999年由O'Reilly&Associates出版 希望它能帮助你开始 这个例子来自《Java》一书 “基础类简”,
大卫·弗拉纳根写的。版权 (c) 1999年由O'Reilly&Associates出版
希望它能帮助您入门阅读有关创建和使用ActionListener并将其添加到JMenuItems的教程。谢谢,但我真的不知道该怎么做,而且它们对您的帮助不大。在您的情况下,我们的帮助甚至比教程还小。你的问题太宽泛,无法给出明确的答案。与其他技能一样,使用Swing教程并从中学习会随着练习而变得更好。不要马上就认为它们没有帮助——它们非常有帮助,但要提高你与它们合作的技能。他们在这里帮助了我和许多其他人,也将帮助你们。祝你好运!阅读有关创建和使用ActionListener并将其添加到JMenuItems的教程。谢谢,但我真的不知道该怎么做,而且它们对您的帮助不大。对于您的情况,我们的帮助甚至比教程还要小。你的问题太宽泛,无法给出明确的答案。与其他技能一样,使用Swing教程并从中学习会随着练习而变得更好。不要马上就认为它们没有帮助——它们非常有帮助,但要提高你与它们合作的技能。他们在这里帮助了我和许多其他人,也将帮助你们。祝你好运!它被称为注释。基本上,这个注释将确保下面的方法继承自它的超类。换句话说,它将确保您键入的所有内容都正确。有关更多信息,请查看。它称为注释。基本上,这个注释将确保下面的方法继承自它的超类。换句话说,它将确保您键入的所有内容都正确。请查看以获取更多信息。感谢您的帮助-1用于传播过时代码;-)使用Action而不是ActionLister,使用componentPopupMenu属性而不是manual mouseListener(此处甚至不完整),感谢您的帮助-1用于传播过时代码;-)使用Action而不是ActionLister,使用componentPopupMenu属性而不是手动mouseListener(此处甚至不完整)
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
// Clear two values.
}
});`
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuDemo {
public static void main(String[] args) {
// Create a window for this demo
JFrame frame = new JFrame("Menu Demo");
JPanel panel = new JPanel();
frame.getContentPane().add(panel, "Center");
// Create an action listener for the menu items we will create
// The MenuItemActionListener class is defined below
ActionListener listener = new MenuItemActionListener(panel);
// Create some menu panes, and fill them with menu items
// The menuItem() method is important. It is defined below.
JMenu file = new JMenu("File");
file.setMnemonic('F');
file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N));
file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O));
file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S));
file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A));
JMenu edit = new JMenu("Edit");
edit.setMnemonic('E');
edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X));
edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C));
edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V));
// Create a menubar and add these panes to it.
JMenuBar menubar = new JMenuBar();
menubar.add(file);
menubar.add(edit);
// Add menubar to the main window. Note special method to add menubars
frame.setJMenuBar(menubar);
// Now create a popup menu and add the some stuff to it
final JPopupMenu popup = new JPopupMenu();
popup.add(menuItem("Open...", listener, "open", 0, 0));
popup.addSeparator(); // Add a separator between items
JMenu colors = new JMenu("Colors"); // Create a submenu
popup.add(colors); // and add it to the popup menu
// Now fill the submenu with mutually-exclusive radio buttons
ButtonGroup colorgroup = new ButtonGroup();
colors.add(radioItem("Red", listener, "color(red)", colorgroup));
colors.add(radioItem("Green", listener, "color(green)", colorgroup));
colors.add(radioItem("Blue", listener, "color(blue)", colorgroup));
// Arrange to display the popup menu when the user clicks in the window
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// Check whether this is the right type of event to pop up a popup
// menu on this platform. Usually checks for right button down.
if (e.isPopupTrigger())
popup.show((Component)e.getSource(), e.getX(), e.getY());
}
});
// Finally, make our main window appear
frame.setSize(450, 300);
frame.setVisible(true);
}
// A convenience method for creating menu items.
public static JMenuItem menuItem(String label,
ActionListener listener, String command,
int mnemonic, int acceleratorKey) {
JMenuItem item = new JMenuItem(label);
item.addActionListener(listener);
item.setActionCommand(command);
if (mnemonic != 0) item.setMnemonic((char) mnemonic);
if (acceleratorKey != 0)
item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,
java.awt.Event.CTRL_MASK));
return item;
}
// A convenience method for creating radio button menu items.
public static JMenuItem radioItem(String label, ActionListener listener,
String command, ButtonGroup mutExGroup) {
JMenuItem item = new JRadioButtonMenuItem(label);
item.addActionListener(listener);
item.setActionCommand(command);
mutExGroup.add(item);
return item;
}
// A event listener class used with the menu items created above.
// For this demo, it just displays a dialog box when an item is selected.
public static class MenuItemActionListener implements ActionListener {
Component parent;
public MenuItemActionListener(Component parent) { this.parent = parent; }
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
String cmd = item.getActionCommand();
JOptionPane.showMessageDialog(parent, cmd + " was selected.");
}
}
}