十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你的代码有问题,你的类本身是frame,你也在类中绘制,但是你却没有显示,而是另外定义了一个frame来显示,你修改一下:
创新互联公司自2013年起,先为海口等服务建站,海口等地企业,进行企业商务咨询服务。为海口企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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();
}
}
这样应该没问题了。
直接说event是简单,不过总要试一试才敢拿上来讲,所以就全写上来了。。。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new PaintingPanel());
frame.setBounds(100, 100, 400, 400);
frame.setVisible(true);
}
}
class PaintingPanel extends JPanel {
ArrayListRectangle list;
Rectangle current;
public PaintingPanel() {
list = new ArrayListRectangle();
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
}
MouseInputListener mouseHandler = new MouseInputAdapter() {
Point startPoint;
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
current = new Rectangle();
}
public void mouseReleased(MouseEvent e) {
makeRectangle(startPoint, e.getPoint());
if (current.width 0 current.height 0) {
list.add(current);
current = null;
repaint();
}
}
public void mouseDragged(MouseEvent e) {
if (current != null) {
makeRectangle(startPoint, e.getPoint());
repaint();
}
}
private void makeRectangle(Point p1, Point p2) {
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int w = Math.abs(p1.x - p2.x);
int h = Math.abs(p1.y - p2.y);
current.setBounds(x, y, w, h);
}
};
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
for (Rectangle rect : list) {
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}
if (current != null) {
g.drawRect(current.x, current.y, current.width, current.height);
}
}
}
兄弟帮你写了一个:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}