一道作业题,要求能画直线、矩形、圆等。一开始做就遇到问题了。我是加入两个面板,一个(ButtonArea)用来放按钮,一个(DrawArea,自己通过extends JPanel做的)用来作画图区。不知道是哪错了,画图区就是显示不出来,不明白啊~~~救命!!!希望各位帮帮忙啦,以下是代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Draw extends JFrame implements ActionListener{
private JButton jbtn1=new JButton("自由画图");
private JButton jbtn2=new JButton("直线画图");
public int choose=2;
JPanel ButtonArea;
DrawJPanel DrawArea;
public Draw(){ 
setTitle("Draw");
Container c=getContentPane();
c.setBackground(Color.blue);

ButtonArea = new JPanel();
ButtonArea.setLayout(new FlowLayout());
ButtonArea.add(jbtn1);
ButtonArea.add(jbtn2);
c.add(ButtonArea,BorderLayout.NORTH);

DrawArea = new DrawJPanel();
c.add(DrawArea,BorderLayout.CENTER); jbtn1.addActionListener(this);
jbtn2.addActionListener(this);
setSize(600,400);
setLocation(100,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtn1){
choose=1;
}
else if(e.getSource()==jbtn2){
choose=2;
}
}
public static void main(String[] args){
Draw d=new Draw();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.show();
}
class DrawJPanel extends JPanel implements MouseListener,MouseMotionListener{
private Point start=new Point();
private Point end=new Point();
public void mousePressed(MouseEvent e){
start.x=e.getX();
start.y=e.getY();
  }
  public void mouseReleased(MouseEvent e){
end.x=e.getX();
end.y=e.getY();
}
public void mouseExited(MouseEvent e){  
  }
  public void mouseClicked(MouseEvent e){   
  }
  public void mouseEntered(MouseEvent e){    
  }
public void mouseDragged(MouseEvent e){
end.x=e.getX();
end.y=e.getY();
}
public void mouseMoved(MouseEvent e){
}
  public void paintComponent(Graphics g){
    if(choose==1){
    }
    else if(choose==2){
     g.drawLine(start.x,start.y,end.x,end.y);
    }
  }
}
}

解决方案 »

  1.   

    DrawArea中没有加事件监听
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
      

  2.   

    我把你给的两句加入到Draw()里面(是加到这里吧),但是编译错误了,内容是:Draw.java:26: 无法将 java.awt.Component 中的 addMouseListener(java.awt.event.Mou
    seListener) 应用于 (Draw)
                    addMouseListener(this);
                    ^
    Draw.java:27: 无法将 java.awt.Component 中的 addMouseMotionListener(java.awt.eve
    nt.MouseMotionListener) 应用于 (Draw)
        addMouseMotionListener(this);
        ^
    注意:Draw.java 使用或覆盖了已过时的 API。
    注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
    2 错误后来我改用了swing中的MouseInputListener,但是就没有addMouseInputListener这个了。
    另外,那个不是没有把DrawArea那个面板加进去吗?如果加进出的话应该不会显示蓝色背景吧。
      

  3.   

    ...................你懂上课真认真听了吗????我都说DrawArea这个对象根本就没加键盘监听
    那当然是在DrawJPanel加
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
    呃~无语
      

  4.   

    (T_T)不好意思~~其实我就是没有怎样认真听过课啦。所以才.......加到DrawJPanel里我也有试过,不过编译还是不过啊。好人做到底,能帮我把代码改好吗?先谢过了~~
      

  5.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class Draw extends JFrame implements ActionListener
    {
        private JButton jbtn1  = new JButton("自由画图");    private JButton jbtn2  = new JButton("直线画图");    public int      choose = 2;    JPanel          ButtonArea;    DrawJPanel      DrawArea;    public Draw()
        {
            setTitle("Draw");
            Container c = getContentPane();
    //        c.setBackground(Color.blue);        ButtonArea = new JPanel();
            ButtonArea.setLayout(new FlowLayout());
            ButtonArea.add(jbtn1);
            ButtonArea.add(jbtn2);
            c.add(ButtonArea, BorderLayout.NORTH);        DrawArea = new DrawJPanel();
            c.add(DrawArea, BorderLayout.CENTER);        jbtn1.addActionListener(this);
            jbtn2.addActionListener(this);
            setSize(600, 400);
            setLocation(100, 100);
            setVisible(true);
        }    public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == jbtn1)
            {
                choose = 1;
            }
            else if (e.getSource() == jbtn2)
            {
                choose = 2;
            }
        }    public static void main(String[] args)
        {
            Draw d = new Draw();
            d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            d.show();
        }    class DrawJPanel extends JPanel implements MouseListener,
                MouseMotionListener
        {
            private Point start = new Point();        private Point end   = new Point();        DrawJPanel()
            {
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
            }
            
            public void mousePressed(MouseEvent e)
            {
                start.x = e.getX();
                start.y = e.getY();
            }        public void mouseReleased(MouseEvent e)
            {
                end.x = e.getX();
                end.y = e.getY();
            }        public void mouseExited(MouseEvent e)
            {
            }        public void mouseClicked(MouseEvent e)
            {
            }        public void mouseEntered(MouseEvent e)
            {
            }        public void mouseDragged(MouseEvent e)
            {
                end.x = e.getX();
                end.y = e.getY();
                repaint();
            }        public void mouseMoved(MouseEvent e)
            {
            }        public void paintComponent(Graphics g)
            {
                if (choose == 1)
                {
                }
                else if (choose == 2)
                {
                    g.setColor(this.getBackground());
                    g.fillRect(0, 0, this.getWidth(), this.getHeight());
                    g.setColor(Color.BLACK);
                    g.drawLine(start.x, start.y, end.x, end.y);
                }
            }
        }
    }
      

  6.   

    paintComponent()
    中加的几句是为了刷新的问题
      

  7.   

    谢谢你,原来这样啊。是不是这些addActionListener、addMouseListener之类的都要放到构造函数里才行的啊?
    还有那个如果用了implement MouseInputListener代替了implement MouseListener,MouseMotionListener。那个 
               this.addMouseListener(this);
               this.addMouseMotionListener(this);
    不用改,但是可以用一句话代替的吗?JAVA API中没有addMouseInputListener。