我一直在尝试将我的数组加载到一个没有运气的JTable对象中。所以这是我的数组:
int[][] board = {
{0, 0, 0, 0, 2, 0, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 0, 2, 4},
{1, 0, 0, 4, 0, 0, 0, 3, 8},
{0, 0, 0, 6, 0, 0, 0, 0, 7},
{0, 0, 4, 5, 3, 8, 9, 0, 0},
{8, 0, 0, 0, 0, 7, 0, 0, 0},
{7, 4, 0, 0, 0, 6, 0, 0, 1},
{6, 1, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 9, 0, 0, 0, 0}
我去了http://docs.oracle.com/javase/tutorial/uiswing/components/table.html 并且没有用于放置int数组的构造函数,但是有主题。
任何人都知道一种方法,谢谢!
答案 0 :(得分:2)
你可以这样做:
Integer[][] board = new Integer[][]{
{0, 0, 0, 0, 2, 0, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 0, 2, 4},
{1, 0, 0, 4, 0, 0, 0, 3, 8},
{0, 0, 0, 6, 0, 0, 0, 0, 7},
{0, 0, 4, 5, 3, 8, 9, 0, 0},
{8, 0, 0, 0, 0, 7, 0, 0, 0},
{7, 4, 0, 0, 0, 6, 0, 0, 1},
{6, 1, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 9, 0, 0, 0, 0}};
new JTable(board, new String[]{"columnName1"...});
答案 1 :(得分:2)
我在这里看到两种可能性:zou可以使用Integer[][]
代替int[][]
,可以将其转换为Object[][]
,这将与JTable一起使用或者您可以编写自己的数据模型。
根据您最终想要达到的目标,您应该选择更合适的一个。
答案 2 :(得分:1)
尝试将int
数组更改为Integer
数组
答案 3 :(得分:1)
请试试这个
import javax.swing.*;
import java.awt.*;
public class JTableComponent{
public static void main(String[] args)
{
new JTableComponent();
}
public JTableComponent(){
JFrame frame = new JFrame("Creating JTable Component Example!");
JPanel panel = new JPanel();
Integer[][] board = {
{0, 0, 0, 0, 2, 0, 0, 0, 0},
{0, 0, 5, 0, 0, 0, 0, 2, 4},
{1, 0, 0, 4, 0, 0, 0, 3, 8},
{0, 0, 0, 6, 0, 0, 0, 0, 7},
{0, 0, 4, 5, 3, 8, 9, 0, 0},
{8, 0, 0, 0, 0, 7, 0, 0, 0},
{7, 4, 0, 0, 0, 6, 0, 0, 1},
{6, 1, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 9, 0, 0, 0, 0}};
String col[] = {"1","2","3","4","5","6","7","8","9"};
JTable table = new JTable(board,col);
panel.add(table,BorderLayout.CENTER);
frame.add(panel);
frame.setSize(800,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}