Java - 已弃用的方法 - 该怎么办?
我正在学习一系列 Java 教程,试图学习它。我对教程 72 有疑问。
链接: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)
但这会返回一个错误,指出“JList 类型中的方法 setListData(Object[]) 不适用于参数(列表)'
。
替换上述语句的正确方法是什么?
另外,我想借此机会再问一个无关的问题。在方法外部初始化变量是否更好,如下所示:
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"};
与(如视频中所示)相比:像上面一样在类外部声明变量,但在方法内部为其赋值?
两者表现更好吗?
I am following a series of Java tutorials in an attempt to learn it. I have a question about tutorial 72.
Link: http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu
At 7:02 of the video, this statement is written. However, this method has been deprecated in Java 1.7.
RightList.setListData(LeftList.getSelectedValues());
Eclipse returns the following error:
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)
But this returns an error saying 'The method setListData(Object[]) in the type JList is not applicable for the arguments (List)'
.
What is the correct way to replace the above statement?
Also, I want to take this opportunity to ask a another unrelated question. Is it better to initialize variables outside the method like so:
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"};
Compared to (As shown in the video): Declaring variables outside the class like above, but assigning values to them inside the method?
Does either perform better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Java7 的 JList javadoc,我发现您确实别无选择 - 这两个 API(getSelectedValuesList 和 setDataList)是不相关的。
要解决这个问题,一个简单的解决方案是执行
LeftList.getSelectedValuesList().toArray()
- 它将为您提供一个适合setDataList
的数组。免责声明:我不知道这是否是 Java 推荐的“正确”用法,但它应该可以工作。另外,请注意,已弃用的 API 并不意味着它不起作用 - 如果您觉得现在不想在其中投入时间,您仍然可以使用旧的 API(就像您正在做教程和不是一些将在未来 10 年内投入生产的正在进行的产品)
至于第二个问题 - 这是一个品味问题,我更喜欢声明变量而不在类声明中初始化它们并在构造函数中使用值设置它们。通常为常量赋予初始值(例如
public static final String AAA = "XYZ";
)According to
JList
javadoc for Java7 I see that indeed you have no option - the two APIs (getSelectedValuesList
andsetDataList
) are unrelated.To solve it, a simple solution would be to perform
LeftList.getSelectedValuesList().toArray()
- it will provide you with an array suitable forsetDataList
. Disclaimer: I don't know if this is the "correct" usage recommended by Java, but it should work.Also, note that a deprecated API does not mean it doesn't work - if you feel you don't want to invest time in it now, you can still use the old API (like in your situation where you are doing a tutorial and not some ongoing product that will be in production for the next 10 years)
As for the 2nd question - it is a matter of taste, I prefer declaring the variables without initializing them in the class declaration and setting them with values in the constructor. It is customary to give initial values to constants (e.g.
public static final String AAA = "XYZ";
)您需要更新
setListData
方法以采用新的参数类型(以及任何其他需要Object[]
的代码,包括方法、可能迭代的内容)数组等)不过,仅仅因为某些内容被弃用并不意味着它被删除。做什么取决于您的近期目标:是学习材料,还是学习材料并更新所有源代码以在没有警告的情况下进行编译。
You'd need to update the
setListData
method to take the new parameter type (and any other code that was expecting anObject[]
, including methods, possible things that iterate over the array, etc.) Just because something is deprecated doesn't mean it's removed, though.What to do depends on your immediate goal: is it to learn the material, or to learn the material and update all the source to compile without warnings.
我查看了相关教程。
对于有关 API 的问题,您需要执行以下操作:
PS:一些有关样式的提示。在Java中,变量通常以小写字母开头,类名以大写字母开头。在上面的代码中,我认为您正在尝试调用类上的静态方法,因此您可能需要将名称更改为小写。
I looked at the tutorial in question.
For your question about API, you would need to do the following:
PS: some tips about style. In Java, variables usually begin with a lowercase alphabet and class names begin with an uppercase alphabet. In your code above, it looked to me that you were trying to call a static method on a class, so you might want to change the names to lowercase.