Changing cell background on click in another cell

2019-08-18 06:14发布

问题:

I am trying to build a 9x9 sudoku puzzle. For the UI part, I want to change the background of cell when clicked and return to normal when any other cell is clicked. Below is my Cell Class.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;

    public class Cell extends JPanel implements MouseListener{

    Cell[][] cell;
    private int x;//x pos;
    private int y;//y pos;
    private int num=0;
    private JLabel lnum;
    private Color bg;
    private static int xpos=-1;
    private static int ypos=-1;
    private static Color back;
    public Cell(Cell[][] cell,int x, int y)
    {
        this.cell=cell;
        this.x=x;
        this.y=y;
        x+=1; y+=1;
        setBorder(BorderFactory.createLineBorder(Color.yellow));
        if((x%6>=1&&x%6<=3)&&(y%6==0||y%6>3)||(y%6>=1&&y%6<=3)&&(x%6==0||x%6>3))
            bg=Color.BLUE;
        else
            bg=Color.BLACK;
        setBackground(bg);
        setPreferredSize(new Dimension(50,50));
        lnum=new JLabel(String.valueOf(num),null,JLabel.CENTER);
        lnum.setForeground(Color.green);
        add(lnum,SwingConstants.CENTER);
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        if(xpos!=-1)
        {
        cell[xpos][ypos].setBackground(back);
        cell[xpos][ypos].setBorder(BorderFactory.createLineBorder(Color.yellow));
        }
        xpos=x;
        ypos=y; 
        back=bg;
        setBackground(Color.LIGHT_GRAY);
        setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
                                                        Color.cyan,Color.BLUE));
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if(x!=xpos&&y!=ypos)
        {
        setBackground(Color.WHITE);
        setBorder(BorderFactory.createLineBorder(Color.RED));
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if(x!=xpos&&y!=ypos)
        {
        setBackground(bg);
        setBorder(BorderFactory.createLineBorder(Color.yellow));
        }
    }

}

However, reverting back to normal background color and border of a cell after clicking one and then another cell is not working. Please help. Thanks in advance.

回答1:

You'd be better to use an AbstractButton, such as JButton, which includes this capability for each supported Look & Feel. You can override the defaults using UIManager.put() for button-related keys, listed below, as shown here. This CellTest may also suggest some ideas. Right-click to see the context menu, and tab to see how the focus listener works.

Button.background
Button.border
Button.darkShadow
Button.defaultButtonFollowsFocus
Button.disabledText
Button.font
Button.foreground
Button.highlight
Button.light
Button.margin
Button.opaque
Button.select
Button.shadow
Button.textIconGap
Button.textShiftOffset