| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 650 人关注过本帖
标题:用鼠标在面板上拖拽,画不同颜色的线条
取消只看楼主 加入收藏
爱好者
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-8-15
收藏
 问题点数:0 回复次数:0 
用鼠标在面板上拖拽,画不同颜色的线条

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;

public class Myframe1 extends JFrame { Mypanel1 p1; Mypanel2 p2; public Color c=Color.white; public Myframe1(String str) { super(str); p1=new Mypanel1(c,p2); p2=new Mypanel2(c); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(p1,BorderLayout.NORTH); this.getContentPane().add(p2,BorderLayout.CENTER); setSize(600,500); setVisible(true); } public static void main(String args[]) { new Myframe1("画图"); } } class Mypanel1 extends JPanel implements ActionListener { JButton b1,b2,b3,b4,b5; public Color c; Mypanel2 p2; public Mypanel1(Color c,Mypanel2 p2) { this.c=c; this.p2=p2; b1=new JButton("红色"); b2=new JButton("蓝色"); b3=new JButton("绿色"); b4=new JButton("橡皮"); b5=new JButton("清除"); this.add(b1); this.add(b2); this.add(b3); this.add(b4); this.add(b5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { c=Color.red; } else if(e.getSource()==b2) { c=Color.blue; } else if(e.getSource()==b3) { c=Color.green; } else if(e.getSource()==b4) { c=Color.white; } else { p2.repaint(); } } } class Mypanel2 extends JPanel { public Color c; public Mypanel2(Color c) { this.c=c; this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g=getGraphics(); g.setColor(c);/*这里老报错*/ g.fillOval(e.getX(),e.getY(),3,3); } }); } public void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,getSize().width,getSize().height); } }

搜索更多相关主题的帖子: 鼠标 线条 面板 颜色 
2005-08-25 08:01
快速回复:用鼠标在面板上拖拽,画不同颜色的线条
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018208 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved