为什么动作听众不起作用?

时间:2015-06-09 22:08:37

标签: java swing jbutton actionlistener

ActionListener已正确设置,但出于某种原因,按下其中一个按钮时无法正常工作。有任何想法吗?我一直在寻找方法,但似乎没有任何效果。这是my code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Master extends JPanel implements ActionListener {
    final static String SIGNIN = "Sign In";
    final static String SIGNOUT = "Sign Out";
    final static String OTHER = "Other Stuff";
    final static int extraWindowWidth = 100;

    public String[] Names, SecurityNumber;
    public int FirstService = 0;
    public int SecondService = 0;
    public int counter = 0;

    protected JTextField ChildName, ChildSecurityNum, SecurityNum;
    protected JButton SignOut, FirstServiceButton, SecondServiceButton,
            FirstServiceTotal, SecondServiceTotal;

    public Master(Container pane) {
        // Set up Arrays for storage
        SecurityNumber = new String[300];
        Names = new String[300];

        // Text Fields
        ChildName = new JTextField("");
        ChildName.setEditable(false);
        ChildName.setColumns(10);

        SecurityNum = new JTextField("");
        SecurityNum.setEditable(true);
        SecurityNum.setColumns(2);

        ChildSecurityNum = new JTextField("");
        ChildSecurityNum.setEditable(true);
        ChildSecurityNum.setColumns(5);

        // Buttons
        SignOut = new JButton("Sign Out");
        SignOut.setActionCommand("signOut");
        SignOut.setEnabled(true);

        FirstServiceButton = new JButton("Sign In First Service");
        FirstServiceButton.setActionCommand("signinfirst");
        FirstServiceButton.setEnabled(true);

        SecondServiceButton = new JButton("Sign In Second Service");
        SecondServiceButton.setActionCommand("signinsecond");
        SecondServiceButton.setEnabled(true);

        FirstServiceTotal = new JButton("First Service Total Count");
        FirstServiceTotal.setActionCommand("firstservicetotal");
        FirstServiceTotal.setEnabled(true);

        SecondServiceTotal = new JButton("Second Service Total Count");
        SecondServiceTotal.setActionCommand("secondservicetotal");
        SecondServiceTotal.setEnabled(true);

        SecondServiceButton.addActionListener(this);
        FirstServiceButton.addActionListener(this);
        FirstServiceTotal.addActionListener(this);
        SecondServiceTotal.addActionListener(this);
        SignOut.addActionListener(this);

        JTabbedPane tabbedPane = new JTabbedPane();

        // Create the "cards".
        JPanel card1 = new JPanel() {
            // Make the panel wider than it really needs, so
            // the window's wide enough for the tabs to stay
            // in one row.
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                size.width += extraWindowWidth;
                return size;
            }
        };
        card1.add(new Label("Child's Name :"));
        card1.add(new JTextField("", 15));
        card1.add(new Label("Child's Security Number :"));
        card1.add(ChildSecurityNum);
        card1.add(new Label("Child's Grade :"));
        card1.add(new JTextField("", 2));
        card1.add(FirstServiceButton);
        card1.add(SecondServiceButton);

        JPanel card2 = new JPanel();
        card2.add(new Label("Enter Security tag # :"));
        card2.add(new JTextField("", 5));
        card2.add(SignOut);
        card2.add(new Label("Child's Name :"));
        card2.add(ChildName);

        JPanel card3 = new JPanel();
        card3.add(new JButton("Print Attendence Sheet"));
        card3.add(FirstServiceTotal);
        card3.add(SecondServiceTotal);

        tabbedPane.addTab(SIGNIN, card1);
        tabbedPane.addTab(SIGNOUT, card2);
        tabbedPane.addTab(OTHER, card3);

        pane.add(tabbedPane, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        if ("signOut".equals(e.getActionCommand())) {
            ChildName.setText("");
            SecurityNum.setText("");
            int counter = 0;
            for (String a : SecurityNumber) {
                if (a.equals(SecurityNum.getText())) {
                    ChildName.setText(Names[counter]);
                } else
                    counter++;
            }
        }
        if ("signinfirst".equals(e.getActionCommand())) {
            FirstService++;
            counter++;
            Names[counter] = ChildName.getText();
            SecurityNumber[counter] = SecurityNum.getText();
            ChildName.setText("");
            SecurityNum.setText("");
        }
        if ("signinsecond".equals(e.getActionCommand())) {
            SecondService++;
            counter++;
            Names[counter] = ChildName.getText();
            SecurityNumber[counter] = SecurityNum.getText();
            ChildName.setText("");
            SecurityNum.setText("");
        }
        if ("FirstServiceTotal".equals(e.getActionCommand())) {
            JOptionPane.showMessageDialog(null, "There are " + FirstService
                    + "Kids.");
        }
    }

    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Sign in and Sign out");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        Master demo = new Master(frame.getContentPane());
        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

您的ActionListeners没有任何问题(通过在按下按钮时打印“hello world”确定)。问题是你从来没有将SecurityNum添加到card1,这就是为什么它的文本没有被清除。

答案 1 :(得分:0)

由于您已经拥有Master()构造函数之外的按钮,您可能只想这样做以添加动作侦听器。

举个例子:

signOut.addActionListener(this);

这是为了获得行动

public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if(o.equals(signOut)) {
        // Code to signout.
    }
}

您可能还想重命名变量,使第一个字符为小写(最终变量除外)。这会使阅读更容易。