我的system.exit代码的位置错误

时间:2011-03-21 17:06:31

标签: java user-interface swing

我的用户表单上有一个退出按钮,我认为我在错误的位置有代码,因为它不起作用。我试过移动它,但没有运气。这是代码的底部。

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();
    }
}

5 个答案:

答案 0 :(得分:3)

actionPerformed方法中,您处理3个不同的事件来源(cmdGoodcmdBadcmdAverage),但您无法处理cmdExit,甚至虽然它使用相同的ActionListener。以与处理其他3相同的方式添加对该源的处理。

答案 1 :(得分:1)

cmdExit方法中的actionPerformed没有任何条款。添加一个:

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

答案 2 :(得分:1)

原因已经解释过(cmdExit_pressed从未使用过)。以下是您的程序更接近今天标准的方式:

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();
    }
}

这样每个按钮都有自己的ActionListener,你不必进行if (source == ...)比较。

更好的方法是在这里使用Action个对象(即AbstractAction的匿名子类)。

答案 3 :(得分:0)

你的cmdExit_pressed没有被任何东西调用。你为什么期待它?

所有操作都必须通过ActionListener界面处理。

我强烈建议您每个按钮都有一个单独的ActionListener,而不是一个按钮,其中包含if / else循环。

答案 4 :(得分:0)

呃,那部分没事。

问题是你没有给那个按钮ActionListener