了解我的程序中的变量范围

时间:2013-03-23 02:01:18

标签: java variables scope

我是编程的新手,并成功地利用这个网站来学习和克服障碍。在我提出新问题之前,首先要感谢所有得到如此大帮助的人。

我正在开发我的第一个项目并解决了阻止程序运行的小问题。然而,通过猜测发现了问题的解决方案,我非常想知道原因。它应该是一个可变范围问题。这是该程序的片段?

public class AlladinLamp {    

JMenuBar jmBar;
JComboBox runners, raceDiv, days, months;       
JButton horseList, ok, clear;
JTextField[] jtxt;
JTextField raceName;
JTextArea message;
String status; 
JDialog dialog;
JPanel table, data, jp, middle;
JFrame jfr;     
String[] hNam ;                                    // string array holding horse names
int[] hNum, hVal;
int reducedFNH, id, vp, day, month, fnh, dv;       // fnh is the number of horses in the
                                                      race                  

Processes processes;
GridBagLayout gbLayout;  
GridBagConstraints gbc;  

...

public AlladinLamp()
{
 ...

public ActionListener horseListActionListener = new ActionListener()
{
  @Override
  public void actionPerformed( ActionEvent e )
  {
       Toolkit myKit = dialog.getToolkit();
       Dimension dialSize = myKit.getScreenSize();
       dialog.setLocation( dialSize.width/2, dialSize.height/4 );
       dialog.setSize( 260, 380 );
       dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );       

       jp.setLayout( gbLayout );  
       jtxt = new JTextField[ fnh ];            <--- array of textfields for the horse                                                                                                                 names

       JLabel label;
       String str;
       for( int i = 0; i < fnh; i++ )
       {                   
          gbc.gridx = 0;
          gbc.gridy = i;           
          str = new Integer( i+1 ) + ".";                  
          label = new JLabel( str ); 
          jp.add( label, gbc );         

          gbc.gridx = 1;
          gbc.gridy = i;
          gbc.ipady = 4;
          gbc.insets = new Insets(4,0,0,0);
          jtxt[i] = new JTextField(15);  
          jp.add( jtxt[i], gbc );               
       }
       JPanel buttonPanel = new JPanel();        
       ok = new JButton( "OK" );       
       ok.addActionListener( okActionListener );           
       buttonPanel.add( ok ); 
       clear = new JButton ( "CLEAR" );        
       clear.addActionListener( clearActionListener );   

       buttonPanel.add( clear );       
       JScrollPane jscr = new JScrollPane( jp );  
       dialog.add( jscr, BorderLayout.CENTER ); 
       dialog.add( buttonPanel, BorderLayout.SOUTH ); 
       dialog.setVisible( true );
   }
};  

...

public ActionListener okActionListener = new ActionListener()
{
  @Override
  public void actionPerformed( ActionEvent e )
  {       
    hNam = new String[ fnh ];
    hNum = new int[ fnh ];
    hVal = new int[ fnh ];  
    for( int i = 0; i < fnh; i++ )
    {               
      hNam[i] = jtxt[i].getText();             
      hNum[i] = reduce( i + 1 );       
      hVal[i] = nameValue( hNam[i] );   
    }
    System.out.println( Arrays.toString(hNam) );
    System.out.println( Arrays.toString(hVal) );
    setID( hNam );                              
   dialog.setVisible( false );       
  }
};

...

该程序基本上是一个接受多种输入的GUi。 horselist按钮的监听器构建一个jDialog,接受比赛中马名称的输入。

“确定”按钮的侦听器从文本字段中检索这些名称,并将它们存储在hNam数组中。现在,当我移动第一个tthree行初始化hNam,hVal和hNum到构造函数中时,我得到一个源自下一个语句的hNam[i] = jtxt[i].getText();

的ArrayIndexOutOfBounds异常

为什么会这样???

0 个答案:

没有答案