的mouseClicked事件到面板的的setBackground [英] MouseClicked event to setBackground of a panel

查看:144
本文介绍了的mouseClicked事件到面板的的setBackground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个GUI对猜谜游戏与9x12面板持有每一个随机数。我已经说得那么当你将鼠标悬停在每个单独的面板,它从红色一旦您的鼠标离开面板区域变为黄色,背部为红色。
我现在的问题是改变点击面板的颜色为绿色,任何previously点击面板返回到它的原始颜色为红色。如预期,但我失去了对如何到一个新的面板单击后pviously点击面板恢复$ P $回到红色变为绿色。我希望能有一个明显的答案,但这里是一些相关的code(不打磨掉):

 公共类NumberPanel继承JPanel {
    INT兰特;
    随机数发生器=新的随机();
    JLabel的数字;
    布尔的mouseEntered = FALSE;
    布尔的mouseClicked = FALSE;
    布尔mouseUnClicked = FALSE;
    mainPanel中熔点;
    公共NumberPanel(){
        的setBackground(Color.RED);
        集preferredSize(新尺寸(40,40));
        兰特= generator.nextInt(8)+1;
        addMouseListener将(新NumberListener());
    }    公共NumberPanel(mainPanel中MP){
         //为mainPanel中的回调方法
            this.mp = MP;
    }
    公共无效的paintComponent(图形G){
        super.paintComponent方法(G);
        字体的字体=新的字体(宋体,Font.BOLD,18);
        g.setFont(字体);
        g.drawString(+兰特,14,24);
        如果(的mouseEntered){
            的setBackground(Color.YELLOW);
        }
        其他{
            的setBackground(Color.RED);
        }
        如果(的mouseClicked){
            的setBackground(Color.GREEN);
        }
    }    //重新presents鼠标事件监听器
    私有类NumberListener实现的MouseListener {        公共无效的mouseEntered(事件的MouseEvent){
            =的mouseEntered真实;
            重绘();
        }
        公共无效的mouseExited(事件的MouseEvent){
            =的mouseEntered虚假的;
            重绘();
        }
        公共无效的mouseClicked(事件的MouseEvent){
        }
        公共无效的mouseReleased(事件的MouseEvent){
        }
        公共无效鼠标pressed(事件的MouseEvent){
            的mouseClicked = TRUE;
            重绘();
        }
    }
}


解决方案

就在 NumberPanel NumberPanel 字段>名为电流

 私有静态NumberPanel电流;...
//创建,而不是创建为每一个新的静态的MouseListener
// NumberPanel实例。
私有静态最终MouseAdapter的MouseListener =新MouseAdapter(){    公共无效鼠标pressed(事件的MouseEvent){
        NumberPanel面板=(NumberPanel)event.getSource();
        如果(电流!= NULL){
            current.mouseClicked = FALSE;
        }
        电流=面板;
        panel.mouseClicked = TRUE;
        //重绘号集装箱板
    }
}
...
addMouseListener将(MouseListener的);

这样的东西应该跟踪当前点击面板。

I'm making a GUI pairs guessing game with 9x12 panels to hold a random number in each. I have made it so when you hover over each individual panel, it changes from red to yellow, and back to red once your mouse leaves the panel area. My problem now is changing the color of a clicked panel to green, and any previously clicked panel back to it's original color red. It turns green as intended, but am lost as to how to reset the previously clicked panel back to red after a new panel is clicked. I'm hoping there is an obvious answer but here is some relevant code (Not polished off):

public class NumberPanel extends JPanel {
    int rand;
    Random generator = new Random();
    JLabel numbers;
    boolean mouseEntered = false;
    boolean mouseClicked = false;
    boolean mouseUnClicked = false;
    MainPanel mp;


    public NumberPanel() {
        setBackground(Color.RED);
        setPreferredSize (new Dimension(40,40));            
        rand = generator.nextInt(8) +1;    
        addMouseListener(new NumberListener());    
    }

    public NumberPanel (MainPanel mp) {
         //Callback method for MainPanel
            this.mp = mp;
    }


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Font font = new Font("Verdana", Font.BOLD, 18);
        g.setFont(font);
        g.drawString("" +rand, 14,24);            
        if (mouseEntered) {
            setBackground(Color.YELLOW);
        }
        else {
            setBackground(Color.RED);
        }
        if (mouseClicked) {
            setBackground(Color.GREEN);
        } 
    }

    //represents the listener for mouse events
    private class NumberListener implements MouseListener {

        public void mouseEntered (MouseEvent event) {
            mouseEntered=true;
            repaint();
        }
        public void mouseExited(MouseEvent event) {
            mouseEntered=false;
            repaint();
        }
        public void mouseClicked(MouseEvent event) {
        }
        public void mouseReleased(MouseEvent event) {  
        }
        public void mousePressed(MouseEvent event) { 
            mouseClicked=true;
            repaint();
        }            
    }        
}

解决方案

Just create a static NumberPanel field in NumberPanel called current:

private static NumberPanel current;

...
// create a static MouseListener instead of creating a new one for each
// NumberPanel instance.
private static final MouseAdapter mouseListener = new MouseAdapter(){

    public void mousePressed(MouseEvent event) {
        NumberPanel panel = (NumberPanel) event.getSource(); 
        if(current != null) { 
            current.mouseClicked = false;
        }
        current = panel;
        panel.mouseClicked = true;
        // repaint number panels container
    }
}
...
addMouseListener(mouseListener);

Something like that should keep track of the current clicked panel.

这篇关于的mouseClicked事件到面板的的setBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆