1.: 先定义一个变量 在一个类的开始:private JList flightsList;2.然后写一个方法 :public void doQuery( )
{String startAirport = (String)from.geItem();
String timeDate = date.getText();if( manager.getFlights(startAirport, endAirport,timeDate) != null ){
Flight [] flightQuery = manager.getFlights(startAirport, endAirport,timeDate);
int length = flightQuery.length;
String [] number = new String[length];
System.arraycopy(flightQuery,0,number,0,length);
//flightsList = new JList( number );
}
}
3. make a constructor, 然后我想调用那个方
法在constructor里,for putting the array number in the jlist( flightsList)
code: flightsList = new JList( number );
contents.add( new JScrollPane( flightsList) , BorderLayout.CENTER );
But it does not work.I know it is the variable range,how to pass the number into the flightsList?数组number 里的内容没显示出来,这应该是变量作用域的原因吧,请问怎样才能把数组number
的内容在我创建的jlist( flightsList)中显示出来呢?
{String startAirport = (String)from.geItem();
String timeDate = date.getText();if( manager.getFlights(startAirport, endAirport,timeDate) != null ){
Flight [] flightQuery = manager.getFlights(startAirport, endAirport,timeDate);
int length = flightQuery.length;
String [] number = new String[length];
System.arraycopy(flightQuery,0,number,0,length);
//flightsList = new JList( number );
}
}
3. make a constructor, 然后我想调用那个方
法在constructor里,for putting the array number in the jlist( flightsList)
code: flightsList = new JList( number );
contents.add( new JScrollPane( flightsList) , BorderLayout.CENTER );
But it does not work.I know it is the variable range,how to pass the number into the flightsList?数组number 里的内容没显示出来,这应该是变量作用域的原因吧,请问怎样才能把数组number
的内容在我创建的jlist( flightsList)中显示出来呢?
解决方案 »
- BufferedReader类
- 自定义ClassLoader的问题!!
- 请教:为什么在构造方法中不能一起使用this()和super这样,感谢
- 关于JText组件append()方法的问题
- 大侠请进!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 如何将一个数组中的内容读入到一个新建的文件中?
- 请教几个概念方面的问题,弄懂马上揭贴!
- Java不允许我们在数组定义时指定一个数组有多大。
- 十万火急!!各位高手、大虾快快帮忙,实现后一定给分!!!
- 在CLASSPATH里怎样设置,才能使路径总能指到当前的目录???
- The method **() is undefined for the type
- java ftp server
这个是在一个方法里面flightsList = new JList( number );
这个number 有事什么?肯定不是上面的那个numer,因为那个是局部变量。你是不是还有一个类变量啊!
String [] number; 把第一个地方改成
number = new String[length];
看看
I have tried the way:make 一个类变量,在一个方法里赋值给它,再传到constructor
中的 flightsList = new JList( number ); it can be compiled,but when running,appear :nullpointerexception,这说明我在constructor调用的方
法,没有把值传到我 定义的类变量去。why???前辈,我想是否要用return返回number?
前辈 这是与此相关的code
:
public class gui extends JFrame
{ private JList flightsList;
private String[] number ;
//method
public void doQuery( )
{ String startAirport = (String)from.geItem();
String timeDate = date.getText(); if( manager.getFlights(startAirport, endAirport,timeDate) != null ) {
Flight [] flightQuery = manager.getFlights(startAirport, endAirport,timeDate);
int length = flightQuery.length;
number = new String[length];
System.arraycopy(flightQuery,0,number,0,length);
//flightsList = new JList( number );
}
}
//code in constructor JButton button = new JButton( "Query" );
button.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
doQuery(); //前辈,我在此调用那个方法想把值传到类变量
}
}
);
topPanel.add( button );
contents.add( topPanel, BorderLayout.NORTH);
flightsList = new JList( number );
contents.add( new JScrollPane(flightsList) ,BorderLayout.CENTER );