在java中操作GUI对象

在java中操作GUI对象,java,swing,awt,jbutton,Java,Swing,Awt,Jbutton,考虑以下代码 import edu.cmu.ri.createlab.terk.robot.finch.Finch; import javax.swing.JFrame; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class RobotControl extends JFrame { pub

考虑以下代码

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RobotControl extends JFrame {
   public static void main (String args[])  {

    RobotControl GUI = new RobotControl();
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(500,500);
    GUI.setVisible(true);
    GUI.setTitle("RobotControl");
}

//The following are declarations of object variables.
    private Finch myf;
    private JButton front;
    private JButton back;
    private JButton left;


public RobotControl() { 
    myf = new Finch();
    setLayout (new FlowLayout());


    front = new JButton("front");
    add(front);
    front.addActionListener(new FrontButtonListener(myf));
    back = new JButton("back");
    add(back);
    back.addActionListener(new BackButtonListener(myf));
    left = new JButton("left");
    add(left);
    left.addActionListener(new LeftButtonListener(myf));

    }
public class FrontButtonListener implements ActionListener {
    public FrontButtonListener(Finch myf) {
        // TODO Auto-generated constructor stub
    }
                public void actionPerformed(ActionEvent arg0) {
                    myf.setWheelVelocities(100,100,10000);
                }
    }
public class BackButtonListener implements ActionListener{
    public BackButtonListener(Finch myf){   
    }
                public void actionPerformed(ActionEvent arg0) {
                    myf.setWheelVelocities(-100,-100,10000);
                }
    }   
public class LeftButtonListener implements ActionListener{
    public LeftButtonListener(Finch myf){
    }
                public void actionPerformed(ActionEvent arg0){
                    myf.setWheelVelocities(0, 200, 1000);
                }   

现在,上面的代码将创建一个GUI,有三个按钮,前、后和左。我需要一些关于如何让程序在运行前等待所有三个按钮被点击的建议,而不是一次点击一个按钮

跟踪已经点击的数量(使用
int
)。为此,仅当单击了尚未单击的按钮时,才增加
int
(您可以使用
布尔值来跟踪此操作)。当
int
等于3时,无论单击按钮的顺序如何,调用一个外部方法来执行当前在3个
actionPerformed
方法中执行的操作。左按钮类似于此,其他按钮类似:

    public void actionPerformed(ActionEvent arg0) {
        if(!leftClicked) {
            leftClicked = true;
            numButtonsClicked++;
        }
        countLeftButtonClicks++;
        if(numButtonsClicked == 3) {
            newMethodThatWritesToLogFileToo();
        }
    }

为每个
按钮
创建一个
布尔
变量,并在单击相应按钮时将其启用为true


注意:您需要将这些布尔变量设置为类级别

使用
JToggleButton
JCheckBox
来保留每个按钮的状态。假设一个名为
List
List
,您可以计算一个
allTrue
谓词,如下所示:

boolean allTrue = true;
for (JToggleButton b : list) {
    allTrue &= b.getSelected;
}

仅当
allTrue
true
时才启用所需功能。看到了一个相关示例。

如果我想让一个按钮再点击一次该怎么办?是否要跟踪每个按钮被点击的次数,并最终在所有3个按钮都被点击时执行该按钮的操作多次?我想在所有三个按钮都被点击后执行操作,而不管顺序如何,或者按钮被点击的次数,最后向txt文件发送一个日志,显示已执行的命令或按钮。这种方法是可行的,并且会起作用。如果您单击同一按钮3次,操作的执行(可能会在您将创建的额外方法中发生)将不会发生,因为您不会为该按钮多次递增
int
计数器。如果您还想将每个按钮的点击次数发送到日志文件,请同时记录每个按钮的点击次数。如果我多次点击同一按钮,此方法无效,您建议我如何开始?
boolean allTrue = true;
for (JToggleButton b : list) {
    allTrue &= b.getSelected;
}