【问题标题】:Action Listener detects multiple Events for a Single Event动作侦听器检测单个事件的多个事件
【发布时间】:2014-01-15 23:47:12
【问题描述】:

我设计了一个面板,其中包含一些按钮。按钮附有一个 ActionListener。当我点击那个按钮时,这个 ActionListener 会检测到这个单击的 4 个事件。而它应该只检测到一个。有人知道具体是什么原因吗?

public class Buttons extends JPanel
{
private JButton undo=new JButton("Undo");
private JButton replay=new JButton("Replay");

public void paint(Graphics g)
{
    super.paint(g);
    super.setSize(new Dimension(560,30));
    super.add(replay);
    super.add(undo);
    undo.setBorder(new LineBorder(Color.WHITE,3));
    replay.setBorder(new LineBorder(Color.WHITE,3));

    undo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Controler.pieces.undo();
            Controler.reDraw();

        }
    });
    replay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Dastiii");
        }
    });

}
 }

这些事件在这里被使用

public void undo()
{
    System.out.print(Controler.allMoves.size());
    if(Controler.allMoves.size()<=1)
    {
        init_board();
        return;
    }
    Piece temp[][]=Controler.allMoves.get(Controler.allMoves.size()-2);
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            board[i][j].set_name(temp[i][j].get_name());
            board[i][j].set_oneWay(temp[i][j].get_oneWay());
        }
    }
    Controler.allMoves.remove(Controler.allMoves.size()-2);
}

【问题讨论】:

  • 将您的代码添加到问题中。
  • 也许它在完整的点击过程中检测到类似buttonDown buttonPressed buttonUp 和其他一些东西。
  • @csmckelvey 我添加了带有按钮的动作监听器,buttonDown 或其他一些链接到我认为的鼠标监听器。我说的对吗?
  • 您确实控制了 paint 方法的执行,但您决定在此处添加 ActionListener。所以每次调用paint时,你添加另一个 ActionListener到你的按钮..
  • 一定要查看 MadProgrammer 的答案。

标签: java eclipse swing actionlistener


【解决方案1】:

您在paint 方法中为您注册ActionListeners!!

我们甚至不必担心不建议覆盖paint这一事实

切勿在任何paint 方法中更改或修改组件或其任何子组件的状态,这些将在您的应用程序执行期间被多次调用。例如,当主窗口可见时,paint 方法被调用 2-4 次并不罕见......

public void paint(Graphics g)
{
    super.paint(g);
    /** All this should be done within the constructor
    // If you are using a layout manager, this is pointless, if your not
    // then that's another problem
    super.setSize(new Dimension(560,30));
    super.add(replay);
    super.add(undo);
    undo.setBorder(new LineBorder(Color.WHITE,3));
    replay.setBorder(new LineBorder(Color.WHITE,3));

    undo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Controler.pieces.undo();
            Controler.reDraw();

        }
    });
    replay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Dastiii");
        }
    });
    **/

}

看看:

有关 Swing 中绘画的方式和内容的更多详细信息

【讨论】:

  • @MadProgrammer 哦……我明白了……我明白了……!! :)
  • @csmckelvey 起初我看起来是对的...认为我的潜意识试图保护我...
  • @MadProgrammer 我也需要培养我内心的救星!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多