简体   繁体   中英

java - highlighting only one field

I want to make my code highlights the square on which I click (this works). I do not know how to do that when you click on another square previous died and lights up a new one. How to improve the method mouseClicked?

public class App
{
    public static void main(String[] args)
    {
        new App();
    }

    App()
    {
        JFrame jFrame = new JFrame("Mouse Hover Demo");
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(new GridLayout(5,5));
        for(int i=0;i<25;i++)
        {
            jFrame.add(new CustomPanel(i));
        }
        jFrame.pack();
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }

    class CustomPanel extends JPanel implements MouseListener
    {
        /**
         * 
         */
        boolean isHighlighted;
        int postion;
        int tab[];

        private static final long serialVersionUID = 1L;
        Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);

        CustomPanel(int postion)
        {
            this.postion = postion;
            addMouseListener(this);
            setBorder(blackBorder);
            setFocusable(true);
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(50, 50);
        }

        @Override public void mouseClicked(MouseEvent e)
        {

                System.out.println(this.postion);
                setBackground(Color.black);
        }
        @Override public void mousePressed(MouseEvent e){}
        @Override public void mouseReleased(MouseEvent e){}

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

        }

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

        }
    }
}

Eg in App class you can have field which is table of CustomPanels ( private CustomPanel[] myCustomPanels and another field - integer private int highlightedOne = -1 which will point index of element which is highlighted.

Then in method mouseClicked you can do something like that:

@Override public void mouseClicked(MouseEvent e)
        {

                System.out.println(this.postion);
                if (highlightedOne != -1) 
                    myCustomPanels[highlightedOne].setBackground(Color.white);
                highlightedOne = this.position;
                setBackground(Color.black);
        }

Field section:

public class App
{

    private CustomPanel[] myCustomPanels = new CustomPanel[25];
    private int highlightedOne = -1;

    public static void main(String[] args)
    {
        new App();
    }

    App()
    {
        JFrame jFrame = new JFrame("Mouse Hover Demo");
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(new GridLayout(5,5));
        for(int i=0;i<25;i++)
        {
            CustomPanel toAdd = new CustomPanel(i);
            myCustomPanels[i] = toAdd;
            jFrame.add(toAdd);
        }
        jFrame.pack();
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }
...

Alternatively you can use List instead of table - you don't need then pass size to it on beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM