开发者

Wrong location of my system.exit code

开发者 https://www.devze.com 2023-02-18 15:09 出处:网络
I have an exit button on my user form and I think I have the code for it in the wrong spot because it doesn\'t work.I\'ve tried moving it around, but have had no luck.It\'s toward the bottom of the co

I have an exit button on my user form and I think I have the code for it in the wrong spot because it doesn't work. I've tried moving it around, but have had no luck. It's toward the bottom of the code.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;

public class DayGUI extends JFrame {
    private JFrame mainFrame;
    private JButton cmdGood;
    private JButton cmdBad;
    private JButton cmdAverage;
    private JButton cmdExit;

    public DayGUI()
    {
        mainFrame = new JFrame("How are you feeling today?");

        cmdGood = new JButton("Good");
         cmdGood.setBackground(Color.GREEN);

        cmdBad = new JButton("Bad");
         cmdBad.setBackground(Color.RED);

         cmdAverage = new JButton("Average");
         cmdAverage.setBackground(Color.WHITE);

         cmdExit = new JButton("Exit");
         cmdExit.setBackground(Color.WHITE);

        Container c = mainFrame.getContentPane();
        c.setLayout(new FlowLayout());

        c.add(cmdGood);
        c.add(cmdBad);
        c.add(cmdAverage);
        c.add(cmdExit);

        cmdGood.setMnemonic('G');
        cmdBad.setMnemonic('B');
        cmdAverage.setMnemonic('A');
        cmdExit.setMnemonic('E');

        mainFrame.setSize(280, 100);开发者_如何学运维
        mainFrame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        ButtonsHandler bhandler = new ButtonsHandler();
        cmdGood.addActionListener(bhandler);
        cmdBad.addActionListener(bhandler);
        cmdAverage.addActionListener(bhandler);
        cmdExit.addActionListener(bhandler);
        mainFrame.show();
        }
    class ButtonsHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)      
        {       
            if (e.getSource() == cmdGood)
                JOptionPane.showMessageDialog(null, "Today is a good day!",
                        "I'm Feeling Happy!", JOptionPane.INFORMATION_MESSAGE);
            else if (e.getSource() == cmdBad)
                JOptionPane.showMessageDialog(null, "I'm having a bad day today!",
                        "I'm Feeling Sad!", JOptionPane.INFORMATION_MESSAGE);
            else if (e.getSource() == cmdAverage)
                JOptionPane.showMessageDialog(null, "I'm having an average day!",
                        "I'm Feeling In The Middle", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    private void cmdExit_pressed(){
        System.exit(0);
        }
    public static void main(String args[])
    {
        new DayGUI();
    }
}


In your actionPerformed method you handle 3 different event source (cmdGood, cmdBad, cmdAverage), but you don't handle cmdExit, even though it uses the same ActionListener. Add the handling of that source in the same way you handle the other 3.


You have no clause for cmdExit inside your actionPerformed method. Add one:

else if (e.getSource() == cmdExit)
    cmdExit_pressed();


The reason was already explained (cmdExit_pressed is never used). Here is a way your program would be written nearer to today's standards:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;

public class DayGUI extends JFrame {
    private JFrame mainFrame;
    private JButton cmdGood;
    private JButton cmdBad;
    private JButton cmdAverage;
    private JButton cmdExit;

    public DayGUI()
    {
        mainFrame = new JFrame("How are you feeling today?");

        cmdGood = new JButton("Good");
        cmdGood.setBackground(Color.GREEN);

        cmdBad = new JButton("Bad");
        cmdBad.setBackground(Color.RED);

        cmdAverage = new JButton("Average");
        cmdAverage.setBackground(Color.WHITE);

        cmdExit = new JButton("Exit");
        cmdExit.setBackground(Color.WHITE);

        Container c = mainFrame.getContentPane();
        c.setLayout(new FlowLayout());

        c.add(cmdGood);
        c.add(cmdBad);
        c.add(cmdAverage);
        c.add(cmdExit);

        cmdGood.setMnemonic('G');
        cmdBad.setMnemonic('B');
        cmdAverage.setMnemonic('A');
        cmdExit.setMnemonic('E');

        mainFrame.setSize(280, 100);
        mainFrame.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });

        cmdGood.addActionListener(new ActionListener() {
                public void actionPerformed() {
                    JOptionPane.showMessageDialog(null, "Today is a good day!",
                                                  "I'm Feeling Happy!", JOptionPane.INFORMATION_MESSAGE);
                }
            });
        cmdBad.addActionListener(new ActionListener() {
                public void actionPerformed() {
                    JOptionPane.showMessageDialog(null, "I'm having a bad day today!",
                                                  "I'm Feeling Sad!", JOptionPane.INFORMATION_MESSAGE);
                }
            });
        cmdAverage.addActionListener(new ActionListener() {
                public void actionPerformed() {
                    JOptionPane.showMessageDialog(null, "I'm having an average day!",
                                                  "I'm Feeling In The Middle", JOptionPane.INFORMATION_MESSAGE);
                }
            });
        cmdExit.addActionListener(new ActionListener() {
                public void actionPerformed() {
                    System.exit();
                }
            });

        mainFrame.show();
    }
    public static void main(String args[])
    {
        new DayGUI();
    }
}

This way each button gets his own ActionListener, and you don't have to do this if (source == ...) comparisons.

Even better would be using Action objects (i.e. anonymous subclasses of AbstractAction) here.


Your cmdExit_pressed isn't being called by anything. Why were you expecting it to be?

All actions have to be handled through the ActionListener interface.

I strongly suggest that you have a separate ActionListener per button, rather than one for them all with a if/else loop inside.


Er, that part is fine.

The problem is you don't give that button an ActionListener

0

精彩评论

暂无评论...
验证码 换一张
取 消