java 画矩形package testFrame;import java.awt.Color;import java.awt.Component;import java.awt.Frame;import java.awt.Graphics;import java.awt.Rectangle;public class FrameTest extends Frame {/*** @param args*/Rectangle rec;Frame f;public void paint(G

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/08 19:36:30
java画矩形packagetestFrame;importjava.awt.Color;importjava.awt.Component;importjava.awt.Frame;importjav

java 画矩形package testFrame;import java.awt.Color;import java.awt.Component;import java.awt.Frame;import java.awt.Graphics;import java.awt.Rectangle;public class FrameTest extends Frame {/*** @param args*/Rectangle rec;Frame f;public void paint(G
java 画矩形
package testFrame;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
public class FrameTest extends Frame {
/**
* @param args
*/
Rectangle rec;
Frame f;
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(100,100,30,30);
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
repaint();
}
FrameTest()
{
f=new Frame("title");
f.setLocation(100,100);
f.setSize(600,400);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameTest ft=new FrameTest();
}
}
为什么我的代码画不出矩形
我的一个朋友说他能画出~

java 画矩形package testFrame;import java.awt.Color;import java.awt.Component;import java.awt.Frame;import java.awt.Graphics;import java.awt.Rectangle;public class FrameTest extends Frame {/*** @param args*/Rectangle rec;Frame f;public void paint(G
你的代码有问题,你的类本身是frame,你也在类中绘制,但是你却没有显示,而是另外定义了一个frame来显示,你修改一下:
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
public class FrameTest extends Frame {
/**
* @param args
*/

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(100, 100, 30, 30);
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
//repaint();
}



FrameTest()
{
super("title");
setLocation(100,100);
setSize(600,400);
setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
FrameTest ft=new FrameTest();

}
}
这样应该没问题了.