java - 弃用的方法,用什么代替?

标签 java graphics window deprecated

<分区>

Windows 类中用于 java.awt 的方法 show(); 已弃用。我可以用什么代替?

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();
    }
}

最佳答案

让我们阅读 Window#show() 的 Java API:here

@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.

所以你应该使用 Window#setVisible(boolean) - 对于 show() 使用 setVisible(true)

编辑

在某些环境中,只需将 show() 替换为 setVisible(true) 即可更改应用程序的行为。当您编写覆盖 show()Window 子类时(与 hide() 相同),就会发生这种情况。

因此在您的代码示例中,setVisible(true)show() 完全相同。但总的来说,只要确定,没有人会覆盖 show(),它在使用 setVisible(true) 时将不再执行。在这种情况下,您也必须更改覆盖的方法。

关于java - 弃用的方法,用什么代替?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134028/

相关文章:

java - 尝试在 Java 中创建 json 对象但获取嵌套数组?如何以更好的方式做到这一点?

java - DynamoDB - 对象到 AttributeValue

java - 如何在不使消息出队的情况下浏览rabbitmq中的队列

wpf - 确定窗口在 WPF 中是否实际可见的最佳方法是什么

java - 将 YAML 列表映射到 Spring Boot 中的对象列表

java - 等距 map 绘制、生成

ios - 有充分的理由不让所有 iPhone/iPad 图形都只有视网膜尺寸吗?

opengl - fwidth glsl 函数实际用于什么?

c++ - Qt:最大化框架

android - Android 是否使用自定义窗口系统?