English 中文(简体)
JMenu 项目——点击——行动—— Java
原标题:JMenuitem - click - action - Java

I have written code to perform click event on JMenuItem but that is not firing when I am debugging. (I know that I am not supposed to ask these questions in this forum but I am new to this forum)

public class ClsMenu extends JMenuItem implements ActionListener {

  JTextArea output;
  JScrollPane scrollPane;

  public ClsMenu(String text)
  {
    super(text);
    addActionListener(this);
  }

  public JMenuBar createMenu()
  {
    JMenuBar menuBar;
    JMenu menuFood,menuDrinks,menuParty;
    JMenuItem foodItem;


    menuBar=new JMenuBar();


    menuFood=new JMenu("Food");
    foodItem=new JMenuItem("Pizza");
    menuFood.add(foodItem);
    menuBar.add(menuFood);
    return menuBar;
  }
  public void createGUIandShow()
  {
    JFrame frame = new JFrame("Restuarant");
    frame.setJMenuBar(createMenu());
  }

  public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    System.out.println("item clicked"+e.getActionCommand());
  }
}

在这项呼吁中,我设立了一个物体。

public class ClsMenuDisp {
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    ClsMenu menu=new ClsMenu("testitem");
    menu.createGUIandShow();
  }
}
最佳回答

页: 1 JMenu。 你正在做的是,一个有行动听众的菜单项目。 然后使用该菜单项目的一种实例方法,建立一个完全无关的编码/。 JMenuItem 页: 1 JFrame。

问题回答

页: 1

请在您感兴趣的物体上添加<条码>行动指示/代码”,以便该物体在适当时间警告听众。 就你而言,你对菜单感兴趣,因此,你应当增加你的听众。

一些参考资料链接:

  1. The Swing tutorial about menus
  2. The Observer design pattern, which is what you are using when adding listeners
  3. How to write an ActionListener tutorial

我写了一些可能有用的守则,以作为实例:链接here

http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html” rel=“nofollow” here。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签