下面这个程序的pg.setColor (Color.RED);
cvs.getGraphics().drawOval(20,20,100,100);
这两行程序无法通过编译,请问是什么原因?import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class MPan extends JFrame implements ActionListener {
JPanel list;
Canvas cvs;
JButton btn1, btn2;
Graphics cvsg, pg;
Image pic;
public MPan() {
list = new JPanel();
this.setLayout(new BorderLayout());
list.setSize(360, 230);
this.add(list, BorderLayout.NORTH);
cvs = new Canvas();
cvs.setBackground(Color.BLACK);
list.setLayout(new BorderLayout());
this.add(cvs, BorderLayout.CENTER);
btn1 = new JButton ("BB");
list.add(btn1,BorderLayout.NORTH);
btn1.addActionListener(this);
btn2 = new JButton ("AA");
list.add(btn2,BorderLayout.SOUTH);
cvs.setBackground(Color.BLACK);
pg = cvs.getGraphics();
pg.setColor (Color.RED);
cvs.getGraphics().drawOval(20,20,100,100);
this.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
System.out.println("HER");
//pg.drawOval(200,200,10,10);
//cvs.getGraphics().drawImage(pg,0,0,this);
}
}
public static void main(String args[]) {
MPan d= new MPan();
d.setSize(500,500);
d.setVisible(true);
}}
cvs.getGraphics().drawOval(20,20,100,100);
这两行程序无法通过编译,请问是什么原因?import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class MPan extends JFrame implements ActionListener {
JPanel list;
Canvas cvs;
JButton btn1, btn2;
Graphics cvsg, pg;
Image pic;
public MPan() {
list = new JPanel();
this.setLayout(new BorderLayout());
list.setSize(360, 230);
this.add(list, BorderLayout.NORTH);
cvs = new Canvas();
cvs.setBackground(Color.BLACK);
list.setLayout(new BorderLayout());
this.add(cvs, BorderLayout.CENTER);
btn1 = new JButton ("BB");
list.add(btn1,BorderLayout.NORTH);
btn1.addActionListener(this);
btn2 = new JButton ("AA");
list.add(btn2,BorderLayout.SOUTH);
cvs.setBackground(Color.BLACK);
pg = cvs.getGraphics();
pg.setColor (Color.RED);
cvs.getGraphics().drawOval(20,20,100,100);
this.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
System.out.println("HER");
//pg.drawOval(200,200,10,10);
//cvs.getGraphics().drawImage(pg,0,0,this);
}
}
public static void main(String args[]) {
MPan d= new MPan();
d.setSize(500,500);
d.setVisible(true);
}}
解决方案 »
- 求高手帮忙看一下。。
- 为什么我的get方法取不到值呢,大伙帮我看看呀!
- struts+hibernate+spring整合高手进
- Process p=Runtime.getRuntime().exec("");语句
- rt.jar ,dt.jar ,tool.jar都包含什么 ,分别什么时候需要设置到classpath里?
- 请问j2SE大概要学到什么程度才可以继续往下学西JSP,SERVLET,STRUTS之类的呢?
- 大家谁做过这样的程序,能给我一份吗
- 关于字符串的问题,在线等待!
- 一个解决tomcat4的中文问题,利用filter功能来实现,测试过了,utf8编码用此方法同样能解决,
- 这段代码看的不是很懂?
- 请教:cin.hasNext是什么意思?
- httpclient4 重试机制请教?
发现当我调用 cvs.getGraphics().drawImage(); 方法的时候 提示的传入的参数依次是Image、int、int、ImageObserver 第一个参数是Image 你这里传入的是pg 但是你在申明的时候pg是 Graphics 可能是这个问题吧我的意见仅供参考
关注、等大牛解释
Creates a graphics context for this component. This method will return null if this component is currently not displayable. 没有显示时返回值是null
Creates a graphics context for this component. This method will return null if this component is currently not displayable. 没有显示时返回值是null
==========
能够把这句话解释清楚吗?
以下是j2se API 中关于这个方法的解释
getGraphics
public Graphics getGraphics()
Creates a graphics context for this component. This method will return null if this component is currently not displayable. Returns:
a graphics context for this component, or null if it has none按字面意思上说就是你应该先用setvisible(true),
然后再调用getGraphics()这个方法,
否则会导致返回Null值。