在其他单元格上单击更改单元格背景 [英] Changing cell background on click in another cell

查看:178
本文介绍了在其他单元格上单击更改单元格背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个9x9的数独谜题。为UI部分,我要点击和单击任何其他细胞时恢复正常时改变细胞的背景。下面是我的单元格类。

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.

推荐答案

您最好能够使用 AbstractButton中的,如的 的JButton ,其中包括这种能力对每个支持的外观和放大器;感觉。您可以使用覆盖默认值 UIManager.put()的按钮相关的按键,下面列出,如图所示的此处。这 CellTest 也可以提出一些想法。右键单击以查看上下文菜单,标签查看焦点侦听器是如何工作的。

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

这篇关于在其他单元格上单击更改单元格背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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