2016-01-13 66 views
0

我正在开发一个Java任务。我有很多类联系在一起,并一个构造函数如下: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(""); 
      } 

     } 

在这里的问题是,当我按下提交按钮,一切都很好,而且工作的代码中编写的。

但是,如果我按重置按钮,什么都没有发生。

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

回答

5

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

您的构造函数中添加以下代码:

reset.addActionListener(this);