Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将ActionListener添加到JMenuItem?_Java_Eclipse_Swing_Actionlistener_Jmenuitem - Fatal编程技术网

Java 如何将ActionListener添加到JMenuItem?

Java 如何将ActionListener添加到JMenuItem?,java,eclipse,swing,actionlistener,jmenuitem,Java,Eclipse,Swing,Actionlistener,Jmenuitem,我正在尝试将ActionListener添加到java菜单中的JMenuItem 以下是菜单的屏幕截图: 我想将ActionListener添加到“矩形”JMenuItem,以便在单击“矩形”菜单项时显示矩形形状。我多次尝试添加ActionListener,但每次都失败了 这是我的密码: 类“menubar.java”: 类“shapes.java”: import java.awt.*; 导入java.awt.event.*; 导入javax.swing.*; 公共类形状扩展了JPanel{

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

以下是菜单的屏幕截图:

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

这是我的密码:

类“menubar.java”:

类“shapes.java”:

import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类形状扩展了JPanel{
int-midX=220;
int midY=90;
int半径[]={60,20,50,20};
int nPoints=16;
int[]X=新的int[nPoints];
int[]Y=新的int[nPoints];
公共组件(图形gphcs){
超级油漆组件(gphcs);
这个.背景(颜色.白色);
gphcs.setColor(Color.BLUE);
gphcs.fillRect(20,35100,30);
gphcs.setColor(Color.RED);
drawString(“欢迎来到Java”,20,20);
对于(int i=0;i
如果有人在这个问题上帮助我,我将非常感激


感谢您的时间。

将ActionListener添加到容器的一种方法是:

公共类JMenuClass扩展JFrame实现ActionListener

然后,对于每个需要侦听器的JMenuItem,您可以

item.addActionListener(此)

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


干杯

我看不出您在哪里尝试调用了
addActionListener(…)
。你看过Swing菜单教程了吗?这些都是为你们准备的,所以我们真的没有必要在这里重复这些信息,因为它们是为了提问而存在的。谷歌会帮你找到它——或者我也可以:真的?你看了你上一个问题中的哪一个链接了吗?投票赞成close@MadProgrammer当前位置我知道,这也让我吃惊,我不知道他已经得到了链接!对于原始海报,如果您之前已经看过教程,但它让您感到困惑,那么请确切地告诉我们您的问题让您感到困惑的是什么,但请告诉我们,您至少要先尝试解决它,然后再在这里抛售您的问题,而不显示出任何努力的证据。请告诉我们,你不是在偷懒,而是让别人帮你解决问题。-1,这不是一个好方法。首先,没有理由扩展JFrame,因为您没有向该框架添加任何新的功能性。另外,每个菜单项都应该有自己的ActionListener;我不是说每个JMenuItem都需要自己的侦听器吗?如果传入
这个
,每个JMenuItem如何获得唯一的侦听器?作为ActionListener?一、二、三、四项;公共类示例实现ActionListener//任何必要的实现一个=新的JMenuItem(“第一”);一、addActionListener(这个);二=新项目(“第二号”);二、添加ActionListener(本)//因此,我的意思是,用户可以创建类来处理动作事件,并将处理程序传递给addActionListener;两种方法都有效。我刚刚发现,对于较小的程序,在类定义中实现侦听器非常简单。
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);
}
}
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);
}
}