如果点击前景如何保持前景相同的颜色

时间:2018-05-12 03:15:37

标签: java swing mouselistener

单击组件后,无论将来是否有任何鼠标操作,都应保留Color.BLUE的前景。我怎样才能做到这一点?

这是我的代码到目前为止,我觉得我应该使用布尔值isntance变量来跟踪组件是否被点击,但不知道如何解决这个问题。非常感谢帮助。

到目前为止这是我的代码

public class IdeaMouseListener implements MouseListener {

    //instance      
    private JComponent x;

    //constructor
    public IdeaMouseListener(JComponent x){         
        this.x =x;          
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        x.setForeground(Color.BLUE);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        x.setForeground(Color.LIGHT_GRAY);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        x.setForeground(Color.WHITE);
    }

}

2 个答案:

答案 0 :(得分:0)

然后设置一个标志,表示"点击" state,使用它来确定应该发生什么,例如

public class IdeaMouseListener implements MouseListener {

    //instance      
    private JComponent x;
    private boolean wasClicked = false;

    //constructor
    public IdeaMouseListener(JComponent x) {
        this.x = x;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        x.setForeground(Color.BLUE);
        wasClicked = true;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if (!wasClicked) {
            x.setForeground(Color.LIGHT_GRAY);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        if (!wasClicked) {
            x.setForeground(Color.WHITE);
        }
    }

}

答案 1 :(得分:0)

设置侦听器的属性以控制"单击"组件的颜色。然后,只有当它不是单击的颜色时才更改颜色。

类似的东西:

public class IdeaMouseListener implements MouseListener {

    //instance      
    private Color clickedColor;

    //constructor
    public IdeaMouseListener(Color clickedColor){         
        this.clickedColor = clickedColor;          
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        Component source = e.getComponent();
        source.setForeground(clickedColor);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        Component source = e.getComponent();
        if (!source.getForeground.equals(clickedColor)
            component.setForeground(Color.LIGHT_GRAY);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        Component source = e.getComponent();
        if (!source.getForeground.equals(clickedColor)
            component.setForeground(Color.WHITE);
    }
}

请注意代码的更改:

  1. 您不需要将Component作为参数传递,因为您可以从MouseEvent获取该信息。所以代码更通用

  2. 由于代码是通用的,因此所有组件都可以共享同一个侦听器。

  3. 您知道传入"点击的颜色"。这不是必需的,但它使类更可重用,因为您可以轻松地更改类的属性,而不是硬编码值。