右键单击JButton以锁定它并更改其颜色Java

时间:2014-12-18 22:28:39

标签: java jbutton right-click

我是java的初学者,所以我试图创建一个扫雷游戏。我有一个充满JButtons的网格,当点击时会显示数字或地雷。

我想添加"标志"因此,当我右键单击任何按钮时,它会改变颜色 - 显示它已被标记并被锁定'所以除非右键单击,否则无法单击它。

这是我的按钮

public void buttons()
{
    // Creating the grid with buttons
    grid.setLayout(new GridLayout(20,20));
    // Button grid
    for (int x = 0; x < buttons.length; x++)
    {
        for (int y = 0; y < buttons.length; y++)
        {
            buttons[x][y] = new JButton();
            buttons[x][y].addActionListener(this);
            grid.add(buttons[x][y]);
        }
    }

我尝试创建此右键单击功能,但我目前卡住了,希望得到一些帮助。

public void flag()
{
    buttons.addMouseListener(new MouseAdapter() 
    {
        public void mousePressed(MouseEvent e) 
        {
            JButton rightClick = new JButton("Right Click");
            rightClick.addActionListener(this);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

通常情况下,您会通过按钮ActionListener处理按钮上的用户事件,您要尝试做的是阻止用户触发按钮ActionListener,这是最简单的方法您可以这样做是禁用按钮...

通常,我不喜欢在按钮上使用MouseListener,因为这通常不是确定用户互动的最佳方式,但在这种情况下,它是要求

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton btn;

        public TestPane() {

            setBorder(new EmptyBorder(8, 8, 8, 8));

            setLayout(new GridBagLayout());
            btn = new JButton("O");
            btn.setMargin(new Insets(8, 8, 8, 8));
            add(btn);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("You clicked me");
                }
            });

            btn.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {
                        btn.setEnabled(!btn.isEnabled());
                        if (btn.isEnabled()) {
                            btn.setText("O");
                        } else {
                            btn.setText("X");
                        }
                    }
                }
            });

        }

    }

}