简体   繁体   中英

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 .

I want each JMenuItem to be active when clicked, I used the same code as below but the program has the following error. I used the addActionListener command for the buttons and they work normally, and when I did the JMenuItem , I got an error.

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

The problem seems, that you haven´t initialized the fileMenuLuuTxt . I guess you have an instance variable fileMenuLuuTxt but didn´t assign it in your createMenu method. 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

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; in your class and then modify line as below fileMenuLuuTxt = new JMenuItem("Text File"); - remove JMenuItem in method. 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.

Maybe you are adding the listener before instantiating the object fileMenuLuuTxt.

May you try:

  1. You must declare the variable JMenuItem fileMenuLuTxt globally to the class (instead of within a method).
  2. The method public void createMenu(JFrame chuongTrinh) is executed to instantiate the previous variable.
  3. That the method public formSinhVien() is executed after the previous one.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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