我正在跟随一系列Java教程来学习Java。我对第72篇教程有一个问题。
链接:http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu 在视频的7:02处,写下了这个声明。然而,在Java 1.7中,该方法已被弃用。
与(如视频所示):在类外声明变量,但在方法内赋值相比,哪个更好? 是否有性能上的区别?
链接:http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu 在视频的7:02处,写下了这个声明。然而,在Java 1.7中,该方法已被弃用。
RightList.setListData(LeftList.getSelectedValues());
Eclipse返回以下错误: Object[] javax.swing.JList.getSelectedValues()
getSelectedValues
@Deprecated
public Object[] getSelectedValues()
Deprecated. As of JDK 1.7, replaced by getSelectedValuesList()
Returns an array of all the selected values, in increasing order based on their indices in the list.
Returns:
the selected values, or an empty array if nothing is selected
See Also:
isSelectedIndex(int), getModel(), addListSelectionListener(javax.swing.event.ListSelectionListener)
但是这会返回一个错误,错误信息为“The method setListData(Object[]) in the type JList is not applicable for the arguments (List)”。
什么才是替代上述语句的正确方式呢?
另外,我想借此机会问一个不相关的问题:在方法之外初始化变量是否更好,像这样:
private JList LeftList = new JList();
private JList RightList = new JList();
private JButton Move = new JButton("Move -->");
private static String[] Items = {"Item 1", "Item 2","Item 3","Item 4","Item 5"};
与(如视频所示):在类外声明变量,但在方法内赋值相比,哪个更好? 是否有性能上的区别?
LeftList
是什么类型的变量?它是JList吗?同时显示实际的错误消息,而不是您的解释。 - Hovercraft Full Of Eels