2016-04-21 24 views
0

Dieser Code ist nicht in einer inneren Klasse, soweit ich das beurteilen kann, und die einzige relevante Frage, die ich finden kann ist, wenn dieser Code in einer inneren Klasse ausgeführt wird.Fehler: kann das Symbol "super.paintComponent (pinsel)" nicht finden in nicht-inneren Klasse ausgeführt werden

Relevante Code:

public JDemoLocation() 
{ 
    setTitle("Fireworks"); 
    setLayout(new FlowLayout()); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    add(button); 
    button.addActionListener(this); 
} 

public void paintComponent(Graphics brush) 
{ 
    super.paintComponent(brush); 
    if (drawIt) 
    { 
     x = 450; 
     y = 500; 
     z = 250; 

Voll Code:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.lang.Thread; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class JDemoLocation extends JFrame implements ActionListener 
{ 
    JButton button = new JButton("Fire!"); 
    int y = 50; 
    int x = 50; 
    int z = 500; 
    int v = 500; 
    boolean drawIt = false; 

    final int GAP = 30; 

    public JDemoLocation() 
    { 
     setTitle("Fireworks"); 
     setLayout(new FlowLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     add(button); 
     button.addActionListener(this); 
    } 

    public void paintComponent(Graphics brush) 
    { 
     super.paintComponent(brush); 
     if (drawIt) 
     { 
     x = 450; 
     y = 500; 
     z = 250; 
     v = 500; 
     brush.drawLine(z, v, x, y); 
     x = 550; 
     y = 500; 
     z = 750; 
     v = 500; 
     brush.drawLine(z, v, x, y); 
     x = 500; 
     y = 550; 
     z = 500; 
     v = 750; 
     brush.drawLine(z, v, x, y); 
     x = 500; 
     y = 450; 
     z = 500; 
     v = 250; 
     brush.drawLine(z, v, x, y); 
     x = 550; 
     y = 450; 
     z = 750; 
     v = 250; 
     brush.drawLine(z, v, x, y); 
     x = 550; 
     y = 550; 
     z = 750; 
     v = 750; 
     brush.drawLine(z, v, x, y); 
     x = 450; 
     y = 550; 
     z = 250; 
     v = 750; 
     brush.drawLine(z, v, x, y); 
     x = 450; 
     y = 450; 
     z = 250; 
     v = 250; 
     brush.drawLine(z, v, x, y); 
     int xPoints[] = {502, 512, 532, 512, 520, 500, 475, 488, 469, 492, 502}; 
     int yPoints[] = {468, 492, 498, 510, 535, 515, 532, 505, 488, 490, 468}; 
     brush.drawPolygon(xPoints, yPoints, xPoints.length); 
     } 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     drawIt = true; 
     repaint(); 
    } 


public static void main(String[] args) 
    { 
     JDemoLocation frame = new JDemoLocation(); 
     frame.setSize(1000, 1000); 
     frame.setVisible(true); 
    } 
} 

Unter der Annahme, was ich für richtig halten, dann wird der Abschnitt mit paintcomponent ist nicht in einer inneren Klasse oder anonyme Klasse, und doesn‘ t müssen die "Qualifier", die auch nicht für mich arbeiten ...

EDIT

Ok, ich habe es jetzt dank.

+0

Es Problem überhaupt nicht eine innere Klasse - es ist, dass es als 'paintComponent' in JFrame keine solche Methode ist. Schau dir die Dokumentation an ... http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html –

Antwort

0

JFrame ist kein JComponent, es hat keine paintComponent-Methode, die Sie überschreiben können. Stattdessen können Sie eine JPanel erweitern und es dem Rahmen hinzufügen.

Sehen Sie diese Antwort https://stackoverflow.com/a/27179775/490502 für Beispielcode

+0

Danke Entea, ich habe Fortschritte gemacht, aber jetzt hat die Zeichnung gewonnen ' t anzeigen, bis ich das Fenster aus irgendeinem Grund maximiere. – Kiflaam

+0

Macht nichts, hab es jetzt. Hatte den neuen Rahmen an der falschen Stelle vor dem Button. Danke für die Hilfe! – Kiflaam

0

Ihre Basisklasse JFrame hat keine Methode namens paintComponent().