【问题标题】:SWING actionPerformed never calledSWING actionPerformed 从未调用
【发布时间】:2015-12-19 18:49:32
【问题描述】:

在这段代码中,我有一个添加了 ActionListener 的 jbutton (BoutonListener),但是当单击该按钮时,永远不会调用方法 actionPerformed()。 我是 SWING 的新手,所以我可能忽略了一些重要的事情,但我没有在这里错过什么,请帮忙。

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;


public class Plateau{
Fenetre f;
Modele mo;
CaseListener co;
BoutonListener li; 

public Plateau(int dim){
this.f = new Fenetre(dim);
this.mo = new Modele();
this.co = new CaseListener();
this.li = new BoutonListener();
}

private class Fenetre extends JFrame{

public JPanel principale; // partie informative
public JPanel grille; // grille de jeu
public Case [][] tabs; 
public int dim;

private static final String bf1 = "bf1.png";
private static final String mf1  = "mf1.png";
private static final String nf1 = "nf1.png";

private static final String bf2 = "bf2.png";
private static final String mf2  = "mf2.png";
private static final String nf2 = "nf2.png";

Container c = this.getContentPane();

JButton b = new JButton("bon"); // boutons pour ajouter les fantomes
JButton m = new JButton("mauvais");

public Fenetre(int dim){
this.dim = dim;
this.tabs = new Case[this.dim][this.dim];

c.setLayout(new GridLayout(0,2));
JPanel principale = new JPanel();
JPanel grille = new JPanel();
JLabel lab = new JLabel("Test");
principale.add(lab);

b.addActionListener(li);   
m.addActionListener(li);

principale.add(b);
principale.add(m);

c.add(principale);
grille.setBackground(Color.black);
c.add(grille);

this.setVisible(true);
this.setSize(600,300);
this.setLocationRelativeTo(null);
this.setTitle("Ghosts");

grille.setLayout(new GridLayout(this.dim, this.dim));

for(int i = 0; i < this.dim ; i++){
    for(int j = 0; j < this.dim ; j++){

// création de toutes les cases

    tabs[i][j] = new Case();
    tabs[i][j].addMouseListener(co);

    tabs[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
    grille.add(tabs[i][j]);

    if( (i == 0 || i == this.dim-1) && ( j == 0 || j == this.dim-1  )){ // cases sorties
        tabs[i][j].setBackground(Color.BLUE);
    }

    }
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void miseAJour(){
    for(int i = 0 ; i < this.dim ; i++){
    for(int j = 0 ; j < this.dim ; j++){
        tabs[i][j].repaint();
    }
    }
}

}



private class Modele{

    int total = 0;
    int bj1 = 0;
    int mj1 = 0;
    int bj2 = 0;
    int mj2 = 0;



    public Case prochainePlace(){
        for(int i = 0; i < f.dim ; i++){
        for(int j = 0; j < f.dim ; j++){

            if(j !=0 && j != f.dim - 1 && (i > f.dim-3 || (i < 2)) ){  // fantomes J1
            if(f.tabs[i][j].getFantome() == null){
                total++; 
                return f.tabs[i][j];

            }
            }
        }

        }

        return null;
    }

    public boolean remplissable(){
        return((bj1 + bj2 + mj1 + mj2) < (f.dim *2)-4 );
    }

    public void ajoutFantome(Case c , boolean bon){
        if(total <= 8){
        if(bon && bj1 < 4){
            c.setFantome(new Fantome(true, Fenetre.bf1 , 2 ));

            c.repaint();
            bj1++;
        }
        if(!bon && mj1 < 4){
            c.setFantome(new Fantome(false,Fenetre.mf1 , 1 ));

            c.repaint();
            mj1++;
        }
        }
        else{
        if(bon && bj2 < 4 ){
            c.setFantome(new Fantome(true, Fenetre.bf2 , 2 ));

            c.repaint();
            bj2++;
        }
        if(!bon && mj2 < 4){
            c.setFantome(new Fantome(false, Fenetre.mf2 , 2 ));

            c.repaint();
            mj2++;
        }
        }
    }

    }

private class BoutonListener implements ActionListener{

    public void actionPerformed(ActionEvent e){
        System.out.println("fonction called");
        if(mo.remplissable()){

        if(e.getSource() == f.b){
            mo.ajoutFantome(mo.prochainePlace(), true);
            f.miseAJour();
        }
        else{
            mo.ajoutFantome(mo.prochainePlace(), false);
            f.miseAJour();
        }
        }
    }

}


private class CaseListener implements MouseListener{


    public void mouseClicked(MouseEvent e){

    }

    public void mouseExited(MouseEvent e){

    }   

    public void mousePressed(MouseEvent e){

    }

    public void mouseReleased(MouseEvent e){

    }

    public void mouseEntered(MouseEvent e){

    }

    }

}

【问题讨论】:

  • 如果你正确缩进你的代码,人们更有可能费心去阅读它。
  • 您能否缩短代码以仅包含您的问题?代码越多,就越难提供帮助。
  • 也许把你的代码翻译成英文?请使用最少的代码创建一个Runnable Example 以重现该问题,以便我们可以复制粘贴它并查看发生了什么。
  • @Frakcool 来吧。这里有足够的信息。不过,我有一个问题要问您:Case 类是 Button 还是 JButton? D'abord, en francais, est-ce que votre Case est aussi un bouton?
  • @Zizouz212 绰绰有余。我要求 OP 提供 only 必要的代码,不多也不少。不过好吧,反正我会努力读下去的……

标签: java swing jbutton


【解决方案1】:

注意你的 Plateaue 构造函数:

public Plateau(int dim) {
    this.f = new Fenetre(dim);  // (A)
    this.mo = new Modele();
    this.li = new BoutonListener();  // (B)
}

您在 (A) 行创建 Fenetre JFrame,在此处将 ActionListener li 添加到 JButton,但在 (B)之后 将 ActionListener 分配给 li 变量您已经创建了 JFrame。因此,您实际上是在 Fenetre 构造函数中将 null 引用作为 ActionListeners 添加到 JButtons 中:

    public Fenetre(int dim) {

        // .... <deleted code>

        // li is null here!
        b.addActionListener(li);
        m.addActionListener(li);

一个解决方案:颠倒这个。

public Plateau(int dim) {
    this.li = new BoutonListener();  // call this first
    this.f = new Fenetre(dim);  
    this.mo = new Modele();
}

现在 JButtons 将获得 ActionListeners 实例而不是空引用。

请注意,更好的更符合 OOP 的解决方案可能是为 Fenetre 提供自己的 addBoutonListener(ActionListener l) 方法,您可以在其中将侦听器添加到 JButton,这样您的 Plateau 构造函数代码可以改为:

public Plateau(int dim) {
    this.f = new Fenetre(dim);  
    this.mo = new Modele();
    f.addBoutonListener(new BoutonListener());
}

在 Fenetre 中,您可以:

    public void addBoutonListener(BoutonListener btnListener) {
        b.addActionListener(btnListener);
        m.addActionListener(btnListener);
    }

【讨论】:

  • 非常感谢它现在可以工作了。事实上,你这样做的方式似乎更好,我想我会改变它。对不起,代码中的缩进和法语,下次我会确保发布一个干净的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多