如何使 JPanel 在屏幕上居中

Kes*_*lme 1 java jpanel jframe

我正在尝试使用 JPanel 做类似“生命游戏”的事情,问题是它出现在屏幕的左上角。我的问题是如何使其屏幕居中。PS我还尝试创建JFrame并向其中添加JPanel,但它只是分别创建了JFrame和JPanel。这是代码:

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Animation extends JPanel implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Cell[][] cellMatrix;
private Options op;

Animation(Options received) {

this.op = received;
this.cellMatrix = new Cell[op.getNumberOfCells()][op.getNumberOfCells()];
this.setBackground(op.getBackGroundColour());

}

Timer time = new Timer(5,this);

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    for (int i = 0; i < op.getNumberOfCells() ; i++) {
        for(int j = 0; j < op.getNumberOfCells(); j++) {    
        Cell c1 = new Cell();

        if(c1.getVal() % 2 == 0) {

            g.setColor(op.getAliveCellColour());
            g.fillRect(i * 10, j * 10, 10, 10);
            c1.changeStat(0);
            cellMatrix[i][j]= c1;

        }
        else {

            g.setColor(op.getDeadCellColour());
            g.fillRect(i * 10,j * 10, 10, 10);
            c1.changeStat(1);
            cellMatrix[i][j]= c1;

        }
        }
    }
    time.start();

    for (int i = 0; i < op.getNumberOfCells() ; i=i+2) {
        for (int j = 0; j < op.getNumberOfCells()-1; j++){
            int sum = cellMatrix[i][j].getVal() + cellMatrix[i+1][j].getVal()+ cellMatrix[i][j+1].getVal() + cellMatrix[i+1][j+1].getVal();
            switch(sum) {
            case(2) : continue;
            case(0) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(1) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(4) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(3) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();blockShift(i,j);break;
            }
        }
    }
    for (int i = 1; i < op.getNumberOfCells()-1 ; i=i+2) {
        for (int j = 1; j < op.getNumberOfCells()-1; j++){
            int sum = cellMatrix[i][j].getVal() + cellMatrix[i+1][j].getVal()+ cellMatrix[i][j+1].getVal() + cellMatrix[i+1][j+1].getVal();
            switch(sum) {
            case(2) : continue;
            case(0) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(1) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(4) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(3) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();blockShift(i,j);break;
            }
        }
    }
}

public void blockShift(int i, int j) {

    Cell temp1 = new Cell(this.cellMatrix[i][j]);
    this.cellMatrix[i][j]=this.cellMatrix[i+1][j+1];
    this.cellMatrix[i+1][j+1] = temp1;
    Cell temp2 = new Cell(this.cellMatrix[i+1][j]);
    this.cellMatrix[i+1][j] = this.cellMatrix[i][j+1];
    this.cellMatrix[i][j+1] = temp2;

}

public void actionPerformed(ActionEvent e) {
    time.setDelay(1000);
    repaint();
}
Run Code Online (Sandbox Code Playgroud)

}

这是我尝试使用 JFrame 的部分:

public class Animation extends JPanel implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Cell[][] cellMatrix;
private Options op;
private JFrame jf;

Animation(Options received) {

jf = new JFrame("Example");
jf.setSize(860, 640);
jf.setLocationByPlatform(false);

this.op = received;
this.cellMatrix = new Cell[op.getNumberOfCells()][op.getNumberOfCells()];
this.setBackground(op.getBackGroundColour());
jf.add(this);
jf.setVisible(true);

}[![enter image description here][1]][1]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

use*_*460 5

如果想让框架出现在屏幕中央,可以通过以下方式设置位置:

frame.setLocationRelativeTo(null);
Run Code Online (Sandbox Code Playgroud)

请注意,在设置框架的大小(或 )后,您可能需要调用上述内容。pack()