简体   繁体   中英

How to add mouselisteners outside the constructor

In the example below, both button and button2 are supposed to become unclickable when you press them. When you press the reset button, the two disabled buttons are supposed to work again.

Since I made the two buttons unclickable by removing their mouseListeners, how do add the mouseListeners back using the check() method so they still function in the same way again?

public static boolean isreset = false;
public static JButton button = new JButton("Disable button 1");
public static JButton button2 = new JButton("Disable button 2");
public static JButton reset = new JButton("Reset all buttons");

Test() {
    setTitle("Button Test");
    setSize(420, 80);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel buttons = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    gc.fill = GridBagConstraints.BOTH;
    gc.gridheight = 1;
    gc.gridwidth = 1;

    gc.gridx = 0;
    gc.gridy = 0;
    buttons.add(button, gc);

    gc.gridx = 1;
    gc.gridy = 0;
    buttons.add(button2, gc);

    gc.gridx = 3;
    gc.gridy = 0;
    buttons.add(reset, gc);

    add(buttons);

    button.addMouseListener(new Mouse(){
        public void mousePressed(MouseEvent e){
            button.setEnabled(false);
            button.removeMouseListener(this);
            System.out.println("You pressed button 1.");
            isreset = false;
            check(isreset);
        }
    });

    button2.addMouseListener(new Mouse(){
        public void mousePressed(MouseEvent e){
            button2.setEnabled(false);
            button2.removeMouseListener(this);
            System.out.println("You pressed button 2.");
            isreset = false;
            check(isreset);
        }
    });

    reset.addMouseListener(new Mouse(){
        public void mousePressed(MouseEvent e){
            isreset = true;
            check(isreset);
        }
    });

    setVisible(true);
}

public static void check (boolean input){
    if(input == true){
        Test.button.setEnabled(true);
        Test.button.addMouseListener(this); //error here
        Test.button2.setEnabled(true);
        Test.button2.addMouseListener(this); //error here
    }
}   

Everything in your code is static . That's not how you write a JFrame subclass. I suggest you to read some sample code first.

You don't have to remove the mouse listeners each time. Just calling setEnabled is enough.

Also, you should call addActionListener instead of addMouseListener .

Example:

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        button.setEnabled(false);
        System.out.println("You pressed button 1.");
        isreset = false;
        check(isreset);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM