简体   繁体   中英

Exit Button Doesn't Work

Getting a bit into GUIs and trying to make something simple. However, I seem to be getting stuck on just making a simple "Exit" button. I am using the WindowsBuilder plugin for Eclipse to help me.

private void initialize()
{
    mainWindowFrame = new JFrame();
    mainWindowFrame.setTitle("Lock and Log");
    mainWindowFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/lockandlog/main/resources/icon.png")));
    mainWindowFrame.setResizable(false);
    mainWindowFrame.setBounds(100, 100, 854, 480);
    mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindowFrame.getContentPane().setLayout(null);

    final JTextArea logDisplay = new JTextArea();
    logDisplay.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
    logDisplay.setEditable(false);
    logDisplay.setBounds(259, 11, 579, 250);
    mainWindowFrame.getContentPane().add(logDisplay);

    JLabel viewCmdDisplayLbl = new JLabel("View Command Display:");
    viewCmdDisplayLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
    viewCmdDisplayLbl.setBounds(259, 272, 133, 14);
    mainWindowFrame.getContentPane().add(viewCmdDisplayLbl);

    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    textArea.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
    textArea.setBounds(259, 297, 579, 104);
    mainWindowFrame.getContentPane().add(textArea);

    JToolBar toolBar = new JToolBar();
    toolBar.setBounds(0, 412, 848, 16);
    mainWindowFrame.getContentPane().add(toolBar);

    JPanel manualGrp = new JPanel();
    manualGrp.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    manualGrp.setBounds(10, 11, 239, 72);
    mainWindowFrame.getContentPane().add(manualGrp);
    manualGrp.setLayout(null);

    manualTxt = new JTextField();
    manualTxt.setBounds(10, 7, 219, 20);
    manualGrp.add(manualTxt);
    manualTxt.setColumns(10);

    JButton manualBtn = new JButton("Manual Override");
    manualBtn.setFont(new Font("Tahoma", Font.BOLD, 12));
    manualBtn.setBounds(10, 38, 219, 23);
    manualGrp.add(manualBtn);

    JMenuBar menuBar = new JMenuBar();
    mainWindowFrame.setJMenuBar(menuBar);

    final JMenu fileBtn = new JMenu("File");
    fileBtn.addMouseListener(new MouseAdapter() //This works
    {
        @Override
        public void mouseEntered(MouseEvent e)
        {
            fileBtn.setSelected(true);
        }
        @Override
        public void mouseExited(MouseEvent e)
        {
            fileBtn.setSelected(false);
        }
    });
    menuBar.add(fileBtn);

    JMenuItem exitBtn = new JMenuItem("Exit");
    //Code is definitely passing through here.
    exitBtn.addMouseListener(new MouseAdapter() //This doesn't work?
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            logDisplay.setText("Test"); //This isn't even being called!
            System.exit(0);
        }
    });
    //Definitely adds the button so code is passing through here.
    fileBtn.add(exitBtn);
}

That's the whole initialize class for now. But if you would note near the bottom I have an event for the Exit button to where if you click on it the program should close. Except, when I click on the button nothing happens. Trying out other events like that in the fileBtn that works great. So what am I doing wrong?

Don't use mouseListeners, use ActionListeners.

import java.awt.event.*;


public class SOQ3
{
   JFrame mainWindowFrame;

   public static void main(String[] args)
   {

      SOQ3 s = new SOQ3();

   }

   public SOQ3()
   {

      initialize();

   }

   private void initialize()
   {
      mainWindowFrame = new JFrame();
      mainWindowFrame.setTitle("Lock and Log");
      mainWindowFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/lockandlog/main/resources/icon.png")));
      mainWindowFrame.setResizable(false);
      mainWindowFrame.setBounds(100, 100, 854, 480);
      mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainWindowFrame.getContentPane().setLayout(null);

      final JTextArea logDisplay = new JTextArea();
      logDisplay.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
      logDisplay.setEditable(false);
      logDisplay.setBounds(259, 11, 579, 250);
      mainWindowFrame.getContentPane().add(logDisplay);

      JLabel viewCmdDisplayLbl = new JLabel("View Command Display:");
      viewCmdDisplayLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
      viewCmdDisplayLbl.setBounds(259, 272, 133, 14);
      mainWindowFrame.getContentPane().add(viewCmdDisplayLbl);

      JTextArea textArea = new JTextArea();
      textArea.setEditable(false);
      textArea.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
      textArea.setBounds(259, 297, 579, 104);
      mainWindowFrame.getContentPane().add(textArea);

      JToolBar toolBar = new JToolBar();
      toolBar.setBounds(0, 412, 848, 16);
      mainWindowFrame.getContentPane().add(toolBar);

      JPanel manualGrp = new JPanel();
      manualGrp.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
      manualGrp.setBounds(10, 11, 239, 72);
      mainWindowFrame.getContentPane().add(manualGrp);
      manualGrp.setLayout(null);

      manualTxt = new JTextField();
      manualTxt.setBounds(10, 7, 219, 20);
      manualGrp.add(manualTxt);
      manualTxt.setColumns(10);

      JButton manualBtn = new JButton("Manual Override");
      manualBtn.setFont(new Font("Tahoma", Font.BOLD, 12));
      manualBtn.setBounds(10, 38, 219, 23);
      manualGrp.add(manualBtn);

      JMenuBar menuBar = new JMenuBar();
      mainWindowFrame.setJMenuBar(menuBar);

      final JMenu fileBtn = new JMenu("File");
      fileBtn.addMouseListener(
            new MouseAdapter() //This works
            {
               @Override
               public void mouseEntered(MouseEvent e)
               {
                  fileBtn.setSelected(true);
               }
               @Override
               public void mouseExited(MouseEvent e)
               {
                  fileBtn.setSelected(false);
               }
            });
      menuBar.add(fileBtn);

      JMenuItem exitBtn = new JMenuItem("Exit");
      //Code is definitely passing through here.
      exitBtn.addActionListener(
            new ActionListener() //This doesn't work?
            {
               @Override
               public void actionPerformed(ActionEvent e)
               {
                  logDisplay.setText("Test"); //This isn't even being called!
                  System.exit(0);
               }
            });
      //Definitely adds the button so code is passing through here.
      fileBtn.add(exitBtn);
   }

}

Now as for the reason behind this, you were trying to use MouseListeners, while MouseListeners are good, you need to GET THE SOURCE of what you are listening to. Simply adding a listener will now do anything like it would if it was an ActionListener, part of the reason why I chose to use one. Also, tip for you, using ActionListeners listen for an Action done by the object it is attached to, MouseListeners listen to your and everything it does, whether or not it even has anything to do with your program. In the future, when using listeners, ensure that they listen only to what is absolutely necessary to your program. If you don't need to know the position of the mouse, don't follow it.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Typically this is how you would capture the exit event.

For your custom Exit button, instead of a MouseListener , try just a plain 'ol ActionListener

Any event on the button will then be captured, and your code gracefully exited.

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