为什么我的JButton Action Listener没有响应?

时间:2016-03-13 23:38:22

标签: java eclipse swing user-interface jbutton

我在eclipse for Mac上创建了一个小型GUI应用程序,使用swing来创建和浏览包含学生信息的二进制搜索树。我已经创建了JButtons来执行各种操作,例如从数据文件创建二进制搜索树,逐个节点添加学生信息,以及手动浏览数据。这是有效的,GUI的屏幕截图链接如下。 但是没有按钮正常工作!!我已经实现了一个内部的Listener类。按钮显示并可以单击,但没有任何反应。

Current GUI produced by code below

感谢任何帮助!

public class MyFrame extends JFrame 
{
    /**
     * Member variables include a serialversionUID
     * various required Labels and Buttons,
     * the container, a listener and the BST
     */
    static final long serialVersionUID = 2L;

    private JLabel titleLabel, mainLabel;
    private JButton insertButton, findButton,   
        browseButton, createButton;

    private Container c;

    MyListener listener;

    BinSearchTree bst;
    /**
     * Inner class MyListener implements the 
     * ActionListener used by the program to determine
     * which button was clicked and how to proceed
     */
    class MyListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            if (e.getSource() == insertButton)
            {
                JTextField field1 = new JTextField();
                JTextField field2 = new JTextField();
                JTextField field3 = new JTextField();
                JTextField field4 = new JTextField();
                Object[] message = {
                    "StudentID:", field1,
                    "Faculty", field2,
                    "Major:", field3,
                    "Year:", field4,
                };
                int option = JOptionPane.showConfirmDialog(null,message,"Please Enter the following:", JOptionPane.OK_CANCEL_OPTION);
                String studentID="";
                String faculty="";
                String major="";
                String year="";
                if (option == JOptionPane.OK_OPTION)
                {
                    studentID = field1.getText();
                    faculty = field2.getText();
                    major = field3.getText();
                    year = field4.getText();
                }
                mainLabel.setText(mainLabel.getText() + 
                        studentID + '\t' + faculty + '\t' + major 
                        + '\t' + year + '\n');

                bst.insert(studentID, faculty, major, year);
            }

            else if (e.getSource() == findButton)
            {
                String studentID = JOptionPane.
                        showInputDialog("Please enter a Student ID:");

                Node student = bst.find(bst.root,studentID);

                JOptionPane.showMessageDialog(null, student.toString());
            }

            else if (e.getSource() == browseButton)
            {
                c.add("Center",mainLabel);
                c.add("East",new Scrollbar(Scrollbar.VERTICAL));
                validate();
                repaint();
            }

            else if (e.getSource() == createButton)
            {

                String fileName = JOptionPane.
                        showInputDialog("Please specify which file to use:");
                try
                {
                    Scanner scan = new Scanner(new File(fileName));

                    bst = createTree(scan);

                    scan.close();

                } catch(FileNotFoundException fnfe) 
                {
                    System.err.println(fnfe.getMessage());

                }
            }
        }
    }

    /**
     * Constructor takes the frame handle as a string argument
     * and initiates the member Buttons and Labels.
     * The inner class MyListener
     * 
     * @param MyFrame handle
     */
    public MyFrame(String s)
    {
        super(s);
        titleLabel = new JLabel("Title");
        titleLabel.setText("Student Information");

        listener = new MyListener();

        insertButton = new JButton("Insert");
        findButton = new JButton("Find");
        browseButton = new JButton("Browse");
        createButton = new JButton("Create Tree From File");

        JPanel buttonPanel = new JPanel();

        buttonPanel.setLayout(new FlowLayout());

        buttonPanel.add(insertButton);
        buttonPanel.add(findButton);
        buttonPanel.add(browseButton);
        buttonPanel.add(createButton);

        c = getContentPane();
        c.setLayout(new BorderLayout());

        c.add("North", titleLabel);
        c.add("South", buttonPanel);
    }
    /**
     * Creates the member Binary Search Tree and populates
     * it with the input from the text file specified
     * 
     * @param scanner
     * 
     * @return populated Binary Search Tree
     */
    public BinSearchTree createTree(Scanner scan)
    {
        BinSearchTree bstree = new BinSearchTree();

        while(scan.hasNext())
        {
            mainLabel.setText("");

            String[] lineData = scan.nextLine().split(" ");

            mainLabel.setText
                (mainLabel.getText() + scan.nextLine() + "\n");

            bstree.insert(lineData[0],lineData[1],
                    lineData[2],lineData[3]);
        }

        return bstree;

    }

    public static void main(String[] args)
    {
        MyFrame frame = new MyFrame("Frame1");
        frame.pack();
        frame.setVisible(true);
    }    
}   

2 个答案:

答案 0 :(得分:2)

您已创建了按钮和侦听器,但您需要将侦听器添加到按钮中。

信息:https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

E.g:

insertButton.addActionListener(listener);

答案 1 :(得分:1)

您正在创建侦听器,但您没有注册按钮。

因此,按钮永远不会通知听众任何点击。

insertButton.addActionListener(listener);

将激活insertButton。