java中的按钮没有做动作

时间:2016-01-13 17:32:45

标签: java database

我正在从事Java任务。我有很多类链接在一起,其中一个构造函数如下:

public class RemovePatientForm extends JFrame implements ActionListener {


        JPanel northPanel = new JPanel();
        JPanel southPanel = new JPanel();
        JPanel midPanel = new JPanel();
        JLabel removeLabel = new JLabel("Please type in the ID of the patient to be removed");
        JLabel idLabel= new JLabel("ID");
        JTextField idText=new JTextField();
        JButton submit = new JButton("Submit");
        JButton reset = new JButton("Clear");
        boolean externalForm = false;



        public RemovePatientForm(){

            setTitle("Removing a patient");
            setLayout(new BorderLayout());
            setSize(400,200);
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(false);

            add("North", northPanel);
            add("South", southPanel);
            northPanel.add(removeLabel);
            southPanel.add(submit);
            southPanel.add(reset);

            add(idLabel);
            add(idText);

            submit.addActionListener(this);

        }




        @Override
        public void actionPerformed(ActionEvent arg0) {

            if(arg0.getSource()==submit){

                if(!(idText.getText().equals(""))){
                    int selectedvalue = JOptionPane.showConfirmDialog(null, "Do you want to proceed with the deletion?", "do you want to proceed with the deletion?", JOptionPane.YES_NO_OPTION);

                    if(selectedvalue==JOptionPane.YES_OPTION){

                        int id=Integer.parseInt(idText.getText());
                        if(searchForId(id)){

                            removeToDatabase();
                            dispose();
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"This ID is not available!","Warning",JOptionPane.ERROR_MESSAGE);
                        }
                    }               
                    else{
                        JOptionPane.showMessageDialog(null, "Nothing is affected!");                                    
                    }
                }

                else{
                    JOptionPane.showMessageDialog(null,"You have to fill the ID number!","Warning",JOptionPane.ERROR_MESSAGE);
                }
            }

            if(arg0.getSource()==reset && externalForm==false){
                idText.setText("");
            }

        }

这里的问题是,当我按下提交按钮时,一切正常并按照代码中的说明进行操作。

但是,如果按重置按钮,则不会发生任何事情。

您认为解决方案是什么?这段代码足以确定问题吗?

1 个答案:

答案 0 :(得分:5)

你没有为action listener添加reset button ..我想你忘了它。尝试添加它,它应该工作。

在构造函数中添加此代码:

reset.addActionListener(this);