java中关于paint()方法的问题

hellen_99010 2010-03-31 07:47:52
能说下我的paint()方法为什么没有被执行嘛,最好能详细点。paint()方法好像是自动被调用的。我写代码画不出东西来。帮调试下,说明下原因。非常感谢!!!


import java.awt.*;
import java.awt.event.*;

public class TestGraphics1 extends Frame{
Frame f;
Button b1;
Button b2;

TestGraphics1() {
b1 = new Button("Draw Line");
b2 = new Button("Draw circle");
f = new Frame("Draw Graphics");
f.setBounds(300,300,500,400);
f.setVisible(true);
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.addWindowListener(new Monitor());
}

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.drawLine(10,10,20,30);
g.setColor(c);
}



class Monitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}


public static void main(String []args) {
TestGraphics1 tg = new TestGraphics1();
}
}
...全文
271 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
旷逍遥 2012-04-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
借贵地一用,本人还不能发帖,谢谢

初学java,麻烦各位看下代码,问题出在哪里,实在是找不出来...图就是出不来~

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class test {

public test() {
}
public static cla……
[/Quote]


drawcanvas dc = new drawcanvas(240, 40);
你把这个 X Y 改小点,他的左上角已经不在 窗口中了.你肯定看不到了啊
SeaHome81 2012-04-01
  • 打赏
  • 举报
回复
借贵地一用,本人还不能发帖,谢谢

初学java,麻烦各位看下代码,问题出在哪里,实在是找不出来...图就是出不来~

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class test {

public test() {
}
public static class drawcanvas extends Canvas{
int x,y;
public drawcanvas(int x,int y){

this.x=x;
this.y=y;
}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawRect(x,y,10,10);
g.fillRect(x,y,10,10);
}

}

public static void main (String[] args) {
JFrame app = new JFrame();
drawcanvas dc = new drawcanvas(640,400);
app.add(dc);
app.setSize(600,600);
app.setLocation(340,100);
app.setVisible(true);
}


}

hellen_99010 2010-04-01
  • 打赏
  • 举报
回复
非常感谢楼上的,还能够对paint()这方法的原理说得详细点嘛,比如它的执行调用过情!
tcm441224375 2010-04-01
  • 打赏
  • 举报
回复
学习,飘过
wd9053 2010-04-01
  • 打赏
  • 举报
回复
TestGraphics1中的paint()方法不是f的paint()方法,另外,你画的线会与标题栏重合绝大部分
import java.awt.*;
import java.awt.event.*;

public class TestGraphics1 extends Frame{
Frame f;
Button b1;
Button b2;

TestGraphics1() {
super("Draw Graphics");
b1 = new Button("Draw Line");
b2 = new Button("Draw circle");
setBounds(300,300,500,400);
setLayout(new FlowLayout());
add(b1);
add(b2);

addWindowListener(new Monitor());
setVisible(true);

//下面的paint()方法是TestGraphics1.this的paint方法,而不是f的
/*f = new Frame("Draw Graphics");
f.setBounds(300,300,500,400);
f.setVisible(true);
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.addWindowListener(new Monitor());*/
}

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.drawLine(100,100,20,30);
//g.drawLine(10,10,20,30);坐标与标题栏重合了,如果仔细看的话,还是能在左上方看到一个红点的。。。
g.setColor(c);
}



class Monitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}


public static void main(String []args) {
TestGraphics1 tg = new TestGraphics1();
}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧