这个程序有点问题:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class J_Draw extends JFrame implements ActionListener
{
int x=0,y=0;//当前坐标
int startX=0,startY=0;//开始坐标
int width=0,height=0;//初始化宽度与高度为零
Button jb1,jb2,jb3;
int J_switch;//选择画图类型控制变量
public J_Draw()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout(0));
jb1=new Button("画自由线");
jb2=new Button("画矩形");
jb3=new Button("画椭圆");
jb1.addActionListener(this);//注册监听器
jb2.addActionListener(this);
jb3.addActionListener(this);
cp.add(jb1);//添加按钮到容器
cp.add(jb2);
cp.add(jb3);
setBackground(Color.white);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)//按钮事件
{
if(e.getSource()==jb1)//判断按钮所触发的事件为画自由线
{
J_switch=0;
FreeDraw();//调用自由画线方法
}
if(e.getSource()==jb2)
{
J_switch=1;
Rect_OvalDraw();
}
if(e.getSource()==jb3)
{
J_switch=2;
Rect_OvalDraw();
}
}
public void FreeDraw()/////////////////////////////////自由画线动作函数
{
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
startX=x;
startY=y;
repaint();
}
}
);//结束
addMouseMotionListener(new MouseMotionAdapter()//定义鼠标内部类
{
public void mouseDragged(MouseEvent e)
{
startX=x;
startY=y;
x=e.getX();
y=e.getY();
repaint();
}
}
);//结束
}
public void Rect_OvalDraw()////////////////////////////////画矩形与椭圆动作函数
{
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mousePressed(MouseEvent e)
{
startX=e.getX();
startY=e.getY();
}
}
);//内部类结束
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mouseReleased(MouseEvent e)
{
width=e.getX()-startX;
height=e.getY()-startY;
repaint();
}
}
);//结束
}
public void paint(Graphics g)/////////实现画图功能
{
if(J_switch==0)
g.drawLine(startX,startY,x,y);
else if(J_switch==1)
g.drawRect(startX,startY,width,height);
// g.clearRect(startX+1,startY+1,width-1,height-1);
else if(J_switch==2)
g.drawOval(startX,startY,width,height);
}
public static void main(String[] args)
{
J_Draw app=new J_Draw();
Container cp=app.getContentPane();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
----------------解决方案--------------------------------------------------------
有什么问题
好像我以前写过一个类似的程序,也是可以选择画什么的
----------------解决方案--------------------------------------------------------
你运行一下试一下能不能帮我改一下,谢谢千里兄
----------------解决方案--------------------------------------------------------
好的
----------------解决方案--------------------------------------------------------
原因是你定义了太多的MouseListener,
每加一下按钮,你都加入了几个MouseListener, 这样当然不好
你应该只加入一次,然后根据你点的按钮的不同做不同的绘图操作
而不是在那里一直加入 MouseListener
具体的例子,我这里好像有
----------------解决方案--------------------------------------------------------
[CODE]/*
* TestDraw.java
*
* Created on 2006年11月29日, 上午9:35
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author lbf
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestDraw extends JFrame implements ActionListener{
private JRadioButton jr1,jr2,jr3,jr4;
private int drawWhat=Const.DRAW_LINE;
private int startX,startY,endX,endY;
/** Creates a new instance of TestDraw */
public TestDraw() {
initWindow();
}
private void initWindow(){
JPanel jrPanel=new JPanel();
DrawPanel dp=new DrawPanel();
jrPanel.add(jr1=new JRadioButton("任意线段",true));
jrPanel.add(jr2=new JRadioButton("画直线"));
jrPanel.add(jr3=new JRadioButton("画椭圆"));
jrPanel.add(jr4=new JRadioButton("画矩形"));
ButtonGroup bg=new ButtonGroup();
bg.add(jr1);
bg.add(jr2);
bg.add(jr3);
bg.add(jr4);
jr1.addActionListener(this);
jr2.addActionListener(this);
jr3.addActionListener(this);
jr4.addActionListener(this);
dp.setBorder(BorderFactory.createTitledBorder("画图区"));
jrPanel.setBorder(BorderFactory.createTitledBorder("选择区"));
this.getContentPane().add(jrPanel,BorderLayout.SOUTH);
this.getContentPane().add(dp,BorderLayout.CENTER);
this.setSize(400,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==jr1){
drawWhat=Const.DRAW_LINE;
} else if(ae.getSource()==jr2){
drawWhat=Const.DRAW_LINE_TO;
}else if(ae.getSource()==jr3){
drawWhat=Const.DRAW_OVAL;
}else if(ae.getSource()==jr4){
drawWhat=Const.DRAW_RECT;
}
}
private class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{
public DrawPanel(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me){
}
public void mouseExited(MouseEvent me){
}
public void mousePressed(MouseEvent me){
startX=me.getX();
startY=me.getY();
endX=me.getX();
endY=me.getY();
}
public void mouseClicked(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
endX=me.getX();
endY=me.getY();
repaint();
}
public void mouseMoved(MouseEvent me){
}
public void mouseDragged(MouseEvent me){
if(drawWhat==Const.DRAW_LINE){
startX=endX;
startY=endY;
}
endX=me.getX();
endY=me.getY();
repaint();
}
public void paintComponent(Graphics g){
if(drawWhat==Const.DRAW_LINE){
g.drawLine(startX,startY,endX,endY);
} else if(drawWhat==Const.DRAW_OVAL){
super.paintComponent(g);
g.drawOval(startX,startY,endX-startX,endY-startY);
}else if(drawWhat==Const.DRAW_RECT){
super.paintComponent(g);
g.drawRect(startX,startY,endX-startX,endY-startY);
}else if(drawWhat==Const.DRAW_LINE_TO){
super.paintComponent(g);
g.drawLine(startX,startY,endX,endY);
}
}
}
protected static class Const{
public static final int DRAW_LINE=1;
public static final int DRAW_OVAL=2;
public static final int DRAW_RECT=3;
public static final int DRAW_LINE_TO=4;
}
public static void main(String[] args) {
new TestDraw();
}
}
interface MyShape{
void drawSelf(Graphics g);
}
class MyLine implements MyShape{
private int startX,startY,endX,endY;
public MyLine(int startX,int startY,int endX,int endY){
this.startX=startX;
this.startY=startY;
this.endX=endX;
this.endY=endY;
}
public void drawSelf(Graphics g){
g.drawLine(startX,startY,endX,endY);
}
}
class MyOval implements MyShape{
private int startX,startY,width,height;
public void drawSelf(Graphics g){
g.drawOval(startX,startY,width,height);
}
}[/CODE]
----------------解决方案--------------------------------------------------------
谢啦,我看一下先
----------------解决方案--------------------------------------------------------
我在我的容器里加了JPanel容器,但是显示不了,是啥回事啊
----------------解决方案--------------------------------------------------------
千里兄,能否在这个程序里帮我修改修改,我研究了好久,实在是不知道怎么改,谢谢
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class J_Draw extends JFrame implements ActionListener
{
int x=0,y=0;//当前坐标
int startX=0,startY=0;//开始坐标
int width=0,height=0;//初始化宽度与高度为零
Button jb1,jb2,jb3;
int J_switch;//选择画图类型控制变量
public J_Draw()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout(0));
jb1=new Button("画自由线");
jb2=new Button("画矩形");
jb3=new Button("画椭圆");
jb1.addActionListener(this);//注册监听器
jb2.addActionListener(this);
jb3.addActionListener(this);
cp.add(jb1);//添加按钮到容器
cp.add(jb2);
cp.add(jb3);
setBackground(Color.white);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)//按钮事件
{
if(e.getSource()==jb1)//判断按钮所触发的事件为画自由线
{
J_switch=0;
FreeDraw();//调用自由画线方法
}
if(e.getSource()==jb2)
{
J_switch=1;
Rect_OvalDraw();
}
if(e.getSource()==jb3)
{
J_switch=2;
Rect_OvalDraw();
}
}
public void FreeDraw()/////////////////////////////////自由画线动作函数
{
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
startX=x;
startY=y;
repaint();
}
}
);//结束
addMouseMotionListener(new MouseMotionAdapter()//定义鼠标内部类
{
public void mouseDragged(MouseEvent e)
{
startX=x;
startY=y;
x=e.getX();
y=e.getY();
repaint();
}
}
);//结束
}
public void Rect_OvalDraw()////////////////////////////////画矩形与椭圆动作函数
{
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mousePressed(MouseEvent e)
{
startX=e.getX();
startY=e.getY();
}
}
);//内部类结束
addMouseListener(new MouseAdapter()//定义鼠标内部类
{
public void mouseReleased(MouseEvent e)
{
width=e.getX()-startX;
height=e.getY()-startY;
repaint();
}
}
);//结束
}
public void paint(Graphics g)/////////实现画图功能
{
switch(J_switch)
{
case 0:
g.drawLine(startX,startY,x,y);
break;
case 1:
g.drawRect(startX,startY,width,height);
break;
case 2:
g.drawOval(startX,startY,width,height);
break;
}
}
public static void main(String[] args)
{
J_Draw app=new J_Draw();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
----------------解决方案--------------------------------------------------------
你的程序要改的话,只有重写
因为你每次点按钮事件的时间都加入了鼠标监听事件.这样监听事件会越来越多
你如果想用变换鼠标监听的方法来实现的话,你可以把你画圆,画线,圆矩形等的监听器写成几个有名字的内部类,然后在点按钮的时候分别使用addMouseListener或者 addMouseMotionListener,然后再去掉以前添加进去的监听器
removeMouseListener()和removeMouseListener()来清除以前添加进去的监听器
----------------解决方案--------------------------------------------------------