2016-04-22 9 views
0
//create and display a label containing icon and a string 
//JLabel and ImageIcon 

import java.awt.*; 
import java.applet.*; 
import javax.swing.*; 
import javax.swing.JLabel.*; 

public class JLabelDemo extends JApplet { 

    public void init() { 
     try { 
      SwingUtilities.invoke(
       new Runnable() { 
        public void run() { 
         makeGUI(); 
        } 
       } 
      ); 
     } 
     catch(Exception e) { 
      System.out.println("Cannot happen due to exception " + e); 
     } 
    } 

    private void makeGUI() { 
     //create an icon 
     ImageIcon ii=new ImageIcon("The Big Trip.png"); 
     //create a label 
     JLabel jl=new JLabel("India",ii,JLabel.CENTER); 
     add(jl);//add label to content pane 
    } 

} 
/*<applet code="JLabelDemo" height=250 width=150>*/ 

Dieser Code ausgeführt wird unter Verwendung zusammengestellt:Nicht in der Lage ein JLabel Programm

javac JLabelDemo.java 

Aber durch folgend mit laufendem cmd das funktioniert nicht (wird jedes Applet nicht angezeigt) !!

appletviewer JLabelDemo.java 
+2

1) Ihren Code nicht kompiliert. 2) Ich bin mir sicher, dass du deinen Code nicht linksbündig schreibst, also erwarte nicht, dass wir ihn auch so lesen. Post korrekt formatierten Code, der kompiliert. – camickr

+1

Wolltest du SwingUtilities.invokeAndWait aufrufen? Es gibt keine Aufrufmethode. – ManoDestra

+1

FYI: [Java Plugin-Unterstützung nicht mehr unterstützt] (http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) und [Wechsel zu einem Plugin-freien Web] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) – MadProgrammer

Antwort

0

Verwenden SwingUtilities.invokeAndWait (runnable obj) statt SwingUtilities.invoke (runnable obj)

0

Sie müssen stattdessen SwingUtilities.invokeAndWait nennen. Und Sie müssen auch Ihr Applet-Tag schließen, damit es im Appletviewer funktioniert. Und Sie müssen Ihren Inhalt zum Inhaltsteil hinzufügen, anstatt add zu verwenden, da dieser lediglich zum Container hinzugefügt wird.

JLabelDemo.java

//create and display a label containing icon and a string 
//JLabel and ImageIcon 

import java.awt.*; 
import java.applet.*; 
import javax.swing.*; 

public class JLabelDemo extends JApplet { 
    public void init() { 
     try { 
      SwingUtilities.invokeAndWait(
       new Runnable() { 
        public void run() { 
         this.makeGUI(); 
        } 
       } 
      ); 
     } catch (Exception e) { 
      System.out.println("Cannot happen due to exception "+e); 
     } 
    } 

    private void makeGUI(){ 
     //create an icon 
     ImageIcon ii = new ImageIcon("The Big Trip.png"); 

     //create a label 
     JLabel jl = new JLabel("India", ii, JLabel.CENTER); 
     //add label to content pane 
     this.getContentPane().add(jl); 
    } 
} 

HTML:

<applet code="JLabelDemo" width="150" height="250"></applet>