新的細節:我的JPanel對象從我的主類的actionPerformed事件中調用時不工作的快速重畫問題(Canvas對象)
重繪方法。它在主類ImageViewer的ImageViewer方法內工作,但不能用於此類的actionPerformed方法。
這些是不能正常工作(重繪部分)我的代碼的關鍵部分:
主類:
/**
* @(#)NeatImageViewer.java
*
* NeatImageViewer application
*
* @author
* @version 1.00 2010/11/1
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class NeatImageViewer extends JFrame implements ActionListener {
GraphicsPanel gp;
NeatImageViewer() {
//... window components ...
}
public static void main(String[] args) {
NeatImageViewer niv = new NeatImageViewer();
niv.setSize(500,500);
niv.setLocationRelativeTo(niv);
niv.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//...
gp = new GraphicsPanel();
gp.img = img;
gp.repaint(); //<!--- Not Working!
this.add(gp);
//...
}
}
GraphicsPanel類:
/**
* @(#)GraphicsPanel.java
*
*
* @author
* @version 1.00 2010/11/1
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class GraphicsPanel extends JPanel {
BufferedImage img;
GraphicsPanel() {
super();
}
protected void paintComponent(Graphics g) {
//paint method isn't executed???
super.paintComponent(g);
System.out.println("Paint!");
}
}
」不適用於此類的actionPerformed方法。「對我們毫無意義。發佈你的SSCCE(http://sscce.org)來證明問題。 – camickr 2010-11-01 20:16:39