- Java code
import java.awt.*;import javax.swing.*;import java.awt.Graphics;class Test extends JFrame{ public static void main(String args[]){ Test frame = new Test(); frame.setSize(400,300); frame.setVisible(true); } public void paint(Graphics g){ g.drawString("abc",10,10); System.out.println("paintint.."); } public void paintComponent(Graphics g){ System.out.println("in paint component..."); g.drawString("bcd",20,20); }}
paint()调用了 为什么abc却没显示,还有 什么时候调用paintComponent方法呢?
------解决方案--------------------
- Java code
import java.awt.*;import javax.swing.*;import java.awt.Graphics;class Test extends JFrame{ public static void main(String args[]){ Test frame = new Test(); JPanel board = new PaintBoard(); board.setOpaque(false); frame.add(board); frame.setSize(400,300); frame.setVisible(true); } @Override public void paint(Graphics g){ super.paint(g); g.drawString("abc",10,10); System.out.println("paintint.."); } private static class PaintBoard extends JPanel{ @Override public void paintComponent(Graphics g){ super.paintComponent(g); System.out.println("in paint component..."); g.drawString("bcd",20,20); } }}
------解决方案--------------------
谢谢提醒,直接add是让人蛋疼的做法