1

我目前正在开发我的一个程序,用于作为程序员的个人发展,我在我正在处理的这个鼠标点击事件中遇到了挫折的微型砖墙。当我在脑海中和白板上想出这个过程时,我看到它使用 switch 语句来完成工作。我的这个计划没有成功。从那以后,我尝试了其他一些控制结构,但似乎没有任何效果,问题在于控制 mouseClicked 事件中的控制结构。我将提供一些相关的示例代码来尝试传达我的目标。请注意,我知道提供的代码很糟糕。我只是想传达一个想法。目标是让盒子开始是绿色的,然后用户可以点击盒子把它变成红色,再次点击它变成白色,最后一次它又回到了绿色。出于某种原因,目前这超出了我的范围。任何和所有的帮助将不胜感激。提前致谢!

        //This component allows the user to store information on current unit identifier. Maybe necessary to pass this in as an arguement to the PssGui since there is no accounting for callsigns. 
    //This component also needs to be updated with a mouse click event that can turn the color of the box to reflect the tooltip text.
    unitId = new JTextField();
    unitId.setEditable(false);
    unitId.setBackground(Color.GREEN);
    unitId.setHorizontalAlignment(SwingConstants.CENTER);
    unitId.setToolTipText("<html>SHADE CELLS TO REFLECT CURRENT UNIT STATUS:" + "<br/>GREEN-MC" + "<br/>RED-NMC" + "<br/>WHITE-UNIT IN TRANSISTION</html>");
    unitId.setText("A 4/5");
    unitId.setBounds(0, 116, 79, 172);
    getContentPane().add(unitId);
    unitId.setColumns(10);
    unitId.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            //Fill with sweet code to change the color of this box accordingly.
            //use an Int and a while loop or something so that every click increments the Int
            //then each int value corresponds to a color 
            // have a statement at the end that resets the int back to zero to keep the colors in the loop
            //I.E int color = 0, mouse click happens int color = 1, now color =1 which turns the box red, 
            //color can never be greater than 3, when it is, we set color back to zero. 

            for(int i = 0; i<=3; i++){
                switch (i){
                case 1: unitId.setBackground(Color.GREEN);
                break;
                case 2: unitId.setBackground(Color.red);
                break;
                case 3: unitId.setBackground(Color.white);
                break;
                }
            }//end while



            //System.out.println(i);


        }
    });

附录:

我解决了我自己的问题。我知道这将是一件微不足道的事情,如果在这里发布是浪费服务器资源,我深表歉意。昨晚当我试图解决这个问题时,我感到非常沮丧。这是功能代码。这里要记住的教训是休息一下,不要总是试图强迫解决方案。

unitId = new JTextField();
    unitId.setEditable(false);
    unitId.setBackground(Color.GREEN);
    unitId.setHorizontalAlignment(SwingConstants.CENTER);
    unitId.setToolTipText("<html>SHADE CELLS TO REFLECT CURRENT UNIT STATUS:" + "<br/>GREEN-MC" + "<br/>RED-NMC" + "<br/>WHITE-UNIT IN TRANSISTION</html>");
    unitId.setText("A 4/5");
    unitId.setBounds(0, 116, 79, 172);
    getContentPane().add(unitId);
    unitId.setColumns(10);
    unitId.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            //Fill with sweet code to change the color of this box accordingly.
            //use an Int and a while loop or something so that every click increments the Int
            //then each int value corresponds to a color 
            // have a statement at the end that resets the int back to zero to keep the colors in the loop
            //I.E int color = 0, mouse click happens int color = 1, now color =1 which turns the box red, 
            //color can never be greater than 3, when it is, we set color back to zero. 


            if(starter==0){
                unitId.setBackground(Color.GREEN);
                starter++;
            }
            else if(starter==1){
                unitId.setBackground(Color.WHITE);
                starter++;
            }
            else if(starter==2){
                unitId.setBackground(Color.RED);
                starter++;
            }
            else{
                starter=0;
                unitId.setBackground(Color.GREEN);

            }


            //System.out.println(i);


        }
    });
4

0 回答 0