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);
}}
//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);
}}
buttons[count]=new JButton(strb[count]); //应该是这里出错了,buttons还没有被new,所以直接使用buttons[count]会出异常
首先应该buttons = new JButton[4];