给你一个能画圆的例子,运行就可以了.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Frame2 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
MyPanel jPanel2 = new MyPanel(); //Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame2.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
contentPane.add(jPanel2, BorderLayout.CENTER);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} public static void main(String [] args) {
Frame2 frame = new Frame2();
frame.validate(); frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); frame.setVisible(true);
}}class MyPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
update(g);
} public void update(Graphics g){ Graphics2D g2 = (Graphics2D)g;
g2.drawOval(100,100,50,50);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Frame2 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
MyPanel jPanel2 = new MyPanel(); //Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame2.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
contentPane.add(jPanel2, BorderLayout.CENTER);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} public static void main(String [] args) {
Frame2 frame = new Frame2();
frame.validate(); frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); frame.setVisible(true);
}}class MyPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
update(g);
} public void update(Graphics g){ Graphics2D g2 = (Graphics2D)g;
g2.drawOval(100,100,50,50);
}
}
public void init(Graphics g){}
java就能按你的要求调用了吗.
你的问题涉及到swing组件的调用次序,你直接取panel的graphics,
可能取不到,为null,而应该由panel的父组件初始化后,再它调用Panel的
update(Graphics g)方法.
import java.awt.*;
import java.applet.*;
import javax.swing.*;public class PanelDraw extends JApplet
{
JPanel jp=new JPanel();
public void init()
{
jp.setBackground(Color.gray);
getContentPane().add(jp);
Graphics g=jp.getGraphics();
//jp.paintComponents(g);
g.setColor(Color.blue);
g.drawOval(100,100,50,50);
}
}
这样怎么还是不对?我会不断加分的,:)
你的错误在于把画图的代码写在INIT方法中,INIT方法只是用来做APPLET初始工作的,比如添加COMPONENT等
import java.awt.*;
import java.applet.*;
import javax.swing.*;public class PanelDraw extends JApplet
{
JPanel jp=new JPanel();
public void init()
{
jp.setBackground(Color.gray);
getContentPane().add(jp);
//Graphics g=jp.getGraphics();
//jp.paintComponents(g);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawOval(100,100,50,50);
}
}
xioyoo(xioyoo)的方法,并没有调用panel的paint方法,而是applet的。
你要在panel中绘画,就一定要派生panel,在其中的update方法中(paint 好象也可以)作画,见我给的例子。
这样还不清楚吗?
偶只是针对帖主CFree (自由从哪派生?)的问题作答,偶根本没有,也没必要留意你发的帖。偶和你没任何干系,所以请不要指偶的名点偶的姓。听你的意思好象是你认为自己的答案已经非常之权威,如神灵不可亵渎,偶不该再在你后面画蛇添足,抢你的分数。偶告诉你,偶完全没有这意思。如急需分数,请说明,偶开帖给你
偶并非权威,只是经验之谈,偶答问题时刚好见了你的回答,只是补充了一下,
不知你很在意这个,实在是抱歉,sorry,very sorry.
我不在乎分数,我从来没见那个高才答的.都谢你的好意.