使用鼠标单击事件控制JTextField的背景颜色

时间:2013-09-11 03:07:21

标签: java swing jtextfield mouseclick-event

我目前正致力于一个程序员的个人发展计划,我正在努力解决这个鼠标滑动事件的微型砖墙。当我在脑海中和白板上想出这个程序时,我看到它利用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);


        }
    });

0 个答案:

没有答案