2012-06-21 36 views
2

不贊成使用類Windows中的方法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(); 
    } 
} 
+2

如您所知,該方法已被棄用,您必須擁有對API的一些訪問權限。如果你讀了第一句話,你會看到用什麼來代替...... – brimborium

+2

我想你應該先開始閱讀Javadoc。 –

+2

我知道有文件,但我想從一個人的答案。有時候一個人知道的不僅僅是文件。例如,這裏和那裏都沒有文件。感謝您的評論。 –

回答

16

讓我們閱讀的Java API爲Window#show()here

@Deprecated 
public void show() 

已過時。從JDK 1.5版開始,替換爲setVisible(boolean)

使窗口可見。如果窗口和/或其所有者尚不可顯示,則兩者均可顯示。在使窗口可見之前,該窗口將被驗證爲 。如果窗口已經可見,這個 將把窗口放在前面。

所以你應該使用Window#setVisible(boolean) - 對於show()使用setVisible(true)

編輯

在某些環境中只更換show()setVisible(true)改變應用程序的行爲。發生這種情況的時候,你寫了一個Window的子類,它覆蓋了show()(與hide()相同)。

所以在你的代碼示例中setVisible(true)show()完全一樣。但總的來說,只要確定,沒有人重寫show(),在使用setVisible(true)時不會執行任何操作。在這種情況下,您也必須更改重寫方法。

5

如果您檢查API,你會看到:

void show()  

已過時。從JDK 1.5版開始,替換爲setVisible(boolean)

相關問題