菜单监听器操作已执行

问题描述 投票:0回答:2
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代码,但我没有得到如何做到这一点 如果有人对我可能做错了什么有任何想法,请指出

java swing jmenuitem jmenubar
2个回答
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可以添加到JMenu,而不是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
投票

试试吧。这就是重点。菜单是一种组织事物的方式。你有一个JMenuBar,它由一组Menus组成。每个菜单都有一套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);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.