gpt4 book ai didi

java - 右键单击 JButton

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:32:04 24 4
gpt4 key购买 nike

尝试将 mouseAdapter 添加到 JButton,以便右键单击以标记单元格。问题是当我将它实例化到按钮上时,它不会让我这样做。也许是因为它已经有一个 Action 监听器了?我不太确定。任何帮助表示赞赏。我正在创建扫雷游戏,仅供引用。

button = new JButton[size][size];
ButtonListener bl = new ButtonListener();
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
button[r][c] = new JButton("");
button[r][c].addActionListener(bl);
button[r][c].addMouseListener (new MouseAdapter());``
// error message: cannot instantiate the type MouseAdapter
panel.add(button[r][c]);

最终如果可行,我想将其合并到游戏中:

button.addMouseListener(new MouseAdapter() { 
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) { // if right click
button.setText("F");
button.getModel().setPressed(false);
// button.setEnabled(true);
} else {
button.setText("X");
button.getModel().setPressed(true);
// button.setEnabled(false);
}
}
});

最佳答案

你要找的扫雷游戏

1) JToggleButton

2) 添加IconJToggleButton方法

  • JToggleButton#setIcon();

  • JToggleButton#setSelectedIcon();

  • JToggleButton#setDisabledIcon();

3) 添加MouseListenerJToggleButton

4) 用 SwingUtilities.isRightMouseButton() 方法覆盖 mouseClicked

结果可能是

enter image description here enter image description here enter image description here

来自代码

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

关于java - 右键单击 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com