eclipse 如何将 ActionListener 添加到 JMenuItem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16329734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:26:54  来源:igfitidea点击:

How to add ActionListener to JMenuItem?

javaeclipseswingactionlistenerjmenuitem

提问by Mina Hafzalla

I'm trying to add ActionListener to JMenuItem in my java menu.

我正在尝试将 ActionListener 添加到我的 Java 菜单中的 JMenuItem。

Here is a screenshot of the menu:

这是菜单的屏幕截图:

enter image description here

在此处输入图片说明

I want to add ActionListener to "Rectangle" JMenuItem in order to show up the rectangle shape upon clicking on the "Rectangle" menu item. I tried many times to add the ActionListener but I fail every time.

我想将 ActionListener 添加到“矩形”JMenuItem,以便在单击“矩形”菜单项时显示矩形形状。我尝试了很多次添加 ActionListener 但每次都失败了。

Here is my code:

这是我的代码:

Class "menubar.java" :

类“menubar.java”:

import javax.swing.*;

public class menubar extends JFrame{

public menubar(){
    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);

    JMenu shape = new JMenu("Shape");
    menubar.add(shape);

    JMenuItem rect = new JMenuItem("Rectangle");
    shape.add(rect);

    JMenuItem star = new JMenuItem("Star");
    shape.add(star);

    JMenu color = new JMenu("Color");
    menubar.add(color);

    JMenuItem black = new JMenuItem("Black");
    color.add(black);

    JMenuItem orange = new JMenuItem("Orange");
    color.add(orange);
}

public static void main(String[] args) {
    menubar gui = new menubar();
    gui.setTitle("Menu Bar");
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    shapes SPS = new shapes();
    gui.add(SPS);
    gui.setSize(500,300);
    gui.setVisible(true);
    gui.setLocationRelativeTo(null);
}
}

Class "shapes.java" :

类“shapes.java”:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class shapes extends JPanel{
int midX = 220;
int midY = 90;
int radius[] = {60,20,50,20};
int nPoints = 16;
int[] X = new int[nPoints];
int[] Y = new int[nPoints];

public void paintComponent(Graphics gphcs){
super.paintComponent(gphcs);
this.setBackground(Color.WHITE);

gphcs.setColor(Color.BLUE);
gphcs.fillRect(20,35,100,30);

gphcs.setColor(Color.RED);
gphcs.drawString("Welcome to Java", 20, 20);

for (int i=0; i < nPoints; i++) {
       double x = Math.cos(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];
       double y = Math.sin(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];

       X[i] = (int) x + midX;
       Y[i] = (int) y + midY;
}
gphcs.setColor(Color.GREEN);
gphcs.fillPolygon(X, Y, nPoints);
}
}

I would be very thankful if anybody helped me with this issue.

如果有人帮助我解决这个问题,我将非常感激。

Thanks for your time..

谢谢你的时间..

采纳答案by sparkyShorts

One way that you can add an ActionListener to your container is :

您可以向容器添加 ActionListener 的一种方法是:

public class JMenuClass extends JFrame implements ActionListener

公共类 JMenuClass 扩展 JFrame 实现 ActionListener

Then, for each JMenuItem that you need a listener, you would do

然后,对于每个需要侦听器的 JMenuItem,你会做

item.addActionListener(this)

item.addActionListener(this)

At the bottom of your class, you would need to add your actionPerformed methods, or however you want to implement it.

在类的底部,您需要添加 actionPerformed 方法,或者您想要实现它。

Cheers

干杯