Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何避免同时使用两个按钮_Java_Swing_Actionlistener_Mouselistener - Fatal编程技术网

Java 如何避免同时使用两个按钮

Java 如何避免同时使用两个按钮,java,swing,actionlistener,mouselistener,Java,Swing,Actionlistener,Mouselistener,我正在用Java编写一个简单的绘图程序。与所有油漆应用程序一样,有用于笔刷工具、sprayTool、sprayTool的按钮。。。这些工具有自己的类,扩展到MouseAdapter。他们正在按他们应该的方式工作。然而,当我在选择另一个工具之后选择一个工具时,问题就开始了,按钮和它们的ActionListener都会继续执行,并且它们同时执行它们编写的任务。我的意思是,如果我选择直线工具(画直线)和矩形工具,我也有一个对角线。这是我的两个按钮的例子。我要做的是在单击另一个按钮时停止当前操作。你们能

我正在用Java编写一个简单的绘图程序。与所有油漆应用程序一样,有用于笔刷工具、sprayTool、sprayTool的按钮。。。这些工具有自己的类,扩展到MouseAdapter。他们正在按他们应该的方式工作。然而,当我在选择另一个工具之后选择一个工具时,问题就开始了,按钮和它们的ActionListener都会继续执行,并且它们同时执行它们编写的任务。我的意思是,如果我选择直线工具(画直线)和矩形工具,我也有一个对角线。这是我的两个按钮的例子。我要做的是在单击另一个按钮时停止当前操作。你们能帮帮我吗

brushBotton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            pen = new PenTool(mainDrawArea);
            mainDrawArea.addMouseListener(pen);
            mainDrawArea.addMouseMotionListener(pen);
            }
    });

rectangleButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
                shapeToolbar.setVisible(false);
                rect = new RectangleTool(mainDrawArea);
                rect.setStrokeSize(strokeInt);
                mainDrawArea.addMouseListener(rect);
                mainDrawArea.addMouseMotionListener(rect);
            }
        });

不能每次单击按钮时都将鼠标侦听器添加到绘图区域

相反,您需要跟踪当前的鼠标侦听器。然后,单击按钮时,您需要:

  • 移除当前的鼠标侦听器
  • 添加新的鼠标侦听器

  • 我会将按钮操作侦听器替换为组中的一组切换按钮

    然后在一个鼠标侦听器中移动所有内容

    public void mousePressed(MouseEvent e) {
       this.drawingState = !this.drawingState
       if ( isRightCLick(e) ) resetAllPendingOperation();
       if (drawingState) {
          this.startPoint = getPointFromEvent(e);
          switch(toolbarGetCurrentTool()) {
              case "line":
                registerMouseLineListener(startPoint);//here you draw live preview
              break
              case "rectangle":
                registerMouseRectangleListener(startPoint); //here you draw live preview
              break;
          }
       } else {
         //user clicked the second time, commit changes
         //same switch as above
          this.endPoint = getPointFromEvent(e);
                    switch(toolbarGetCurrentTool()) {
              case "line":
                commitLine(startPoint, endpoint);//here you draw live preview
              break
              case "rectangle":
                commitRectangle(startPoint, endpoint); //here you draw live preview
              break;
          }
       }
    }
    

    您当前正在将侦听器绑定到mainDrawArea,而不是为每个按钮设置操作

    请注意,您在
    actionPerformed()
    中为每个按钮的actionListener编写的代码是您希望在每次单击该按钮时触发的操作。您不希望每次单击按钮时都向mainDrawArea添加新的侦听器

    您可以为当前操作创建状态,例如:

    brushBotton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                state = BRUSH;
            }
        });
    
    lineBotton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                state = LINE;
            }
        });
    
    状态可以是整数,
    BRUSH
    LINE
    是常量,例如0和1

    然后在侦听器中(对于mainDrawArea),检查当前状态

    switch (state){
        case BRUSH: //trigger action needed for brushing;
                   break;
        case LINE: //trigger action needed for drawing line;
                   break;
    }
    

    看看罗曼的回答。