尝试让扫雷一起使用正确的点击,一切都达到了这一点;
发现我需要使用MouseListener ....这似乎很好,但我的主要错误是我无法让applet初始化,也不明白为什么。我看了这个,但无法理解这个解决方案: Abstract Class Error in Java
任何帮助都会受到赞赏,我知道错误源于一个抽象类,但如果没有它,applet就不会运行。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.SwingUtilities;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
abstract class Minesweeper extends Applet implements MouseListener{
// initializing all data types
JButton[] a; // The grid boxes
int counter = 1; // Obsolete
char[] letter; // Array of mine locations
int[] numbers; // Array of numbers; values and locations
boolean explode = false;
String name1;
String name2;
int mines;
public void init() {
// The code below initializes and locates the grid
a = new JButton[100];
setLayout(new GridLayout(10, 10));
// The code below fills the grid with buttons that can be clicked
for (int counter = 0; counter < 100; counter++) {
a[counter] = new JButton();
a[counter].setText(" ");
a[counter].setBackground(Color.white);
a[counter].addMouseListener(this);
add(a[counter]);
}
numbers = new int[100];
letter = new char[100];
for (counter = 0; counter < 10; counter++) {
mines = (int) (Math.random() * 100);
if (letter[mines] == '*') {
counter--;
} else {
letter[mines] = '*';
}
}
for (counter = 0; counter < 100; counter++) {
numbers[counter] = 0;
}
for (int search = 0; search < 10; search++) {
for (int searchb = 0; searchb < 10; searchb++) {
if (letter[search * 10 + searchb] == '*') {
if (search != 0) {
numbers[((search - 1) * 10) + searchb]++;
}
if (search != 9) {
numbers[((search + 1) * 10) + searchb]++;
}
if (searchb != 0) {
numbers[((search * 10) + searchb) - 1]++;
}
if (searchb != 9) {
numbers[((search * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 0)) {
numbers[(((search - 1) * 10) + searchb) - 1]++;
}
if ((search != 9) && (searchb != 9)) {
numbers[(((search + 1) * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 9)) {
numbers[(((search - 1) * 10) + searchb) + 1]++;
}
if ((search != 9) && (searchb != 0)) {
numbers[(((search + 1) * 10) + searchb) - 1]++;
}
}
}
}
for (int counter = 0; counter < 100; counter++) {
if(letter[counter]!= '*')
letter[counter] = (char) ('0' + numbers[counter]);
}
}
// ActionEvent e is the click
public void clickPerformed(MouseEvent e) {
int pickedsquare = 0, WinCheck=1, pickedcolumn = 0;
JButton b = (JButton) e.getSource();
counter++;
b.setText("Test");
if (SwingUtilities.isRightMouseButton(e)){
for (int f = 0; f < 100; f++) {
if (a[f].getText() == "Test") {
pickedsquare = f;
name1=" "+letter[f];
a[f].setBackground(Color.red);
break;
}
}
}
else{
for (int f = 0; f < 100; f++) {
if (a[f].getText() == "Test") {
pickedsquare = f;
name1=" "+letter[f];
a[f].setText(name1);
a[f].setBackground(Color.lightGray);
if(a[f].getText()=="0")
a[f].setText(" ");
break;
}
}
if (letter[pickedsquare] == '*')
explode = true;
if (explode == true) {
JOptionPane.showMessageDialog(null, "You are dead!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
WinCheck=1;
for(int g=0;g<100;g++){
if(a[g].getBackground()!=Color.lightGray){
if(letter[g]!='*'){
WinCheck=0;
break;
}
}
}
if (WinCheck==1) {
JOptionPane.showMessageDialog(null, "You have swept all mines!");
for (int counterb = 0; counterb <= 99; counterb++) {
a[counterb].setEnabled(false);
}
}
}
}
}
答案 0 :(得分:2)
你有一个没有实现它的抽象类。如果您使该类成为具体类并添加缺少的方法,那么它将运行而没有任何错误。 这里是固定代码,你仍然需要努力才能使其完全正常运行:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.SwingUtilities;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Minesweeper extends Applet implements MouseListener{
// initializing all data types
JButton[] a; // The grid boxes
int counter = 1; // Obsolete
char[] letter; // Array of mine locations
int[] numbers; // Array of numbers; values and locations
boolean explode = false;
String name1;
String name2;
int mines;
public void init() {
// The code below initializes and locates the grid
a = new JButton[100];
setLayout(new GridLayout(10, 10));
// The code below fills the grid with buttons that can be clicked
for (int counter = 0; counter < 100; counter++) {
a[counter] = new JButton();
a[counter].setText(" ");
a[counter].setBackground(Color.white);
a[counter].addMouseListener(this);
add(a[counter]);
}
numbers = new int[100];
letter = new char[100];
for (counter = 0; counter < 10; counter++) {
mines = (int) (Math.random() * 100);
if (letter[mines] == '*') {
counter--;
} else {
letter[mines] = '*';
}
}
for (counter = 0; counter < 100; counter++) {
numbers[counter] = 0;
}
for (int search = 0; search < 10; search++) {
for (int searchb = 0; searchb < 10; searchb++) {
if (letter[search * 10 + searchb] == '*') {
if (search != 0) {
numbers[((search - 1) * 10) + searchb]++;
}
if (search != 9) {
numbers[((search + 1) * 10) + searchb]++;
}
if (searchb != 0) {
numbers[((search * 10) + searchb) - 1]++;
}
if (searchb != 9) {
numbers[((search * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 0)) {
numbers[(((search - 1) * 10) + searchb) - 1]++;
}
if ((search != 9) && (searchb != 9)) {
numbers[(((search + 1) * 10) + searchb) + 1]++;
}
if ((search != 0) && (searchb != 9)) {
numbers[(((search - 1) * 10) + searchb) + 1]++;
}
if ((search != 9) && (searchb != 0)) {
numbers[(((search + 1) * 10) + searchb) - 1]++;
}
}
}
}
for (int counter = 0; counter < 100; counter++) {
if(letter[counter]!= '*')
letter[counter] = (char) ('0' + numbers[counter]);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}