简体   繁体   English

单击时JMenuItem如何接收任务?

[英]How does JMenuItem receive tasks when clicking?

I'm trying to create a Menu that involves saving and reading files, in each menu that saves and reads the file, will be JMenuItem . 我正在尝试创建一个涉及保存和读取文件的菜单,在每个保存和读取文件的菜单中,将为JMenuItem

I want each JMenuItem to be active when clicked, I used the same code as below but the program has the following error. 我希望每个JMenuItem在单击时都处于活动状态,我使用了与下面相同的代码,但是该程序具有以下错误。 I used the addActionListener command for the buttons and they work normally, and when I did the JMenuItem , I got an error. 我对按钮使用了addActionListener命令,它们可以正常工作,当我执行JMenuItem ,出现了错误。

Here is my code: 这是我的代码:

public void createMenu(JFrame chuongTrinh){
   JMenuBar barMenu = new JMenuBar();
   JMenu fileMenu = new JMenu("File");
   fileMenu.setIcon(new ImageIcon("pic/system.png"));
   barMenu.add(fileMenu);

   fileMenu.addSeparator();

   JMenu fileMenuLuu = new JMenu("Lưu File");
   fileMenuLuu.setIcon(new ImageIcon("pic/saveFile.png"));
   fileMenu.add(fileMenuLuu);

   JMenuItem fileMenuLuuTxt = new JMenuItem("Text File");
   fileMenuLuuTxt.setIcon(new ImageIcon("pic/txtFile.png"));
   fileMenuLuu.add(fileMenuLuuTxt);

public formSinhVien(){
        fileMenuLuuTxt.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
}

This is its fault: 这是它的错:

"Exception in thread "main" java.lang.NullPointerException “线程“主”中的异常” java.lang.NullPointerException

The problem seems, that you haven´t initialized the fileMenuLuuTxt . 问题似乎在于您fileMenuLuuTxt初始化fileMenuLuuTxt I guess you have an instance variable fileMenuLuuTxt but didn´t assign it in your createMenu method. 我猜您有一个实例变量fileMenuLuuTxt但是没有在createMenu方法中分配它。 So change your code as follows: 因此,如下更改代码:

    private JMenuItem fileMenuLuuTxt; // your instance variable

        public void createMenu(JFrame chuongTrinh){
           JMenuBar barMenu = new JMenuBar();
           JMenu fileMenu = new JMenu("File");
           fileMenu.setIcon(new ImageIcon("pic/system.png"));
           barMenu.add(fileMenu);

           fileMenu.addSeparator();

           JMenu fileMenuLuu = new JMenu("Lưu File");
           fileMenuLuu.setIcon(new ImageIcon("pic/saveFile.png"));
           fileMenu.add(fileMenuLuu);

           fileMenuLuuTxt = new JMenuItem("Text File"); // this is the change
           fileMenuLuuTxt.setIcon(new ImageIcon("pic/txtFile.png"));
           fileMenuLuu.add(fileMenuLuuTxt);
       }

for me correctly is below. 对我来说正确地在下面。 You create unnecesary method formSinhVien 您创建不必要的方法formSinhVien

public void createMenu(JFrame chuongTrinh){
       JMenuBar barMenu = new JMenuBar();
       JMenu fileMenu = new JMenu("File");
       fileMenu.setIcon(new ImageIcon("pic/system.png"));
       barMenu.add(fileMenu);

       fileMenu.addSeparator();

       JMenu fileMenuLuu = new JMenu("Lưu File");
       fileMenuLuu.setIcon(new ImageIcon("pic/saveFile.png"));
       fileMenu.add(fileMenuLuu);

       JMenuItem fileMenuLuuTxt = new JMenuItem("Text File");
       fileMenuLuuTxt.setIcon(new ImageIcon("pic/txtFile.png"));
       fileMenuLuuTxt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
       fileMenuLuu.add(fileMenuLuuTxt);
}

If you need get access to object fileMenuLuuTxt from outside of this method, you can create field JMenuItem fileMenuLuuTxt; 如果您需要从此方法之外访问对象fileMenuLuuTxt ,则可以创建字段JMenuItem fileMenuLuuTxt; in your class and then modify line as below fileMenuLuuTxt = new JMenuItem("Text File"); 在您的班级中,然后如下所示修改行fileMenuLuuTxt = new JMenuItem("Text File"); - remove JMenuItem in method. -删除方法中的JMenuItem。 That meens you declare class field and then initialize this field in method. 这意味着您声明了类字段,然后在方法中初始化了该字段。 In your case you declare and initialize variable in method and you have no access to this object from outside of method. 在您的情况下,您可以在method中声明和初始化变量,并且您无法从method外部访问此对象。

Maybe you are adding the listener before instantiating the object fileMenuLuuTxt. 也许您在实例化对象fileMenuLuuTxt之前添加了侦听器。

May you try: 可以尝试:

  1. You must declare the variable JMenuItem fileMenuLuTxt globally to the class (instead of within a method). 您必须在类中全局声明变量JMenuItem fileMenuLuTxt (而不是在方法中)。
  2. The method public void createMenu(JFrame chuongTrinh) is executed to instantiate the previous variable. 执行方法public void createMenu(JFrame chuongTrinh)实例化先前的变量。
  3. That the method public formSinhVien() is executed after the previous one. 该方法public formSinhVien()在上一个方法之后执行。

If you can't control the execution order, a solution could be to initialize the global variable like this: private JMenuItem fileMenuLuTxt = new JMenuItem() //Add the necessary parameters by default. 如果您无法控制执行顺序,则解决方案可能是这样初始化全局变量: private JMenuItem fileMenuLuTxt = new JMenuItem() //Add the necessary parameters by default.

The bad thing about this is that when you add the listener before the "public void createMenu(JFrame chuongTrinh)" method is executed, it will be lost because you have created a new instance. 不好的事情是,当您在执行“ public void createMenu(JFrame chuongTrinh)”方法之前添加侦听器时,由于创建了新实例,该侦听器将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM