繁体   English   中英

为什么我的JButton动作监听器没有响应?

[英]Why are my JButton Action Listeners not responding?

我在eclipse上为Mac开发了一个小型GUI应用程序,它使用swing创建和浏览包含学生信息的Binary Search Tree。 我创建了JButton,以执行各种操作,例如从数据文件创建二进制搜索树,逐节点添加学生信息以及手动浏览数据。 它正在工作,并且GUI的屏幕快照在下面链接。 但是这些按钮都不起作用!! 我已经实现了一个内部的Listener类。 显示按钮并可以单击,但是什么也没有发生。

当前的GUI由下面的代码生成

任何帮助表示赞赏!

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

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

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

例如:

insertButton.addActionListener(listener);

您正在创建侦听器,但未在按钮上注册。

因此,这些按钮永远不会将任何点击通知您的听众。

insertButton.addActionListener(listener);

将激活insertButton。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM