简体   繁体   English

如何在屏幕上居中放置JPanel

[英]How to center JPanel on the screen

I am trying to do something similar to "game of life" using JPanel, the problem is that it appears in left upper side of the screen. 我正在尝试使用JPanel做类似于“生活游戏”的问题,问题在于它出现在屏幕的左上角。 My question is how can I make it screen centered. 我的问题是如何使屏幕居中显示。 PS I also tried creating JFrame and adding JPanel to it, but it just created JFrame and JPanel separately. PS我也尝试创建JFrame并向其中添加JPanel,但它只是分别创建了JFrame和JPanel。 Here is the code: 这是代码:

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();
}

} }

Here is the part where I try to use JFrame: 这是我尝试使用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]

在此处输入图片说明

If you want to let the frame appear at the center of your screen, you can set the location by: 如果想让边框出现在屏幕中央,可以通过以下方式设置位置:

frame.setLocationRelativeTo(null);

Note that you would want to invoke the above after you set the size (or pack() ) your frame. 请注意, 设置框架的大小(或pack() ,您需要调用以上内容。

You have a JPanel: you add it to a JFrame: 您拥有一个JPanel:将其添加到JFrame中:

Then to center the JFrame on the screen you do 然后将JFrame放在屏幕上居中

  Toolkit it=Toolkit.getDefaultToolkit();
  Dimension d=it.getScreenSize();
  int w=jf.getWidth(), h=jf.getHeight();
  jf.setLocation(d.width/2-w/2, d.height/2-h/2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM