import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;
public class home1 extends JFrame
{private  JButton buttons[],submit;
 private  JLabel labels[];
 private   JTextField fields[];
 private static String[] strb={"开始交易","保存记录","修改记录","停止交易"};
 private static String[] strl={"账号","商品","价格","现金支付"};
 public home1()
 { 
JPanel arraybutton=new JPanel(new GridLayout(1,4));
      
     for(int count=0;count<4;count++)
      {buttons[count]=new JButton(strb[count]);
       arraybutton.add(buttons[count]);
      }
   JPanel inputtable=new JPanel(new GridLayout(4,4)); 
    try{ for(int count=0;count<4;count++)
     { labels[count]=new JLabel(strl[count]);
       inputtable.add(labels[count],count*4);
       fields[count]=new JTextField();
       inputtable.add(fields[count],count*4+1);
     }
      submit=new JButton("提交");
      inputtable.add(submit,15);
    }catch(Exception e){}
 
  Container container=getContentPane();
  container.setLayout(new BorderLayout(5,5));
  container.add(arraybutton,BorderLayout.NORTH);
  container.add(inputtable,BorderLayout.CENTER);
  setSize(500,500);
  setVisible(true); }
 public static void main(String[] args)
 {

  home1 application=new home1();
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }}

解决方案 »

  1.   

    稍微溜了一眼
    buttons[count]=new JButton(strb[count]); //应该是这里出错了,buttons还没有被new,所以直接使用buttons[count]会出异常
    首先应该buttons = new JButton[4];
      

  2.   

    不光是buttons,其他的数组对象,如labels等都是没有new就用了
      

  3.   

    谢谢,那在使用之前加buttons=new JButton[4]你看行不行
      

  4.   

    尽量在用NEW初始化这些基本的控件纯软件编译这些都运行不过去的.
      

  5.   

    谢谢,那在使用之前加buttons=new JButton[4]你看行不行