【问题标题】:Right click on JButton to lock it and change its colour Java右键单击 JButton 将其锁定并更改其颜色 Java
【发布时间】:2014-12-18 22:28:38
【问题描述】:

我是java的初学者,所以正在尝试创建一个扫雷游戏。我有一个充满 JButton 的网格,当单击它们时会显示数字或地雷。

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

这是我的按钮

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);
        }
    });
}

【问题讨论】:

  • “卡住”到底是什么意思?
  • 我不知道该怎么做(右键单击一个按钮,当你这样做时它会改变颜色)。我知道我需要 MouseListeners 和 MouseAdapters 但我不确定如何
  • 签出MouseEvent docs.oracle.com/javase/7/docs/api/java/awt/event/… - 你需要检测按钮2的点击

标签: java jbutton right-click


【解决方案1】:

通常,您将通过按钮的ActionListener 处理按钮上的用户事件,您想要尝试并做的是阻止用户触发按钮ActionListener,您可以执行此操作的最简单方法是禁用按钮...

通常,我不喜欢在按钮上使用MouseListeners,因为这通常不是确定用户交互的最佳方式,但在这种情况下,这是必需的

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");
                        }
                    }
                }
            });

        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多