import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.awt.*;
class displaymsg extends JFrame {
public static void main(String args []){
JFrame f = new JFrame();
JMenuBar mb = new JMenuBar();
JMenu supply = new JMenu("supply");
JMenuItem addnew = new JMenuItem("add");
addnew.addMenuListener(new MenuListener(){
@Override
public void menuSelected(MenuEvent me){
JOptionPane.showMessageDialog(f,"under construction");
}
});
f.add(mb); mb.add(supply);
supply.add("New Product");
f.setSize(400, 400);
f.setJMenuBar(mb);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
这是我的menuListener代码,但我还没有得到如何做到这一点 如果有人对我可能做错了什么有任何想法,请指出
答案 0 :(得分:1)
请参阅以下mcve中的评论:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
//see https://www.javatpoint.com/java-naming-conventions
//the class does not need to extend JFrame if not used as JFrame
public class DisplayMsg{
public static void main(String args []){
JFrame f = new JFrame();
JMenu supply = new JMenu("Supply");
supply.add("New Product");
JMenuItem addNew = new JMenuItem("Add"); //naming conventions
addNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f,"under construction");
}
});
JMenuBar mb = new JMenuBar();
mb.add(supply);
mb.add(addNew); //missing in your code
//f.add(mb); this would add the menu to the content pane
f.setJMenuBar(mb);
f.setSize(400, 400);
//f.setLayout(null); // do not use null layout
//see https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
可以将MenuListener添加到JSenu,而不是添加到JMenuItem。如果你想使用它
将addNew
更改为JMenu
:
JMenu addNew = new JMenu("Add");
addNew.addMenuListener(new MenuListener(){
@Override
public void menuSelected(MenuEvent me){
JOptionPane.showMessageDialog(f,"under construction");
}
@Override
public void menuCanceled(MenuEvent e) {}
@Override
public void menuDeselected(MenuEvent e) {}
});
答案 1 :(得分:1)
试一试。这就是重点。菜单是一种组织事物的方式。您有一个JMenuBar
,其中包含一组Menu
个。每个菜单都有一组MenuItems
。您只需向ActionListener
添加MenuItem
即可处理事件。
public class DisplayMsg {
public static void main(String[] args) {
JFrame f = new JFrame();
JMenuBar mb = new JMenuBar();
JMenu supply;
JMenuItem addnew;
supply = new JMenu("supply");
addnew = new JMenuItem("add");
// JButton b = new JButton("click me");
// b.setBounds(100,120,100,40);
addnew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f, "under construction");
}
});
// f.add(b);
f.add(mb);
mb.add(supply);
supply.add(addnew);
supply.add("New Product");
f.setSize(400, 400);
f.setJMenuBar(mb);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}