为什么我的JDialog中的JButton不工作?固定

时间:2014-07-28 19:36:05

标签: java swing model-view-controller mouseevent jbutton

这是有问题的JDialog View的构造函数:

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class AjoutPatient extends JDialog {

  private static final long serialVersionUID = 1L;
  private ControleurAjout ajoutControleur;
  private JPanel contentPane;
  private JPanel buttonPane;
  private JButton ajouterButton;
  private JButton annulerButton;

  AjoutPatient() {
      init();

      //Creation du controleur
      ajoutControleur = new ControleurAjout(this);

      //Ajout de listeners sur les composantes
      ajouterButton.addActionListener(ajoutControleur);
      annulerButton.addActionListener(ajoutControleur);

      //Reglage de visibilite et modal
      setModal(true);
      setVisible(true);
  }

  private void init(){
      setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      setTitle("Test JDialog");
      //Ajout panel principal
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);

      //Panel des bouton ajouter et annuler
      buttonPane = new JPanel();
      buttonPane.setBounds(5, 322, 304, 42);
      contentPane.add(buttonPane);
      buttonPane.setLayout(null);

      //Boutons ajouter et annuler
      ajouterButton = new JButton("Ajouter");
      ajouterButton.setBounds(130, 11, 81, 23);
      buttonPane.add(ajouterButton);
      annulerButton = new JButton("Annuler");
      annulerButton.setBounds(213, 11, 81, 23);
      buttonPane.add(annulerButton);
  }

  /*
   * Getters
   */
  public JButton getAjouterButton(){
      return ajouterButton;
  }

  public JButton getAnnulerButton(){
      return annulerButton;
  }
}

然后是JDialog控制器:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ControleurAjout implements ActionListener {

  private AjoutPatient vue;

  ControleurAjout(AjoutPatient ajout){
      vue = ajout;      
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == vue.getAjouterButton()){
            vue.dispose();
        }
    }else if(e.getSource() == vue.getAnnulerButton()){
            vue.dispose();
    }
  }

  public JButton getAjouterButton(){
      return ajouterButton;
  }

  public JButton getAnnulerButton(){
      return annulerButton;
  }
}

这是主要的JFrame控制器:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;

public class ControleurRecherche implements ActionListener {

  private FenetreRecherche vue;

  ControleurRecherche(FenetreRecherche recherche){
      vue = recherche;
  }

  //Ecouteurs d'evenement
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == vue.getAjouterButton()){
        ajout = new AjoutPatient();
        //modele.ajouterPatient(ajout);
    }else if(e.getSource() == vue.getFermerButton()){
            System.exit(0);
    } 
}

这是主要的JFrame方法:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class FenetreRecherche extends JFrame {

  private static final long serialVersionUID = 1L;
  private ControleurRecherche rechControleur;
  private JPanel contentPane;
  private JPanel buttonPane;
  private JButton ajouterButton;
  private JButton fermerButton;

  public static void main(String[] args) {

    FenetreRecherche recherche = new FenetreRecherche();
    recherche.setVisible(true);

  }

  FenetreRecherche() {
      init();
      //Creation du controlleur
      rechControleur = new ControleurRecherche(this);
      //Ajout de listeners sur les composantes
      ajouterButton.addActionListener(rechControleur);
      fermerButton.addActionListener(rechControleur);
  }

    //Initialiser le GUI
    private void init(){

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Test");
    setResizable(false);
    setBounds(100, 100, 600, 320);

    //Panel de contenu de la fenetre
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    contentPane.setLayout(new GridLayout(4, 1, 0, 0));

    //Panel des boutons menu
    buttonPane = new JPanel();
    FlowLayout flowButtonPane = (FlowLayout) buttonPane.getLayout();
    flowButtonPane.setVgap(20);
    flowButtonPane.setAlignment(FlowLayout.RIGHT);
    contentPane.add(buttonPane);

    //Boutons menu
    ajouterButton = new JButton("Ajouter");
    buttonPane.add(ajouterButton);
    fermerButton = new JButton("Fermer");
    buttonPane.add(fermerButton);       
  }
  /*
   * Getters
   */
  public JButton getAjouterButton(){
      return ajouterButton;
  }
  public JButton getFermerButton(){
      return fermerButton;
  }

}

尽管如此,我希望问题能够在没有问题的情况下解决。 只需要在构造函数的末尾更改JDialog的setVisible(true)。编辑以便在此处获得最终的工作版本。

1 个答案:

答案 0 :(得分:3)

  

我使用MousePressed事件打开JDialog

不要使用MouseListener~

您需要使用ActionListener。阅读How to Use Buttons上Swing教程中的部分,了解更多信息和工作示例。