先把这段说明看一下,你就明白了 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 下面是我把你的代码稍微改了一点,可以运行了,你Copy过去。import javax.swing.*; import java.awt.*; import java.awt.event.*;class MyCircle { private int x; private int y; private int width; private int height; private Graphics g; public MyCircle(int x, int y, int width, int height, Graphics g) { this.x = x; this.y = y; this.width = width; this.height = height; this.g=g; } public void draw() { g.drawOval(x, y, width, height); g.dispose(); } }class JMyPanel extends JPanel { public JMyPanel(Graphics g) { circle = new MyCircle(150, 100, 80, 80, g); circle.draw(); } private MyCircle circle; }class JMyFrame extends JFrame { public JMyFrame() { setTitle("CircleTest"); setSize(640, 480); this.show(); Graphics g=this.getGraphics(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = getContentPane(); JMyPanel panel = new JMyPanel(g); contentPane.add(panel); }}public class TestMain { public static void main(String[] args) { JFrame frame=new JMyFrame(); } }
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
下面是我把你的代码稍微改了一点,可以运行了,你Copy过去。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class MyCircle {
private int x;
private int y;
private int width;
private int height; private Graphics g; public MyCircle(int x, int y, int width, int height, Graphics g) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.g=g;
} public void draw() {
g.drawOval(x, y, width, height);
g.dispose();
}
}class JMyPanel extends JPanel {
public JMyPanel(Graphics g) {
circle = new MyCircle(150, 100, 80, 80, g);
circle.draw();
} private MyCircle circle;
}class JMyFrame extends JFrame {
public JMyFrame() {
setTitle("CircleTest");
setSize(640, 480);
this.show();
Graphics g=this.getGraphics();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane(); JMyPanel panel = new JMyPanel(g);
contentPane.add(panel); }}public class TestMain {
public static void main(String[] args)
{
JFrame frame=new JMyFrame(); }
}
我来帮你修改一下
比如private MyCircle circle;没见过
还有画圆根本就不用你写类,直接用
你对窗体属性了解也不怎样frame.show();改为frame.setVisable(true);
格式要规范。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class JMyFrame extends JFrame {
public JMyFrame() {
JPanel panel=new JPanel();
Container contentPane=getContentPane();
contentPane.add(panel); setTitle("CircleTest");
setSize(640, 480);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}}public class TestMain {
public static void main(String[] args)
{
JFrame frame=new JMyFrame();
frame.setVisable(true);
}
}
另外,to fruitking(许果),其实我知道画圆可以直接画的,呵呵,只是为了做个小试验,另外,frame.show()其实是我从CoreJava书上直接看来的。