java 不推荐使用的方法,用什么代替?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deprecated method, what to use instead?
提问by Niklas
The method show();
in class Windows for java.awt in deprecated. What can I use instead?
show();
不推荐使用 java.awt 的类 Windows 中的方法。我可以用什么代替?
package adventure;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import java.applet.*;
// Infogar testkommentar mvh Holger 2012-06-20 kl 19.03
public class Adventure extends Frame {
private static final long serialVersionUID=100L;
public Adventure() {
setSize(850, 440);
World world = new DungeonWorld ( this );
Person me = new Person( world, "You", null );
show();
me.goTo("Dungeon");
add( new Player( world, me ) );
addWindowListener(new MyWindowAdapter ());
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
}
// Load an image from the net, making sure it has already been
// loaded when the method returns
public Image loadPicture ( String imageName ) {
Image im = null;
// Load the image from the net
try {
URL imageSource = new URL( "http://www...xxx/"
+ imageName );
try {
im = createImage( (ImageProducer) imageSource.getContent());
} catch (IOException e) {}
} catch (MalformedURLException e ) { }
// Wait to ensure that the image is loaded
MediaTracker imageTracker = new MediaTracker( this );
imageTracker.addImage( im, 0 );
try {
imageTracker.waitForID( 0 );
}
catch( InterruptedException e ) { }
return im;
}
// Load and play a sound from /usr/local/hacks/sounds/
public void playSound (String name) {
URL u = null;
try {
u = new URL("file:" + "/usr/local/hacks/sounds/" + name + ".au");
} catch (MalformedURLException e ) { }
AudioClip a = Applet.newAudioClip(u);
a.play();
}
public static void main (String[] args) {
System.out.println("test");
new Adventure();
}
}
回答by Bertram Nudelbach
Let's read the Java API for Window#show()
: here
让我们阅读 Java API Window#show()
:这里
@Deprecated
public void show()
Deprecated. As of JDK version 1.5, replaced by
setVisible(boolean)
.Makes the Window visible. If the Window and/or its owner are not yet displayable, both are made displayable. The Window will be validated prior to being made visible. If the Window is already visible, this will bring the Window to the front.
已弃用。从 JDK 1.5 版开始,由
setVisible(boolean)
.使窗口可见。如果 Window 和/或其所有者尚不可显示,则两者都可显示。窗口将在可见之前进行验证。如果 Window 已经可见,这会将 Window 带到前面。
So you should use Window#setVisible(boolean)
- for show()
use setVisible(true)
.
所以你应该使用Window#setVisible(boolean)
-for show()
use setVisible(true)
。
EDIT
编辑
In some environments just replacing show()
with setVisible(true)
changes the behavior of the application. This happens, when you wrote a subclass of Window
that overrides show()
(same for hide()
).
在某些环境中只更换show()
与setVisible(true)
变化的应用程序的行为。当您编写Window
该覆盖的子类时会发生这种情况show()
(与 相同hide()
)。
So in your code example setVisible(true)
does exactly the same as show()
. But in general, just be certain, that no one overrides show()
which wouldn't be executed anymore when using setVisible(true)
. In such a case, you have to change the overriden methods, too.
所以在你的代码示例setVisible(true)
中与show()
. 但总的来说,请确定,show()
在使用setVisible(true)
. 在这种情况下,您也必须更改被覆盖的方法。
回答by tom
If you check the API you will see:
如果您检查 API,您将看到:
void show()
Deprecated. As of JDK version 1.5, replaced by
setVisible(boolean)
.
已弃用。从 JDK 1.5 版开始,由
setVisible(boolean)
.