尝试将一个mouseAdapter添加到JButton中以右键单击以标记该单元格。问题是当我将它实例化到按钮上时,它不会让我。也许是因为它上面已经有一个actionlistener?我不太确定。任何帮助表示赞赏。我正在创造一个扫雷游戏,fyi。
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);
}
}
});
答案 0 :(得分:5)
对于扫雷游戏,您需要寻找
1)JToggleButton
2)将Icon
添加到JToggleButton
方法
JToggleButton#setIcon();
JToggleButton#setSelectedIcon();
JToggleButton#setDisabledIcon();
3)将MouseListener
添加到JToggleButton
4)使用
中的方法mouseClicked
覆盖SwingUtilities.isRightMouseButton()
结果可能是
来自代码
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();
}
});
}
}
答案 1 :(得分:2)
MouseAdapter是一个抽象类,您无法创建它的实例。 这就是你得到错误的原因。
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. ; )
}
});