其实repaint()方法,好像是这样的,如果有paint()方法则调用paint()方法,没有paint()方法
而有paintComponent()方法则调用paintComponent()方法,下面代码就是为了测试。你可以测试有Paint()方法,和注释掉
paint()方法两种情况。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics2D;
public class TestPanel extends JFrame {
TTT panel = new TTT();
JPanel pnlCtl = new JPanel();
JButton button = new JButton("Draw line");
JButton button2 = new JButton("Clear all");
public TestPanel() {
Container cp = this.getContentPane();
cp.add(panel,BorderLayout.CENTER);
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
Graphics2D g2 = (Graphics2D) panel.getGraphics();
g2.drawRect(20,20,100,300);
panel.repaint();
}
});
button2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
panel.getGraphics().clearRect(0,0,700,400);
}
});
pnlCtl.add(button);
pnlCtl.add(button2);
cp.add(pnlCtl,BorderLayout.NORTH);
setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String[] args) {
TestPanel testPanel = new TestPanel();
}
}
class TTT extends JPanel
{
public TTT()
{
super();
}
public void paintComponent(Graphics g)
{
JOptionPane.showConfirmDialog(null,"paintComponent() called");
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(10,10,300,300);
}
//如果要试验没有paint()的情况注释掉下面的方法
public void paint(Graphics g)
{
JOptionPane.showConfirmDialog(null,"paint() called");
Graphics2D g2 = (Graphics2D) g;
g2.drawRect(20,20,300,300);
}
}
而有paintComponent()方法则调用paintComponent()方法,下面代码就是为了测试。你可以测试有Paint()方法,和注释掉
paint()方法两种情况。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics2D;
public class TestPanel extends JFrame {
TTT panel = new TTT();
JPanel pnlCtl = new JPanel();
JButton button = new JButton("Draw line");
JButton button2 = new JButton("Clear all");
public TestPanel() {
Container cp = this.getContentPane();
cp.add(panel,BorderLayout.CENTER);
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
Graphics2D g2 = (Graphics2D) panel.getGraphics();
g2.drawRect(20,20,100,300);
panel.repaint();
}
});
button2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
panel.getGraphics().clearRect(0,0,700,400);
}
});
pnlCtl.add(button);
pnlCtl.add(button2);
cp.add(pnlCtl,BorderLayout.NORTH);
setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String[] args) {
TestPanel testPanel = new TestPanel();
}
}
class TTT extends JPanel
{
public TTT()
{
super();
}
public void paintComponent(Graphics g)
{
JOptionPane.showConfirmDialog(null,"paintComponent() called");
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(10,10,300,300);
}
//如果要试验没有paint()的情况注释掉下面的方法
public void paint(Graphics g)
{
JOptionPane.showConfirmDialog(null,"paint() called");
Graphics2D g2 = (Graphics2D) g;
g2.drawRect(20,20,300,300);
}
}
解决方案 »
- 静态内部类的问题
- Date类下的valueOf方法怎样使用??
- 如何用PushbackReader来实现重新读一串字符?
- Java网络关闭连接的资源清理问题
- java高手:这是必杀得帖子???来看
- 关于如何处理时间的问题
- 那位大侠知道java编程指南(1.4)的电子书籍下载地址啊??????
- 怎样才能在系统中使用sun.jdbc.rowset.CachedRowSet类!
- 求java 从入门到精通 (第三版) 光盘的下载地址!灰常感谢...
- SOS:JAVA 中对树进行操作的问题---谈谈想法也可以了。!
- 请问一下,VECTOR\ENUMERATLN\HASHTABLE的区别是什么?
- 菜问:关于 File(String pathname)中的pathname
{
private int i = 10;
public void display()
{
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String args[])
{
Test t = new Test();
t.display();
Thread thread = new Thread(t);
thread.start();
}
public void run()
{
while(true)
{
try
{
Thread.currentThread().sleep(1000);
}
catch (Exception ex)
{
System.out.println (ex.toString());
}
System.out.println ("excute");
this.repaint();
}
}
/****************************************************************************/
public void paintComponent(java.awt.Graphics g)//这儿用paint方法就没有问题,用paintComponent就不行,为什么?
{
System.out.println (i);
g.drawString("i am test",10,i);
i= i +10;
}
}
public class TestFrame extends JFrame
{
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.drawString("aaaaaa", 50, 50);
}
public static void main(String[] args)
{
JFrame f = new TestFrame();
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setSize(640, 480);
f.show();
}
}