2011-06-02 102 views
1

我试图创建一个JButtons的网格,索引值位于按钮网格的左侧和右侧。Java Swing帮助

这是我的代码,但我得到一个NullPointerException。问题是什么?

public class Test { 
public static void main(String args[]) { 
    JFrame myapp = new JFrame("test"); 
    myapp.setLayout(new FlowLayout()); 

    myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myapp.setSize(400,400); 
    myapp.setVisible(true); 

    myapp.getContentPane().add(Board()); 
} 

private static JPanel Board() { 
    JButton[][] buttons = new JButton [10][10]; 

    JPanel board = new JPanel(new GridLayout(11,11)); 

    String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"}; 
    String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"}; 
    JTextField k; 

    for (int i = 0; i < 11; i++) { 
     for (int j = 0; j < 11; j++) { 
      if ((j != 0) && (i != 0)) { 
       //myboard[i-1][j-1].addActionListener(new ButtonHandler()); 
       board.add(buttons[i-1][j-1]); 
      } 
      if (i == 0) { 
       if (j != 0) { 
        // used to display row of numbers 
        k = new JTextField(columnlabels[j]); 
        k.setEditable(false); 
        k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
       } else { 
        // used to display column of numbers 
        k = new JTextField(); 
        k.setEditable(false); 
       } 
       board.add(k); 
      } else if (j == 0) { 
       k = new JTextField(rowlabels[i]); 
       k.setEditable(false); 
       k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
       board.add(k); 
      } 
     } 
    } 

    return board; 
} 
} 

我可能在做一些很明显的事情,但Swing对我来说是一种新鲜的东西,所以任何帮助都将不胜感激。

+0

上线你得到NPE? – 2011-06-02 03:42:28

+0

发布stacktrace – 2011-06-02 03:46:55

回答

4

Swing GUI,将应在美国东部时间来构建。这部分作为OP的练习。

import java.awt.*; 
import javax.swing.*; 

public class Test { 
    public static void main(String args[]) { 
     JFrame myapp = new JFrame("test"); 
     myapp.setLayout(new FlowLayout()); 

     myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     myapp.getContentPane().add(Board()); 

     // very important! 
     myapp.pack(); 

     // do these last. 
     myapp.setSize(400,400); 
     myapp.setVisible(true); 

    } 

    private static JPanel Board() { 
     JButton[][] buttons = new JButton [10][10]; 
     for(int x=0; x<buttons.length; x++) { 
      for (int y=0; y<buttons[0].length; y++) { 
       buttons[x][y] = new JButton(); 
      } 
     } 

     JPanel board = new JPanel(new GridLayout(11,11)); 

     String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"}; 
     String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"}; 
     JTextField k; 

     for (int i = 0; i < 11; i++) { 
      for (int j = 0; j < 11; j++) { 
       if ((j != 0) && (i != 0)) { 
        //myboard[i-1][j-1].addActionListener(new ButtonHandler()); 
        board.add(buttons[i-1][j-1]); 
       } 
       if (i == 0) { 
        if (j != 0) { 
         // used to display row of numbers 
         k = new JTextField(columnlabels[j]); 
         k.setEditable(false); 
         k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
        } else { 
         // used to display column of numbers 
         k = new JTextField(); 
         k.setEditable(false); 
        } 
        board.add(k); 
       } else if (j == 0) { 
        k = new JTextField(rowlabels[i]); 
        k.setEditable(false); 
        k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
        board.add(k); 
       } 
      } 
     } 

     return board; 
    } 
} 

enter image description here


鉴于GUI的性质,我猜你是更多的东西这样以后。

import java.awt.*; 
import javax.swing.*; 

public class Test { 
    public static void main(String args[]) { 
     JFrame myapp = new JFrame("test"); 
     myapp.setLayout(new FlowLayout()); 

     myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     myapp.getContentPane().add(Board()); 

     // very important! 
     myapp.pack(); 

     // do this last. 
     myapp.setVisible(true); 
    } 

    private static JPanel Board() { 
     JButton[][] buttons = new JButton [10][10]; 
     for(int x=0; x<buttons.length; x++) { 
      for (int y=0; y<buttons[0].length; y++) { 
       JButton temp = new JButton(); 
       temp.setPreferredSize(new Dimension(30,30)); 
       buttons[x][y] = temp; 
      } 
     } 

     JPanel board = new JPanel(new GridLayout(11,11)); 

     String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"}; 
     String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"}; 
     JTextField k; 

     for (int i = 0; i < 11; i++) { 
      for (int j = 0; j < 11; j++) { 
       if ((j != 0) && (i != 0)) { 
        //myboard[i-1][j-1].addActionListener(new ButtonHandler()); 
        board.add(buttons[i-1][j-1]); 
       } 
       if (i == 0) { 
        if (j != 0) { 
         // used to display row of numbers 
         k = new JTextField(columnlabels[j]); 
         k.setEditable(false); 
         k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
        } else { 
         // used to display column of numbers 
         k = new JTextField(); 
         k.setEditable(false); 
        } 
        board.add(k); 
       } else if (j == 0) { 
        k = new JTextField(rowlabels[i]); 
        k.setEditable(false); 
        k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT); 
        board.add(k); 
       } 
      } 
     } 

     return board; 
    } 
} 

enter image description here

+0

非常感谢!这有助于解决一切。 :) – AnujSuper9 2011-06-03 18:42:07

2
JButton[][] buttons = new JButton [10][10]; 

只会创建一个空数组,您必须初始化按钮。并向面板添加null会导致NPE。

1

刚刚试过你的代码,并注意到你得到NPE是因为你刚刚创建了一个按钮数组,但是并没有在其中创建按钮,所以你的按钮数组包含空值,并且在试图将它添加到板上时会引发NPE。

尝试这样的事情在创建阵列后:

private static JPanel Board() { 
    JButton[][] buttons = new JButton [10][10]; 
    for (int i = 0; i < 10; i++) { 
     for (int j = 0; j < 10; j++) { 
      buttons[i][j] = new JButton(); 
     } 
    } 
    JPanel board = new JPanel(new GridLayout(11,11)); 

    String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"}; 
    String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"}; 
    JTextField k; 

    // ---- your remaining code. 

} 
+0

感谢您的提示!我不再得到NPE,但网格仍然没有出现在我的GUI上... – AnujSuper9 2011-06-02 03:57:41