我有2个类,其中所有主要代码都保存在WordSearch.java中,而我希望创建一个JTable来显示2D数组“字母”的地方是WordSearchSwing.java。运行以下代码时,JTable不显示任何内容(仅12个空白单元格)。我不知道为什么它不起作用,任何帮助将不胜感激。
字词搜寻类别:
import java.util.ArrayList;
public class WordSearch {
public static String[][] letters = new String[12][12];
static ArrayList<String> words = new ArrayList<String>();
public static void main(String[] args) {
//populating the arrayList with Strings, some which are in the wordsearch and some which aren't
words.add("ghost"); words.add("knife"); words.add("patch"); words.add("ghoul"); words.add("apple");
words.add("boo"); words.add("spider"); words.add("pumpkin"); words.add("house"); words.add("trick");
words.add("cat"); words.add("candy"); words.add("orange"); words.add("rat"); words.add("crow");
words.add("blood"); words.add("bats"); words.add("neck"); words.add("mask"); words.add("witch");
words.add("party"); words.add("broom"); words.add("fangs"); words.add("mummy");
//A 2D array is used to make the wordsearch
//Here each coordinate is assigned a letter
letters[0][0] = "o"; letters[1][0] = "e"; letters[2][0] = "o"; letters[3][0] = "e"; letters[4][0] = "b"; letters[5][0] = "o";
letters[0][1] = "h"; letters[1][1] = "n"; letters[2][1] = "i"; letters[3][1] = "l"; letters[4][1] = "t"; letters[5][1] = "r";
letters[0][2] = "b"; letters[1][2] = "c"; letters[2][2] = "o"; letters[3][2] = "d"; letters[4][2] = "y"; letters[5][2] = "a";
letters[0][3] = "d"; letters[1][3] = "o"; letters[2][3] = "t"; letters[3][3] = "m"; letters[4][3] = "d"; letters[5][3] = "n";
letters[0][4] = "d"; letters[1][4] = "h"; letters[2][4] = "m"; letters[3][4] = "a"; letters[4][4] = "c"; letters[5][4] = "g";
letters[0][5] = "p"; letters[1][5] = "u"; letters[2][5] = "o"; letters[3][5] = "a"; letters[4][5] = "p"; letters[5][5] = "e";
letters[0][6] = "m"; letters[1][6] = "u"; letters[2][6] = "n"; letters[3][6] = "u"; letters[4][6] = "t"; letters[5][6] = "r";
letters[0][7] = "m"; letters[1][7] = "d"; letters[2][7] = "m"; letters[3][7] = "k"; letters[4][7] = "s"; letters[5][7] = "e";
letters[0][8] = "y"; letters[1][8] = "d"; letters[2][8] = "c"; letters[3][8] = "p"; letters[4][8] = "h"; letters[5][8] = "e";
letters[0][9] = "w"; letters[1][9] = "e"; letters[2][9] = "i"; letters[3][9] = "e"; letters[4][9] = "k"; letters[5][9] = "r";
letters[0][10] = "n"; letters[1][10] = "y"; letters[2][10] = "n"; letters[3][10] = "e"; letters[4][10] = "o"; letters[5][10] = "i";
letters[0][11] = "c"; letters[1][11] = "t"; letters[2][11] = "f"; letters[3][11] = "h"; letters[4][11] = "o"; letters[5][11] = "w";
letters[6][0] = "b"; letters[7][0] = "o"; letters[8][0] = "o"; letters[9][0] = "l"; letters[10][0] = "c"; letters[11][0] = "h";
letters[6][1] = "o"; letters[7][1] = "b"; letters[8][1] = "h"; letters[9][1] = "t"; letters[10][1] = "a"; letters[11][1] = "s";
letters[6][2] = "g"; letters[7][2] = "h"; letters[8][2] = "o"; letters[9][2] = "s"; letters[10][2] = "t"; letters[11][2] = "g";
letters[6][3] = "r"; letters[7][3] = "b"; letters[8][3] = "a"; letters[9][3] = "t"; letters[10][3] = "s"; letters[11][3] = "n";
letters[6][4] = "e"; letters[7][4] = "o"; letters[8][4] = "f"; letters[9][4] = "u"; letters[10][4] = "t"; letters[11][4] = "a";
letters[6][5] = "d"; letters[7][5] = "e"; letters[8][5] = "f"; letters[9][5] = "t"; letters[10][5] = "m"; letters[11][5] = "f";
letters[6][6] = "i"; letters[7][6] = "c"; letters[8][6] = "k"; letters[9][6] = "o"; letters[10][6] = "p"; letters[11][6] = "m";
letters[6][7] = "p"; letters[7][7] = "h"; letters[8][7] = "o"; letters[9][7] = "e"; letters[10][7] = "a"; letters[11][7] = "a";
letters[6][8] = "s"; letters[7][8] = "r"; letters[8][8] = "c"; letters[9][8] = "l"; letters[10][8] = "r"; letters[11][8] = "s";
letters[6][9] = "b"; letters[7][9] = "r"; letters[8][9] = "d"; letters[9][9] = "p"; letters[10][9] = "t"; letters[11][9] = "k";
letters[6][10] = "o"; letters[7][10] = "o"; letters[8][10] = "e"; letters[9][10] = "p"; letters[10][10] = "y"; letters[11][10] = "e";
letters[6][11] = "n"; letters[7][11] = "l"; letters[8][11] = "u"; letters[9][11] = "a"; letters[10][11] = "a"; letters[11][11] = "s";
//searchAcross();
//searchDown();
}
//A method used to search across the grid for words
public static void searchAcross(){
for (int i =0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
for (int k = 0; k < words.size(); k++){
if (words.get(k).contains(letters[i][j]) && (words.get(k).contains(letters[i][j+1])) && (words.get(k).contains(letters[i][j+2]))){
System.out.println(words.get(k));
}
}
}
}
}
// A method used to search down the grid for words
public static void searchDown(){
for (int j =0; j < 12; j++) {
for (int i = 0; i < 12; i++) {
for (int k = 0; k < words.size(); k++){
if (words.get(k).contains(letters[j][i]) && (words.get(k).contains(letters[j][i+1])) && (words.get(k).contains(letters[j][i+2]))){
System.out.println(words.get(k));
}
}
}
}
}
}
WordSearchSwing类别:
import javax.swing.*;
public class WordSearchSwing {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WordSearchSwing frame = new WordSearchSwing();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WordSearchSwing() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
Object[] title = {"Happy Halloween"};
table = new JTable(WordSearch.letters, title);
contentPane.add(table, BorderLayout.CENTER);
}
}
最佳答案
WordSearch.letters
在WordSearch.main(...)
方法中初始化,并且永远不会调用WordSearch.main()
方法。
注意:此代码中有许多改进的机会,但是我仅回答了这个问题。