嘗試將鼠標適配器添加到JButton中以右鍵單擊以標記該單元。問題是,當我將它實例化到按鈕上時,它不會讓我。也許是因爲它已經有一個actionlistener呢?我不太確定。任何幫助表示讚賞。我正在創建一個掃雷遊戲,fyi。右鍵單擊JButton
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);
}
}
});
@deporter你在說什麼?這是這個人的第一個問題... – 2012-02-20 23:00:33
編譯器是你的朋友在這裏:爲什麼你會想要註冊一個do-nothing-mouseListener :-) – kleopatra 2012-03-09 10:27:44