Right Clicking on JButton

后端 未结 2 1073
名媛妹妹
名媛妹妹 2020-11-30 15:42

trying to add a mouseAdapter to a JButton for a right click to flag the cell. Problem is when I instantiate it onto the button, it won\'t let me. Maybe because it already

相关标签:
2条回答
  • 2020-11-30 15:48

    For Minesweeper game you have look for

    1) JToggleButton

    2) add Icon to JToggleButton methods

    • JToggleButton#setIcon();

    • JToggleButton#setSelectedIcon();

    • JToggleButton#setDisabledIcon();

    3) add MouseListener to JToggleButton

    4) override mouseClicked with method SwingUtilities.isRightMouseButton() inside

    result could be

    enter image description hereenter image description hereenter image description here

    from code

    import java.awt.Insets;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    public class MyToggleButton extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
        private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
        private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    
        public MyToggleButton() {
            final JToggleButton toggleButton = new JToggleButton();
            toggleButton.setBorderPainted(false);
            toggleButton.setBorder(null);
            toggleButton.setFocusable(false);
            toggleButton.setMargin(new Insets(0, 0, 0, 0));
            toggleButton.setContentAreaFilled(false);
            toggleButton.setIcon((errorIcon));
            toggleButton.setSelectedIcon(infoIcon);
            //toggleButton.setRolloverIcon((infoIcon));
            //toggleButton.setPressedIcon(warnIcon);
            toggleButton.setDisabledIcon(warnIcon);
            toggleButton.addMouseListener(new MouseListener() {
    
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) {
                        if (toggleButton.isEnabled()) {
                            toggleButton.setEnabled(false);
                        } else {
                            toggleButton.setEnabled(true);
                        }
                    }
                }
    
                public void mousePressed(MouseEvent e) {
                }
    
                public void mouseReleased(MouseEvent e) {
                }
    
                public void mouseEntered(MouseEvent e) {
                }
    
                public void mouseExited(MouseEvent e) {
                }
            });
            add(toggleButton);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyToggleButton t = new MyToggleButton();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-30 15:59

    MouseAdapter is an abstract class and you cannot create instances of it. That is why you get the error.

    button[r][c].addMouseListener (new MouseAdapter());  //  this will not work
    
    button[r][c].addMouseListener (new MouseAdapter(){});  // this will
                                                     ^
    
    button[r][c].addMouseListener (new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
             // and this will actually do sth. ; )
        }
    });
    
    0 讨论(0)
提交回复
热议问题