使用java JDK提供的JFrame构建窗体框,使用JPanel充当画布,Graphics充当画笔,在窗体中布置内容。
创建窗体:
package drawRect; import javax.swing.JFrame; /** * 利用Graphics画矩形 * @author wuyan * */ public class Rect extends JFrame{ private static final long serialVersionUID = 1L; MyJPanel mJP = null; public Rect() { mJP = new MyJPanel(); this.add(mJP); this.setSize(600,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { Rect rect = new Rect(); } }
在窗体中填充内容:
package drawRect; import java.awt.Graphics; import javax.swing.JPanel; public class MyJPanel extends JPanel{ private static final long serialVersionUID = 1L; // 重写paint方法 public void paint(Graphics g) { super.paint(g); g.drawRect(100, 100, 400, 300); } }
至此,完成了一个简陋java窗体,并在里面画出一个矩形
原文:http://linuxpf.blog.51cto.com/11464235/1892707