在窗格中更改较小的JPanel的颜色

时间:2019-03-13 15:39:37

标签: java jpanel

我在使用MouseListener来更改面板的颜色时遇到问题,面板的颜色是行和列的许多其他类似面板之一。到目前为止,这是我的进步:

import java.awt.*;
    import javax.swing.*;
    import java.awt.Color.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.util.Random;

    public class Checkers extends JFrame implements MouseListener
    {
        Random random = new Random();
        private final int ROWS = 2;
        private final int COLS = 5;
        private final int GAP = 2;
        private final int NUM = ROWS * COLS;
        private int x;
        private JPanel pane = new JPanel(new GridLayout(ROWS,COLS, GAP,GAP));
        private JPanel pane2 = new JPanel();

        private JPanel [] panel = new JPanel[NUM];
        private Color col1 = Color.RED;
        private Color col2 = Color.WHITE;
        private Color tempColor;

        public Checkers()
        {
            super("Checkers");
            setSize(600,600);
            setVisible(true);
            addMouseListener(this);
            mouseClicked(null);
            setBackground(Color.BLACK);
            setBoard();
        }

        public void setBoard()
        // rotates colors on the checkbaord
        {

            for(int x = 0; x < NUM; x++) {
                panel[x] = new JPanel();
                pane.add(panel[x]);
                if (x % COLS == 0) {
                    tempColor = col1;
                    col1 = col2;
                    col2 = tempColor;}
                if (x % 2 == 0) {
                    panel[x].setBackground(col1);}
                else {
                    panel[x].setBackground(col2);}
            }

            //pane background colour and the size of this pane.
            pane.setBackground(Color.BLACK);
            pane.setPreferredSize(new Dimension(300,300));


            //pane background colour and size of this pane.
            pane2.setBackground(Color.BLACK);
            pane2.setPreferredSize(new Dimension(300,300));

            //directions on the board where these panes appear.
            add(pane, BorderLayout.WEST);
            add(pane2, BorderLayout.EAST);

            pane2.setLayout(new BoxLayout(pane2, BoxLayout.PAGE_AXIS));
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {

            //randomly generate a new color for one of the panes when that pane is clicked.
            int r = random.nextInt(255);
            int g = random.nextInt(255);
            int b = random.nextInt(255);
            setBackground(new Color(r, g, b));
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {        
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    }

预期结果-我要做的是,一旦单击红色或白色面板之一,它将触发mouseClicked覆盖并设置该面板的颜色变黑(当前设置为随机rgb,但仍然无法正常工作),因此用户知道已按下该按钮。其余的替代项将保留为空,因为它们对我而言毫无用处。

当前结果-程序已执行,但单击后窗格的颜色不会改变。

以下是程序当前执行时的外观的直接图像文件:

example

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

可能有更好的方法来做您想要的事情,但是一种方法是创建自定义JPanel类:

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class MyPanel extends JPanel implements MouseListener {

    public MyPanel() {
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        setBackground(Color.BLACK);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {   
    }

    @Override
    public void mouseEntered(MouseEvent e) {    
    }

    @Override
    public void mouseExited(MouseEvent e) { 
    }
}

然后代替创建常规的JPanel数组:

private JPanel[] panel = new JPanel[NUM];

您可以创建自定义JPanel的数组:

private MyPanel[] panel = new MyPanel[NUM];

自定义JPanel都有自己的MouseListener,因此,当单击一个时,它会自动更新。