正文

Java画布画圆2007-12-14 22:07:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tld5yj/31425.html

分享到:

这个程序很简单,没有什么好说的了,希望大家多多指教一下,在JDK1.6下通过。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Draw extends WindowAdapter implements ActionListener,WindowListener
{Canvas c;TextField radius,x,y;Button b;
 public static void main(String args[])
 {Draw d=new Draw();
  Frame f=new Frame("画布画圆");
  d.c=new Canvas();
  d.x=new TextField(5);
  d.y=new TextField(5);
  d.radius=new TextField(5);
  d.b=new Button("清空画布");
  Label x1=new Label("请输入左上角x:");
  Label y1=new Label("请输入左上角y:");
  Label radius1=new Label("请输入半径:");
  Panel p=new Panel();
  f.setLayout(new BorderLayout());
  p.setLayout(new FlowLayout());
  f.add("Center",d.c);
  f.add("South",p);
  p.add(radius1);p.add(d.radius);p.add(x1);p.add(d.x);
  p.add(y1);p.add(d.y);p.add(d.b);
  d.x.addActionListener(d);
  d.y.addActionListener(d);
  d.radius.addActionListener(d);
  d.b.addActionListener(d);
  f.addWindowListener(d);
  f.setSize(600,400);
  f.setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==b)
  {c.repaint();
   x.setText("");y.setText("");radius.setText("");
   radius.requestFocusInWindow();
  }
  else
  {if(!(radius.getText()==""||x.getText()==""||y.getText()==""))
   {c.getGraphics().drawOval(Integer.parseInt(x.getText()),Integer.parseInt(y.getText()),2*Integer.parseInt(radius.getText()),2*Integer.parseInt(radius.getText()));
   }
  }
 }
 public void windowClosing(WindowEvent e)
 {System.exit(0);
 }
}

阅读(6417) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册