【问题标题】:Deprecated method, what to use instead? [closed]不推荐使用的方法,改用什么? [关闭]
【发布时间】:2012-06-21 08:10:59
【问题描述】:

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

【问题讨论】:

  • 如您所知,该方法已被弃用,您必须对 API 有一定的访问权限。如果您阅读第一个短语,您将看到该使用什么...
  • 我想你应该先开始阅读 Javadoc。
  • 我知道有文档,但我想从一个人那里得到答案。有时一个人知道的不仅仅是文档。例如,到处都有未记录的东西。感谢 cmets。

标签: java graphics window deprecated


【解决方案1】:

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

@Deprecated
public void show()

已弃用。从 JDK 1.5 版开始,被setVisible(boolean) 取代。

使窗口可见。如果窗口和/或其所有者尚未 displayable,两者都可以显示。窗口将被验证 在变得可见之前。如果窗口已经可见,这 会将窗口带到前面。

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

编辑

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

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

【讨论】:

    【解决方案2】:

    如果您检查 API,您将看到:

    void show()       
    

    已弃用。从 JDK 1.5 版开始,被setVisible(boolean) 取代。

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多