简体   繁体   English

如何避免同时工作两个JButton

[英]How to avoid to work two JButton at the same time

I'm writing a simple paint program with Java. 我正在用Java编写一个简单的绘画程序。 As all paint applications there are buttons for brushTool, sprayTool, sprayTool... This tools have their own class which extends to MouseAdapter. 作为所有绘画应用程序,都有brushTool,sprayTool,sprayTool ...按钮。此工具有自己的类,该类扩展到MouseAdapter。 They are working as they should. 他们正在按自己的方式工作。 However, the problem starts when I choose a tool after choose another tool, both buttons and their ActionListeners keep executing and they do what they are written for at the same time. 但是,当我在选择另一个工具之后又选择了一个工具时,问题就开始了,这两个按钮及其ActionListener都继续执行,并且它们同时执行编写的操作。 I mean if I choose lineTool(which draws straight line) with rectangleTool I hava a diagonal too. 我的意思是,如果我选择带有矩形工具的lineTool(绘制直线),我也有一个对角线。 here is example of my two button. 这是我的两个按钮的示例。 What I'm tring to do is stop the current action when I click another button. 我要做的是单击另一个按钮时停止当前操作。 Can you guys help me 你们能帮我吗

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);
            }
        });

You can't keep adding a MouseListener to the drawing area every time you click a button. 每次单击按钮时,您不能一直将MouseListener添加到绘图区域。

Instead you need to keep track of the current MouseListener. 相反,您需要跟踪当前的MouseListener。 Then when you click a button you need to: 然后,当您单击按钮时,您需要:

  1. remove the current MouseListener 删除当前的MouseListener
  2. add the new MouseListener 添加新的MouseListener

I would replace the button action listener for a set of Toggle Buttons in a group 我将用一组按钮中的一组切换按钮替换按钮动作侦听器

https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html

Then you move everything in a single mouse listener. 然后,您可以在单个鼠标侦听器中移动所有内容。

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;
      }
   }
}

You are currently binding the listeners to the mainDrawArea, not setting an action for each individual button. 当前,您正在将侦听器绑定到mainDrawArea,而不是为每个按钮设置操作。

Note that the codes you write within actionPerformed() for each button's actionListener is the action you want to trigger everytime that button is clicked. 请注意,您在actionPerformed()为每个按钮的actionListener编写的代码是您希望在每次单击该按钮时触发的动作。 You do not want to add a new listener to the mainDrawArea everytime we click the buttons. 您不想每次我们单击按钮时都向mainDrawArea添加新的侦听器。

You can a create a state for your current action, for example: 您可以为当前操作创建状态,例如:

brushBotton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            state = BRUSH;
        }
    });

lineBotton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            state = LINE;
        }
    });

state can be an integer and BRUSH and LINE are constant such as 0 and 1. state可以是整数,并且BRUSHLINE是常数,例如0和1。

Then in the listener (for the mainDrawArea), check the current state 然后在侦听器中(对于mainDrawArea),检查当前状态

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM