cannot find symbol - symbol : method

Status
Nicht offen für weitere Antworten.

akaDisi

Aktives Mitglied
Hi, ich habe das Problem, dass ich aus einem AktionListener heraus eine Funktion in einer Canvas-Klasse ausführen möchte.
Es ist wahrscheinlich etwas total banales, aber ich komm im Moment einfach nicht drauf :-\

Hier mal die meines Erachtens nach die Wichtigesten Passagen meines Programms:

Java:
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class Applet extends JApplet {
  public Canvas cvsAnzeige = new GrafikCanvas();
[...]
  public void init() {
[...]
  Container cp = getContentPane();
  cp.setLayout(null);
[...]
  cvsAnzeige.setBounds(0, 0, 600, 473);
  cp.add(cvsAnzeige);
[...]
  public void btnShow_ActionPerformed(ActionEvent evt) {
[...]
  cvsAnzeige.zeichnen();
[...]
  }//ende Listener
}//ende class

class GrafikCanvas extends Canvas {
    public GrafikCanvas(){
    }
[...]
    public void zeichnen(){
      Graphics grafik = this.getGraphics();
      
      grafik.drawString("Hallo", 6,9);
    }
}

ich hoffe ihr könnte damit was anfangen...

Sollte iwas an Code fehlen, dann reiche ich das nach.

MfG Disi
 

Der Müde Joe

Top Contributor
canvas final setzen, wenn ich mich nicht irre. (so verstückelt das teil)

> Graphics grafik = this.getGraphics();

igit....mach das NIE !! paint Methode überschreiben.

Swing und AWT mischen. Generell schlechte Idee.
 

dayaftereh

Top Contributor
Hey, also ich habe mal deine Code ein wenig verändert und ich kann die Zeichen Methode aus dem AktionListener aufrufen.

Schau mal hier:
Java:
import java.awt.Canvas;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;

public class Applet extends JApplet {
	
	private static final long serialVersionUID = -448098890644311107L;
	
	public Canvas cvsAnzeige = new GrafikCanvas();

	// [...]
	
	public void init() {

		// [...]
		
		Container cp = getContentPane();
		cp.setLayout(null);
		
		// [...]
		
		cvsAnzeige.setBounds(0, 0, 600, 473);
		cp.add(cvsAnzeige);
		
		// [...]

		JButton b = new JButton();
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				((GrafikCanvas) cvsAnzeige).zeichnen();
			}
		});
		cp.add(b);

		// [...]
	}

	private class GrafikCanvas extends Canvas {
				
		private static final long serialVersionUID = -2362983907655953833L;
		
		// [...]

		public GrafikCanvas() {
			
		}
		
		// [...]
		
		public void zeichnen() {
			Graphics grafik = this.getGraphics();
			grafik.drawString("Hallo", 6, 9);
		}
		
		// [...]
	}

}
 

akaDisi

Aktives Mitglied
> Graphics grafik = this.getGraphics();

igit....mach das NIE !! paint Methode überschreiben.

Swing und AWT mischen. Generell schlechte Idee.

Das ist mein Problem, die habe ich schon überschrieben .... da wird ein Koordinatensystem gezeichnet und ich will dann nachträglich, nachdem ich im hauptprogramm die Werte eingeben gelassen habe die Koordinaten ausrechnen und dann erst zeichnen


@ dayaftereh:

Keine Ahnung warum, aber mit dem (heißt das Ding so?) Cast geht es, danke ;)
 

faetzminator

Gesperrter Benutzer
Wenn du paint() überschreibst, kannst du jederzeit repaint() aufrufen, es gibt also keinen Grund paint() nicht zu überschreiben.
 

dayaftereh

Top Contributor
Hey, schau dir mal das Code beispiel an, hier wird eine Parabel auf eine JPanel mit Punkten gezeichnet, aber erst wenn man den Button Draw drückt. vielicht hilft es dir.
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Applet extends JApplet implements ActionListener {

	private static final long serialVersionUID = -448098890644311107L;

	public JPanel drawPanel = null;

	private JFrame f = null;
	private List<Point> points = null;

	@Override
	public void init() {
		points = new ArrayList<Point>();
		initComponents();
		showFrame();
	}

	private void initComponents() {
		f = new JFrame();
		f.setTitle("Gitter");

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());

		drawPanel = new DrawPanel();
		drawPanel.setPreferredSize(new Dimension(800, 600));
		f.add(drawPanel, BorderLayout.CENTER);

		JButton draw = new JButton("Draw");
		draw.addActionListener(this);
		f.add(draw, BorderLayout.NORTH);
	}

	private void showFrame() {
		f.pack();
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}

	private class DrawPanel extends JPanel {

		private static final long serialVersionUID = 4420012036293406142L;

		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);
			g.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getHeight());
			g.setColor(Color.BLUE);

			synchronized (points) {
				for (Point p : points) {
					g.drawRect(p.x, p.y, 1, 1);
				}
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		int bereich = 40;
		for (int x = -bereich / 2; x <= bereich / 2; x++) {
			int xd = (x * -1) + drawPanel.getWidth() / 2;
			int yd = (x * x * -1) + drawPanel.getHeight() / 2;			
			synchronized (points) {
				points.add(new Point(xd, yd));
			}
		}

		drawPanel.repaint();
	}
}
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Cannot find Symbol Java Basics - Anfänger-Themen 5
A Cannot find symbol mit Konstruktoren Java Basics - Anfänger-Themen 27
A Cannot find symbol bei exceptions Java Basics - Anfänger-Themen 2
L cannot find symbol variable Kon Java Basics - Anfänger-Themen 8
F Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 5
R return: cannot find symbol Java Basics - Anfänger-Themen 2
L Bluej Error: Cannot find Symbol Java Basics - Anfänger-Themen 13
S Cannot find symbol (symbol ist eine Variable) Java Basics - Anfänger-Themen 13
N Cannot find symbol Java Basics - Anfänger-Themen 18
P Cannot find symbol, wieso? Java Basics - Anfänger-Themen 5
M Erste Schritte cannot find symbol - Probleme mit Klassen Java Basics - Anfänger-Themen 6
J Error: cannot find symbol - variable Java Basics - Anfänger-Themen 3
F Erste Schritte parseint: cannot find symbol Java Basics - Anfänger-Themen 6
M Vererbung - Cannot Find Symbol constructor... Java Basics - Anfänger-Themen 11
D error: cannot find symbol Java Basics - Anfänger-Themen 3
B Frage zu Beispielprogramm: "error: cannot find symbol" Java Basics - Anfänger-Themen 2
BlueMountain Erste Schritte error: cannot find symbol Java Basics - Anfänger-Themen 2
L Error: Cannot find symbol Java Basics - Anfänger-Themen 1
P Cannot find Symbol Java Basics - Anfänger-Themen 3
L Cannot Find Symbol - Was soll denn das bedeuten?!? Java Basics - Anfänger-Themen 7
P StdIn.readDouble: cannot find symbol Java Basics - Anfänger-Themen 7
B Fehler "Cannot find symbol - variable number1" Java Basics - Anfänger-Themen 13
B Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 6
K Cannot find symbol Java Basics - Anfänger-Themen 3
H cannot find symbol Java Basics - Anfänger-Themen 4
S cannot find symbol, symbol: constructor Java Basics - Anfänger-Themen 2
3 Compiler-Fehler Fehlerbehebung cannot find Symbol Java Basics - Anfänger-Themen 4
R Compiler-Fehler Cannot find symbol (Method printIn) Java Basics - Anfänger-Themen 3
B Polymorphie A obj = new B; "cannot find symbol app()" Java Basics - Anfänger-Themen 5
S wieso Fehlermeldung cannot find symbol hier Java Basics - Anfänger-Themen 10
T Cannot find Symbol(String) Java Basics - Anfänger-Themen 9
2 Compiler-Fehler cannot find symbol Java Basics - Anfänger-Themen 13
B Erste Schritte cannot find symbol - problem Java Basics - Anfänger-Themen 9
D Cannot find symbol variable Java Basics - Anfänger-Themen 9
A Compiler-Fehler Cannot find Symbol Java Basics - Anfänger-Themen 6
V Packages: Cannot find symbol Java Basics - Anfänger-Themen 12
J Cannot find Symbol Variable mit JPanels Java Basics - Anfänger-Themen 2
L Fehlermeldung: RealMirror.java cannot find symbol Java Basics - Anfänger-Themen 2
D Cannot Find Symbol Java Basics - Anfänger-Themen 6
M Compile Time Error - cannot find symbol (Objekt!) Java Basics - Anfänger-Themen 2
L Problem mit Vererbung (extends) cannot find symbol Java Basics - Anfänger-Themen 3
S Problem beim Kompilieren - cannot find symbol - constructor() Java Basics - Anfänger-Themen 12
N cannot find symbol URL Java Basics - Anfänger-Themen 2
S Problem mit Javaeditor: Cannot find Symbol Java Basics - Anfänger-Themen 13
P cannot find Symbol - WTF? Java Basics - Anfänger-Themen 4
StupidAttack cannot find symbol Java Basics - Anfänger-Themen 16
B Cannot find symbol-Fehler Java Basics - Anfänger-Themen 3
Luk10 Cannot find Symbol Daten Java Basics - Anfänger-Themen 10
E cannot find symbol??? Java Basics - Anfänger-Themen 8
B cannot find symbol Java Basics - Anfänger-Themen 11
N cannot find symbol Java Basics - Anfänger-Themen 11
T OOP cannot find symbol Java Basics - Anfänger-Themen 4
T cannot find symbol Java Basics - Anfänger-Themen 5
I Cannot find Symbol & NullPointerException Java Basics - Anfänger-Themen 8
A cannot find symbol. Java Basics - Anfänger-Themen 5
G cannot find symbol! Java Basics - Anfänger-Themen 22
K Ständige Fehlermeldung "Cannot find symbol" Java Basics - Anfänger-Themen 2
L cannot find symbol-method Java Basics - Anfänger-Themen 3
P Error: Cannot find Symbol Java Basics - Anfänger-Themen 4
J JOptionDialog "cannot find symbol" Java Basics - Anfänger-Themen 3
P JLayer 1.0 - cannot find symbol Java Basics - Anfänger-Themen 4
M cannot find symbol. Java Basics - Anfänger-Themen 7
T ChangeListener cannot find Symbol? Java Basics - Anfänger-Themen 2
D "identifier expected" und "cannot find symbol Java Basics - Anfänger-Themen 4
B cannot find symbol method equalsIgnoreCase? Java Basics - Anfänger-Themen 23
S Fehlermeldung cannot find symbol Java Basics - Anfänger-Themen 8
G Cannot find symbol, Suchfunktion benutzt Java Basics - Anfänger-Themen 3
A cannot find symbol :-( Java Basics - Anfänger-Themen 2
D Cannot find JUnit.framework Java Basics - Anfänger-Themen 1
G Collections.binarySearch(LinkedList): cannot find method Java Basics - Anfänger-Themen 6
R Cannot find a free socket for the debugger Java Basics - Anfänger-Themen 6
W Cannot find symbole variable Java Basics - Anfänger-Themen 4
M NullPointerException: Cannot read the array length because "this.Kinder" is null Java Basics - Anfänger-Themen 1
W Cannot resolve symbol 'HttpServlet' Java Basics - Anfänger-Themen 2
I JSON - cannot deserialize from Object value Java Basics - Anfänger-Themen 16
J Scanner cannot be resolved to a type Java Basics - Anfänger-Themen 3
N Fehler "Cannot instantiate the type" Java Basics - Anfänger-Themen 3
jakobfritzz Array- cannot invoke "" because "" is null Java Basics - Anfänger-Themen 4
Flo :3 Variablen Type dismatch: cannot convert from string to int Java Basics - Anfänger-Themen 9
C system cannot be resolved Fehler in Eclipse Java Basics - Anfänger-Themen 18
V ClientProtocolException cannot be resolved Java Basics - Anfänger-Themen 6
J The import org.bukkit cannot be resolved Java Basics - Anfänger-Themen 3
J Fehlermeldung unklar. non-static variable player0 cannot be referenced from a static context Java Basics - Anfänger-Themen 4
P non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 6
L constructor cannot be applied... Java Basics - Anfänger-Themen 22
M Cannot make a static reference to the non-static method Java Basics - Anfänger-Themen 10
P a cannot be resolved bei einer do while Schleife Java Basics - Anfänger-Themen 1
Aprendiendo Interpreter-Fehler "non-static variable this cannot be referenced from a static context" Java Basics - Anfänger-Themen 2
M Iterator cannot refer to a non final... Java Basics - Anfänger-Themen 20
T Error: int cannot be dereferenced Java Basics - Anfänger-Themen 10
J JLabel cannot be resolved Java Basics - Anfänger-Themen 8
H Variablen error: non-static variable cannot be referenced from a static context Java Basics - Anfänger-Themen 4
UnityFriday method getPrevious in class List<ContentType> cannot be applied to given types Java Basics - Anfänger-Themen 29
B OOP next cannot be resolved or is not a field Java Basics - Anfänger-Themen 6
B OOP Cannot instantiate the type AuDList<Integer> Java Basics - Anfänger-Themen 18
U Erste Schritte cannot be referenced from a static context Java Basics - Anfänger-Themen 1
D Java Eclipse cannot be cast to java.awt.event.ItemListener Java Basics - Anfänger-Themen 3
J Fehlermeldung : cannot invoke char(at) int on the primitive type int --- Anfänger Java Basics - Anfänger-Themen 5
M Erste Schritte [Variable] cannot be resolved to a variable Java Basics - Anfänger-Themen 4
M The Selection cannot be launched... Java Basics - Anfänger-Themen 4

Ähnliche Java Themen

Neue Themen


Oben