我在覆盖我的 JPanel 子类 ChordEditor 中的 paint() 方法时遇到问题。即使我重写paint() 方法,将其添加到框架中,然后调用repaint(),paint() 方法也不会被调用。打印输出“进入油漆功能”不打印。谁能帮我这个?
我的 ChordEditor 类:
public class ChordEditor extends JPanel{
ArrayList<Chord> chordArray = new ArrayList<Chord>();
public ChordEditor() {
this.repaint();
}
@Override
public void paint(Graphics g) {
System.out.println("entering paint function");
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Toolkit.getDefaultToolkit().sync();
System.out.println("drawing line");
g2d.drawLine(10, 10, 40, 40);
g.dispose();
}
}
我将它添加到 JFrame 的位置:
ChordEditor ce = new ChordEditor();
m_frame.getContentPane().removeAll();
m_frame.add(ce);
m_frame.getContentPane().repaint();