【问题标题】:Manipulating GUI objects in java在java中操作GUI对象
【发布时间】:2014-02-07 19:31:12
【问题描述】:

考虑下面的代码。

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,其中包含三个按钮,前、后和左。我需要一些关于如何让程序在运行之前等待所有三个按钮都被单击的建议,而不是一次单击一个按钮。

【问题讨论】:

    标签: java swing awt jbutton


    【解决方案1】:

    为每个button 创建一个boolean 变量,并在单击其对应按钮时将它们设为真

    private  boolean  firstClicked = false;
    private  boolean  secondClicked = false;
    private  boolean  thirdClicked = false;
    ......
    ......
    //set these boolean values in their onClick actionPerfomed method
    if(firstClicked && secondClicked && thirdClicked){
    //do whatever operations you want after three buttons have been clicked
    }
    

    注意:您需要在类级别拥有这些布尔变量

    【讨论】:

      【解决方案2】:

      使用JToggleButtonJCheckBox 保留每个按钮的状态。假设List<JToggleButton> 名为list,您可以计算allTrue 谓词如下:

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

      仅当allTruetrue 时才启用所需功能。相关示例见here

      【讨论】:

        【解决方案3】:

        跟踪已被点击的数量(使用int)。为此,仅在单击尚未单击的按钮时才增加int(您可以使用boolean 跟踪此情况)。当 int 等于 3 时,无论单击按钮的顺序如何,都调用一个外部方法来执行当前在 3 个 actionPerformed 方法中的操作。左键是这样的,其他的也是这样:

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

        【讨论】:

        • 如果我想让一个按钮点击更多棕褐色一次怎么办?
        • 您是否要跟踪每个按钮被点击了多少次,并在所有 3 个按钮都被点击后最终执行该按钮的操作多次?
        • 我想执行操作,在所有三个按钮都被点击之后,不管顺序,或者按钮被点击的次数,最后发送一个日志到一个显示命令的 txt 文件或已执行的按钮。
        • 这种方法没问题,而且会奏效。如果您单击同一按钮 3 次,则可能在您将创建的额外方法中执行的操作不会发生,因为您不会为该按钮多次增加 int 计数器。如果您还想将每个按钮的点击次数发送到日志文件,只需跟踪每个按钮的点击次数即可。
        • 如果我多次单击同一个按钮,此方法不起作用,您建议我如何开始?
        猜你喜欢
        • 2012-12-29
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2011-01-14
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        相关资源
        最近更新 更多