Habe folgendes Problem mit einem Applet :
Das Applet: siehe hier (Das obere funktioniert, es geht um das linke.)
Beim ersten Aufrufen klappt alles. ABER beim 2. mal gibt es in der Java-Konsole folgende Exception :
Kann es sein, daß der MouseListener daran schuld ist??? Mit einem ActionListener scheint es zu funktionieren.
Die Methode "paintPolygon" ist nur zum Testen in beiden Klassen.
Wenn jemand einen Grund weiß, würde ich mich freuen, wenn er was dazu schreibt!
Dom.
EDIT: Möglicherweise hab ich eine Lösung : habe die paintPolygon mal mit einer "try {} catch (Exception e) {}" versehen und siehe da - ES GEHT! (Bist auf die anderen Exceptions)
Das Applet: siehe hier (Das obere funktioniert, es geht um das linke.)
Beim ersten Aufrufen klappt alles. ABER beim 2. mal gibt es in der Java-Konsole folgende Exception :
Exception in thread "AWT-EventQueue-11" java.lang.NullPointerException
at hp.Menue.paintPolygon(Menue.java:81)
at hp.ButtonMouseListener.mouseEntered(Menue.java:143)
at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)
at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
........usw.
Kann es sein, daß der MouseListener daran schuld ist??? Mit einem ActionListener scheint es zu funktionieren.
Code:
package hp;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
public class Menue extends JApplet {
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
static JButton home = new JButton("Startseite");
static JButton version = new JButton("Version");
static JButton impressum = new JButton("Impressum");
static JButton link = new JButton("Link´s");
static JButton programm = new JButton("Download´s");
static JButton gaestebuch = new JButton("Gästebuch");
Graphics g;
JPanel pane;
String url = null;
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
public Menue() {
}
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
super.init();
home.reshape(40, 10, 120, 30);
home.setName("home");
home.addMouseListener(new ButtonMouseListener(home, pane, this));
programm.reshape(40, 50, 120, 30);
programm.setName("programm");
programm.addMouseListener(new ButtonMouseListener(programm, pane, this));
version.reshape(40, 90, 120, 30);
version.setName("version");
version.addMouseListener(new ButtonMouseListener(version, pane, this));
link.reshape(40, 130, 120, 30);
link.setName("link");
link.addMouseListener(new ButtonMouseListener(link, pane, this));
gaestebuch.reshape(40, 170, 120, 30);
gaestebuch.setName("gaestebuch");
gaestebuch.addMouseListener(new ButtonMouseListener(gaestebuch, pane, this));
impressum.reshape(40, 210, 120, 30);
impressum.setName("impressum");
impressum.addMouseListener(new ButtonMouseListener(impressum, pane, this));
pane = new JPanel();
pane.setLayout(null);
pane.add(home);
pane.add(programm);
pane.add(version);
pane.add(link);
pane.add(gaestebuch);
pane.add(impressum);
pane.repaint();
pane.validate();
setContentPane(pane);
}
public void paintPolygon(Graphics g, String name) {
Graphics2D g2D = (Graphics2D) g;
int h = 0;
if (name == "home") { h = 0; }
if (name == "programm") { h = 40; }
if (name == "version") { h = 80; }
if (name == "link") { h = 120; }
if (name == "gaestebuch") { h = 160; }
if (name == "impressum") { h = 200; }
int[] i = {10, 30, 10};
int[] j = {10+h, 25+h, 40+h};
int k = 3;
g2D.setColor(new Color(255, 255, 255));
g2D.fillRect(0, 0, 40, 500);
GradientPaint gradientPaint = new GradientPaint(0, 10+h,
Color.blue,
0, 25+h,
Color.yellow,
true);
g2D.setPaint(gradientPaint);
g2D.fillPolygon(i, j, k);
}
public void paintComponent(Graphics g) {
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
public String getAppletInfo() {
return "Applet Information";
}
public String[][] getParameterInfo() {
return null;
}
}
class ButtonMouseListener implements MouseListener {
JButton button;
JPanel pane;
Menue adaptee;
String url = "";
ButtonMouseListener(JButton button, JPanel pane, Menue adaptee) {
this.button = button;
this.pane = pane;
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
adaptee.paintPolygon(adaptee.getGraphics(), button.getName());
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (button.getName() == "home") { url = "http://dvddatabase.zweipage.de/text_58476398_07594370_69255194_deutsch.html"; }
if (button.getName() == "programm") { url = "http://dvddatabase.zweipage.de/text_58476398_1393814_69255194_deutsch.html"; }
if (button.getName() == "version") { url = "http://dvddatabase.zweipage.de/text_58476398_98333865_69255194_deutsch.html"; }
if (button.getName() == "link") { url = "http://dvddatabase.zweipage.de/text_58476398_3181852_69255194_deutsch.html"; }
if (button.getName() == "impressum") { url = "http://dvddatabase.zweipage.de/text_58476398_32768152_69255194_deutsch.html"; }
if (button.getName() == "gaestebuch"){ url = "http://dvddatabase.zweipage.de/ext_58476398_guestbook_49925691_deutsch.html"; }
try {
adaptee.getAppletContext().showDocument(new URL(url), "frame3");
} catch(MalformedURLException urlE) {
System.out.println(urlE.getMessage());
}
}
public void mouseReleased(MouseEvent e) {
}
public void paintPolygon(Graphics g, String name) {
Graphics2D g2D = (Graphics2D) g;
int h = 0;
if (name == "home") {
h = 0;
}
if (name == "programm") {
h = 40;
}
if (name == "version") {
h = 80;
}
if (name == "link") {
h = 120;
}
if (name == "gaestebuch") {
h = 160;
}
if (name == "impressum") {
h = 200;
}
int[] i = {10, 30, 10};
int[] j = {10 + h, 25 + h, 40 + h};
int k = 3;
g2D.setColor(new Color(255, 255, 255));
g2D.fillRect(0, 0, 40, 500);
GradientPaint gradientPaint = new GradientPaint(0, 10 + h,
Color.blue,
0, 25 + h,
Color.yellow,
true);
g2D.setPaint(gradientPaint);
g2D.fillPolygon(i, j, k);
}
}
Die Methode "paintPolygon" ist nur zum Testen in beiden Klassen.
Wenn jemand einen Grund weiß, würde ich mich freuen, wenn er was dazu schreibt!
Dom.
EDIT: Möglicherweise hab ich eine Lösung : habe die paintPolygon mal mit einer "try {} catch (Exception e) {}" versehen und siehe da - ES GEHT! (Bist auf die anderen Exceptions)