Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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 向多个菜单添加JMenuItem_Java_Swing_Jmenu_Jmenuitem - Fatal编程技术网

Java 向多个菜单添加JMenuItem

Java 向多个菜单添加JMenuItem,java,swing,jmenu,jmenuitem,Java,Swing,Jmenu,Jmenuitem,我希望将相同的JMenuItems添加到多个JMenu中,但它只在最后一个JMenu中显示。这是我写的代码。我希望为所有JMenu状态显示三个JMenu项。在这段代码中,前两个状态没有JMenuItems,所有三个状态都只有最后一个 import javax.swing.*; import java.awt.event.*; public class Menu extends JFrame{ public Menu() { super("Funky Menu"); JMenu

我希望将相同的JMenuItems添加到多个JMenu中,但它只在最后一个JMenu中显示。这是我写的代码。我希望为所有JMenu状态显示三个JMenu项。在这段代码中,前两个状态没有JMenuItems,所有三个状态都只有最后一个

import javax.swing.*;
import java.awt.event.*;  
public class Menu extends JFrame{
  public Menu()
  {
    super("Funky Menu");
JMenu [] states = new JMenu [3];
JMenuItem [] items = new JMenuItem [3];
//Initializing the items
items[0] = new JMenuItem("Industries");
items[0].setMnemonic('I');
items[1] = new JMenuItem("Hill Stations");
items[1].setMnemonic('H');
items[2] = new JMenuItem("Top Institutions");
items[2].setMnemonic('T');
//Initializing the states
//I've set the adjacent keys as the Mnemonics for easy user interaction
//though it is less intuitive, it can vary on the user preference.
states[0] =  new JMenu("Tamil Nadu"); states[0].setMnemonic('Q');
states[1] = new JMenu("West Bengal"); states[1].setMnemonic('W');
states[2] = new JMenu("Haryana"); states[2].setMnemonic('E');
//Adding all the items to each of the states
for(int i=0; i<3; ++i)
{
  for(int j=0; j<3; ++j)
  {
    states[i].add(items[j]);
  }
}
//adding action listener to menu items
for(int j=0; j<3; ++j)
{
  items[j].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt)
  {
    //The next few lines could be clubbed together in one but for
    //clarity sake I write them seperately
    JMenuItem currentItem = (JMenuItem) evt.getSource();
    String textToDisplay = currentItem.getText();
    System.out.println(textToDisplay + " : located in ...");
    //one liner : System.out.println(((JMenuItem) evt.getSource()).getText() + " : located in ...");
  }
  });
}
//finally to fix up the MenuBar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
for(int i=0; i<3; ++i)
{
  bar.add(states[i]);
}
getContentPane();
//TODO Create a JLabel add it to the contents
//Instead of writing to the console, update the frames text
setSize(500, 500);
setVisible(true);
 }

  public static void main(String[] args)
  {
    Menu app = new Menu();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
import javax.swing.*;
导入java.awt.event.*;
公共类菜单扩展了JFrame{
公共菜单()
{
超级(“时髦菜单”);
JMenu[]状态=新JMenu[3];
JMenuItem[]items=新的JMenuItem[3];
//初始化项目
项目[0]=新项目(“行业”);
项[0]。设置助记符('I');
项目[1]=新项目(“山站”);
项目[1]。设置助记符('H');
项目[2]=新项目(“顶级机构”);
项[2]。设置记忆法('T');
//初始化状态
//我已将相邻键设置为便于用户交互的助记符
//虽然它不那么直观,但它可以根据用户的偏好而变化。
states[0]=新的JMenu(“泰米尔纳德邦”);states[0]。setMnemonic('Q');
州[1]=新孟加拉邦(“西孟加拉邦”);州[1]。设置记忆法(“W”);
states[2]=新JMenu(“哈里亚纳”);states[2]。setMnemonic('E');
//将所有项目添加到每个状态

对于(int i=0;i我没有检查您的代码,但组件只能有一个父级。您需要创建单独的菜单项(可能使用相同的操作对象)。

我没有检查您的代码,但组件只能有一个父级。您需要创建单独的菜单项(可能使用相同的操作对象).

@Puce是正确的。取而代之的是,使用来封装共享功能,并让单个菜单项与
操作的公共实例一起构造。这是一个简单的示例。

@Puce是正确的。取而代之的是,使用来封装共享功能,并让单个菜单项与的公共实例一起构造
操作是一个简单的例子。

我应该用什么替换JMenuItem?我应该用什么替换JMenuItem?@nikhil and used ClassName菜单是AWT菜单的保留字,将其更改为MyMenu@nikhil并且used ClassName Menu是AWT菜单的保留字,将其更改为MyMenu