Ich versuche, Beispiel 1-2 aus dem Java In A Nutshell Book zu kompilieren, das sich mit Java 1.0 beschäftigt. Ich bekomme eine Fehlermeldung, dass diese Option nicht in Button umgewandelt werden kann. Nun, ich weiß nicht, ob dies ein Problem von aktuellem Java ist, das die Bibliotheken, die ich anrufe, nicht unterstützt, oder das etwas aus ist.Java 1.0 Applet - Inkompatible Typen: Choice kann nicht in Button konvertiert werden - Java In Kürze Beispiel 1-2
Das Applet soll eine Art Zeichnung Pad sein, und der Fehler ist in Zeile 14/clear_button = new Choice();
import java.applet.*;
import java.awt.*;
public class Scribble extends Applet {
private int last_x = 0;
private int last_y = 0;
private Color current_color = Color.black;
private Button clear_button;
private Choice color_choices;
public void init(){
this.setBackground(Color.white);
clear_button = new Choice();
clear_button.setForeground(Color.black);
clear_button.setBackground(Color.lightGray);
this.add(clear_button);
color_choices = new Choice();
color_choices.addItem("black");
color_choices.addItem("red");
color_choices.addItem("yellow");
color_choices.addItem("green");
color_choices.setForeground(Color.black);
color_choices.setBackground(Color.lightGray);
this.add(new Label("Color: "));
this.add(color_choices);
}
public boolean mouseDown(Event e, int x, int y){
last_x = x; last_y = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y){
Graphics g = this.setGraphics();
g.setColor(current_color);
g.drawline(last_x, last_y, x, y);
last_x = x;
last_y = y;
return true;
}
public boolean action(Event event, Object arg) {
if (event.target == clear_button) {
Graphics g = this.getGraphics();
Rectangle r = this.bounds();
g.setColor(this.getBackground());
g.fillRect(r.x, r.y, r.width, r.height);
return true;
}
else if (event.target == color.choices) {
if (arg.equals("black")) current_color = color.black;
else if (arg.equals("red")) current_color = color.red;
else if (arg.equals("yellow")) current_color = color.yellow;
else if (arg.equals("green")) current_color = color.green;
return true;
}
else return super.action(event, arg);
}
}
Warum eine so alte Version von Java verwenden? – Reimeus
Ihnen einen Gefallen tun und Applets vergessen, sie werden als alte Technologie mit Sicherheitsproblemen betrachtet und sollten nicht mehr verwendet werden – JohnnyAW
* "Ich versuche, Beispiel 1-2 aus dem Java In A Nutshell Book zu kompilieren .." * Holen Sie sich mehr modernes Buch. 1) Siehe [Warum CS-Lehrer ** aufhören sollten ** Java-Applets zu unterrichten] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Siehe [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) .. –