据我所知,这段代码不在内部类中,我能找到的唯一相关问题是关于这段代码何时在内部类中运行。
相关代码:
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;
完整代码:
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);
}
}
假设我认为是正确的,那么带有paintComponent 的部分不在内部类或匿名类中,并且不需要“限定符”,这对我也不起作用......
编辑
好的,我现在明白了,谢谢。