На самом деле я объявил глобальную переменную типа "jjj" типа String с начальным значением "lo", я предоставляю ее значение в функции prant (), но я не могу получить доступ к ее значению вне этой функции prant () в коде jButton6ActionPerformed.
import javax.swing.*;
/**
*
* @author prashant
*/
public class Avg_Game extends javax.swing.JFrame {
public Avg_Game() {
initComponents();
}
public Avg_Game(String Om){
pop = Om;
initComponents();
}
String pop;
JFrame frame = new JFrame();
public String jjj = "lo";
int[] mat[];
int[] mat_sol[];
int n,sqn,k;
public Avg_Game(int n,int k) {
/// String jjj;
this.n = n;
this.k = k;
int m;
Double sqnd = Math.sqrt(n);
sqn = sqnd.intValue();
mat = new int[n][n];
mat_sol = new int[n][n];
initComponents();
}
private void fillValue() {
for(int i = 0;i < n;i = i + sqn) {
fillBox(i,i);
}
fillRemaining(0,sqn);
for(int j = 0;j < n;j++) {
for(int k = 0;k < n;k++) {
mat_sol[j][k] = mat[j][k];
}
}
// this.mat = mat;
removeKDigits(); //remove randomly k digits.
//jTextField26.setText(mat[1][0]);
}
boolean unUsedInBox(int rowstart,int colstart,int num) { //rreturn false if given 3*3 box contains num
for(int i = 0;i<sqn;i++) {
for(int j = 0;j<sqn;j++) {
if(mat[rowstart + i][colstart + j] == num) {
return false;
}
}
}
return true;
}
void fillBox(int row,int col) {
int num;
for(int i = 0;i<sqn;i++){
for(int j = 0;j<sqn;j++) {
do {
num = (int) Math.floor((Math.random()*n+1));
}
while(!unUsedInBox(row,col,num));
mat[row + i][col + j] = num;
}
}
}
boolean checkIfSafe(int r,int c,int num) {
for(int k = 0;k < n;k++) {
if(mat[r][k] == num) {
return false;
}
if(mat[k][c] == num) {
return false;
}
}
int rowstart = r - r%sqn;
int colstart = c - c%sqn;
for(int i = 0;i<sqn;i++) {
for(int j = 0;j<sqn;j++) {
if(mat[rowstart + i][colstart + j] == num) {
return false;
}
}
}
return true;
}
boolean fillRemaining(int r,int c) {
if (c>=n && r<n-1) { // j>=9 && i < 8
r = r + 1;
c = 0;
}
if (r>=n && c>=n) // i >= 9 && j >= 9
return true;
if (r < sqn) { // i < 3 it is to skip the first diagonal 3x3 matrix
if (c < sqn) // j< 3
c = sqn; // j = 3
}
else if (r < n-sqn) { // i < 6 it is to skip the second diagonal 3x3
if (c==(int)(r/sqn)*sqn) // j == 3
c = c + sqn; // j = 6
}
else {
if (c == n-sqn) { // j == 6 && i < 9
r = r + 1;
c = 0;
if (r>=n)
return true;
}
}
for (int num = 1; num<=n; num++) {
if (checkIfSafe(r, c, num)) {
mat[r][c] = num;
if (fillRemaining(r, c+1)) {
return true; }
mat[r][c] = 0;
}
}
return false;
}
private void removeKDigits() {
int count = k;
while(count != 0) {
int cellID = (int) Math.floor((Math.random()*(n*n)+1));
int i = (cellID/n);
int j = cellID%n;
// if (j != 0) {
// j = j - 1;
// }
if(i != n){
if (mat[i][j] != 0) {
count--;
mat[i][j] = 0;
}
}
}
}
// jTextField8.setText(jjj);
public void printSudoku() {
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
jTextField1 = new JTextField();
// System.out.println(jjj);
// try {
//
// catch(Exception ex){}
System.out.println();
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
System.out.print(mat_sol[i][j] + " "); }
System.out.println();
}
}
// edit-fold
public void prant() {
jjj = Integer.toString(mat[0][0]);
System.out.println(jjj);
// jTextField1.setText("loe");
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(jjj);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
xsudoku xs = new xsudoku(pop);
xs.setVisible(true);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(JOptionPane.showConfirmDialog(frame,"Confirm if you wanna to exit","wait wait wait!!!",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION) {
System.exit(0);
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
Home home = new Home();
home.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
int n = 9;
int k = 40;
int[][] mat = new int[n][n];
Avg_Game sudoku = new Avg_Game(n,k);
sudoku.fillValue();
sudoku.printSudoku();
sudoku.prant();
// int j = sudoku.mat[0][0];
// System.out.println(j);
// sudoku.jTextField17.setText(Integer.toString(j));
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Avg_Game().setVisible(true);
}
});
// sudoku.print();
// sudoku.jTextField8.setText("game");
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLMain;
private javax.swing.JLabel jLTime;
private javax.swing.JLabel jLTime1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField10;
private javax.swing.JTextField jTextField11;
private javax.swing.JTextField jTextField2;
}