我正在制作这个非图形,我被困在制作程序检查解决方案(listener2)。我希望你能了解发生了什么。我刚开始编程所以要温柔。
主:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Start {
static String[] stolpci = new String[10];
static String[] vrstice = new String[10];
static JButton[][] gumbi = new JButton[11][11];
static JButton gumbResitev1 = new JButton("Pregled resitev");
static JButton gumbResitev2 = new JButton("Nova igra");
public static void main(String[] args) {
JFrame okno = new JFrame("Nonogram");
okno.setSize(1000, 1000);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
okno.add(panel);
JPanel polje = new JPanel(new GridLayout(11, 11));
panel.add(polje, BorderLayout.CENTER);
JPanel nastavitve1 = new JPanel(new GridLayout(1, 1));
panel.add(nastavitve1, BorderLayout.NORTH);
nastavitve1.add(gumbResitev1);
JPanel nastavitve2 = new JPanel(new GridLayout(1, 1));
panel.add(nastavitve2, BorderLayout.SOUTH);
nastavitve2.add(gumbResitev2);
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i < 1 || j < 1) {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.RED);
polje.add(gumbi[i][j]);
}
else {
gumbi[i][j] = new JButton();
gumbi[i][j].setBackground(Color.WHITE);
gumbi[i][j].addActionListener(new Listener(gumbi));
polje.add(gumbi[i][j]);
}
}
}
// matrika in resitve
Random rand = new Random();
int[][] poljeM = new int[10][10];
for (int i = 0; i < poljeM.length; i++) {
for (int j = 0; j < poljeM[i].length; j++) {
poljeM[i][j] = rand.nextInt(2);
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(poljeM[i][j] + " ");
}
System.out.println(" ");
}
// stetje vrstic
int crneV = 0;
for (int g = 0; g < poljeM.length; g++) {
crneV = 0;
vrstice[g] = "";
for (int i = 0; i < poljeM[0].length; i++) {
if (poljeM[g][i] == 1) {
crneV += 1;
} else if (crneV != 0) {
vrstice[g] += crneV + " ";
crneV = 0;
}
}
if (crneV != 0) {
vrstice[g] += crneV + " ";
}
}
// stetje stolpcev
int crneS = 0;
for (int i = 0; i < poljeM[0].length; i++) {
stolpci[i] = "";
crneS = 0;
for (int j = 0; j < poljeM.length; j++) {
if (poljeM[j][i] == 1) {
crneS += 1;
} else if (crneS != 0) {
stolpci[i] += crneS + " ";
crneS = 0;
}
}
if (crneS != 0) {
stolpci[i] += crneS + " ";
}
}
for (int z = 1; z < 11; z++) {
gumbi[0][z].setText(stolpci[z - 1]);
}
for (int i = 1; i < 11; i++) {
gumbi[i][0].setText(vrstice[i - 1]);
}
gumbi[0][0].setText("Nonogram");
// resize
okno.setVisible(true);
}
}
listener1:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Listener implements ActionListener {
JButton[][] gumbi = Start.gumbi;
public Listener(JButton[][] gumbi) {
this.gumbi = gumbi;
}
public void actionPerformed(ActionEvent e) {
JButton gumb = (JButton) e.getSource();
if (gumb.getBackground() == Color.WHITE) {
gumb.setBackground(Color.BLACK);
} else if (gumb.getBackground() == Color.BLACK) {
gumb.setBackground(Color.WHITE);
}
}
}
LISTENER2:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Resitve extends Start implements ActionListener {
JButton gumbResitev1 = Start.gumbResitev1;
public Resitve (JButton gumbResitev1){
this.gumbResitev1 = gumbResitev1;
}
public void actionPerformed(ActionEvent e) {
JButton gumb = (JButton) e.getSource();
String[] stetjeV = new String[10];
int stevecC = 0;
for (int i = 1; i < Start.gumbi.length; i++) {
stevecC = 0;
stetjeV[i] = "";
for (int j = 1; j < Start.gumbi.length; j++) {
if (gumb.getBackground() == Color.BLACK) {
stevecC += 1;
} else if (gumb.getBackground() == Color.WHITE) {
stetjeV[i] += stevecC + " ";
}
}
if (stevecC != 0) {
stetjeV[i] += stevecC + " ";
}
}
String[] stetjeS = new String[10];
int stevecS = 0;
for (int i = 0; i < Start.gumbi.length; i++) {
stevecS = 0;
stetjeS[i] = "";
for (int j = 0; j < Start.gumbi.length; j++) {
if (gumb.getBackground() == Color.BLACK) {
stevecS += 1;
} else if(gumb.getBackground() == Color.WHITE){
stetjeS[j] += stevecS+ " ";
}
}
if (stevecS !=0) {
stetjeS[i] += stevecS+ " ";
}
}
for (int i = 0; i < Start.gumbi.length; i++) {
for (int j = 0; j < Start.gumbi.length; j++) {
if (stetjeS[j]==stolpci[j] && stetjeV[i]==vrstice[i] ) {
System.out.println("je prav");
}else{
System.out.println("ni prav");
}
}
}
}
}
答案 0 :(得分:1)
开始时间:
static JButton[][] gumbi = new JButton[11][11];
In Resitve
String[] stetjeV = new String[10];
for (int i = 1; i < Start.gumbi.length; i++) {
stevecC = 0;
stetjeV[i] = ""; // NPE! because 10 is less than 11
也许在这里:
String[] stetjeS = new String[10];
并且在Start中还有包含10个元素的数组。