Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaJFrame中创建一组可单击的面板_Java_Jframe_Conways Game Of Life - Fatal编程技术网

如何在JavaJFrame中创建一组可单击的面板

如何在JavaJFrame中创建一组可单击的面板,java,jframe,conways-game-of-life,Java,Jframe,Conways Game Of Life,我正在尝试使用JFrame在Java中重新创建生活游戏。我已经完成了大部分程序,但有一件事困扰着我。如何创建一组可单击的字段(面板),以便用户可以输入自己的模式,而不是计算机每次随机生成模式?您可以使用GridLayout布局管理器将所有JPanel放在一个网格中,并为每个JPanel添加一个带有addMouseListener()的MouseApter类实例侦听鼠标单击以翻转其状态。MouseAdapter的实例将重写mouseClicked(),并在该函数中翻转JPanel的状态 这只是一个

我正在尝试使用JFrame在Java中重新创建生活游戏。我已经完成了大部分程序,但有一件事困扰着我。如何创建一组可单击的字段(面板),以便用户可以输入自己的模式,而不是计算机每次随机生成模式?

您可以使用GridLayout布局管理器将所有JPanel放在一个网格中,并为每个JPanel添加一个带有addMouseListener()的MouseApter类实例侦听鼠标单击以翻转其状态。MouseAdapter的实例将重写mouseClicked(),并在该函数中翻转JPanel的状态

这只是一个完整的示例,但下面是框架的创建和布局管理器的设置:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    int width = 200, height = 200;
    frame.setSize(width, height);
    int rows = width/10, cols = height/10;
    frame.setLayout(new GridLayout(rows, cols));
    // add all the cells
    for(int j = 0; j < cols; j++) {
        for(int i = 0; i < rows; i++) {
            frame.add(new Cell(i, j));
        }
    }
    frame.setVisible(true);
}
}


或者,您可以覆盖一个JPanel的paintComponent()方法,并执行上述操作,但也可以使用addMouseMotionListener(),这样您的一个面板就可以跟踪鼠标所在的绘制网格单元,并可以控制它们的绘制方式。

您的意思是这样的吗?这会给每个单元格添加一个新的MouseListener吗?我已经用一个代码示例更新了我的答案,用于向所有单元格添加MouseListener,尽管是的,您的代码似乎是正确的。
class Cell extends JPanel {
int row, col;
public static final int STATE_DEAD = 0;
public static final int STATE_ALIVE = 1;
int state = STATE_DEAD;

public Cell(int row, int col) {
    this.row = row;
    this.col = col;
    // MouseAdapter tells a component how it should react to mouse events
    MouseAdapter mouseAdapter = new MouseAdapter() {
        // using mouseReleased because moving the mouse slightly
        // while clicking will register as a drag instead of a click
        @Override
        public void mouseReleased(MouseEvent e) {
            flip();
            repaint(); // redraw the JPanel to reflect new state
        }
    };
    // assign that behavior to this JPanel for mouse button events
    addMouseListener(mouseAdapter);
}

// Override this method to change drawing behavior to reflect state
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // fill the cell with black if it is dead
    if(state == STATE_DEAD) {
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

public void flip() {
    if(state == STATE_DEAD) {
        state = STATE_ALIVE;
    } else {
        state = STATE_DEAD;
    }
}