JButton with both ActionListener / MouseListener

蹲街弑〆低调 提交于 2020-01-15 07:18:30

问题


Is it possible to create a Jbutton with both an ActionListener and MouseListener

Meaning so i create a button and then when i press it ( throught actionListener) it changes the frame so that AFTER then button was pressed i can press anywhere on the frame and MouseListener would in use.

JButton button = new JButton();//Creates Button
button.addActionListener(new ActionListener() {         
public void actionPerformed(ActionEvent e) {
    //Insert MouseListener
    //Then do something with mouseListener
}
}); 

Heres the curent code: however they're now in sync when i try to click the button and i cannot call mouseListener a 2nd time

    JButton button2 = new JButton("Click");
    button2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("You clicked the button");
            newCube.stopCube();
        }

    });
    button2.addMouseListener(new java.awt.event.MouseAdapter()
    {
        public void mousePressed(java.awt.event.MouseEvent evt)
        {
            double x = evt.getX();
            double y = evt.getY();
            newCube.setCube(x,y);
        }
    });

回答1:


If you want to move something by clicking on it, you can use a mouse listener on that node directly, instead of using it on the button.

To add both the action listener and a mouse listener on a button, you can use the addActionListener and addMouseListener methods on the button.

Look at the api for information about these methods... http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html




回答2:


If I understood you correctly, this sample might help you (add this to your own ActionListener)

@Override
public void actionPerformed(ActionEvent e) {
    ((JButton)e.getSource()).addMouseListener(yourMouseListener);
}

I tried this, it works.




回答3:


Here is example with JToggleButton which add/remove MouseListener to JFrame.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class Example extends JFrame {

    private MouseAdapter mouseListener;

    public Example(){
        init();
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void init() {
        mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                System.out.println("clicked");
            }
        };
        setLayout(new FlowLayout());
        JToggleButton b = new JToggleButton("add listener");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(((JToggleButton)e.getSource()).isSelected()){
                    Example.this.addMouseListener(mouseListener);
                    ((JToggleButton)e.getSource()).setText("remove listener");
                } else {
                    Example.this.removeMouseListener(mouseListener);
                    ((JToggleButton)e.getSource()).setText("add listener");
                }
            }
        });
        add(b);
    }

    public static void main(String... s){
        new Example();
    }
}

EDIT: examle with JButton:

public class Example extends JFrame {

    private MouseAdapter mouseListener;

    public Example(){
        init();
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void init() {
        mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                System.out.println("clicked");
            }
        };
        setLayout(new FlowLayout());
        JButton b = new JButton("add listener");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(((JButton)e.getSource()).getText().equals("add listener")){
                    Example.this.addMouseListener(mouseListener);
                    ((JButton)e.getSource()).setText("remove listener");
                } else {
                    Example.this.removeMouseListener(mouseListener);
                    ((JButton)e.getSource()).setText("add listener");
                }
            }
        });
        add(b);
    }

    public static void main(String... s){
        new Example();
    }
}



回答4:


What you want to do is still not clear to me. Though this can help you. It will add a mouse listner to the component when the start button is clicked and remove the mouse listner when stop button is clicked. Thus you can stop the two listeners from working in sync..

JButton startButton = new JButton();

startButton.addActionListener(new ActionListener() {         
public void actionPerformed(ActionEvent e) {

   //Add MouseListener to move the component

}
}); 

JButton stopButton = new JButton();

stopButton.addActionListener(new ActionListener() {         
public void actionPerformed(ActionEvent e) {

   //Remove the MouseListener

}
}); 


来源:https://stackoverflow.com/questions/22348685/jbutton-with-both-actionlistener-mouselistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!