添加到JPanel

时间:2015-12-22 22:00:27

标签: java graphics mouselistener

我正在编写一个需要鼠标监听器的游戏程序,似乎它没有添加到我的面板中,因为即使在我的鼠标方法中放入一个sysout之后,也没有响应。以下是我的代码:

public class Display extends JComponent implements MouseListener, MouseMotionListener{

//fields
private GameBoardDisplay game;
private JFrame frame;
private JPanel boardPanel, controls;
private JButton nextGen, lastGen, clear, auto, genReturn;
private Timer time;
private JTextField genField;


//ctor that takes a GameOfLife object
public Display(GameOfLife game) {
    this.game = new GameBoardDisplay(game.getRows(), game.getCols());
    for (int row = 0; row < game.getRows(); row++) {
        for (int col = 0; col < game.getCols(); col++) {
            this.game.setState(row, col, game.getState(row, col));
        }
    }
}

//ctor that takes a 2D array
public Display(int[][] game){
    this.game = new GameBoardDisplay(game);
}

//ctor that takes the dimensions of the game
public Display(int rows, int cols){
    this.game = new GameBoardDisplay(rows,cols);
}

// sets up the Graphic user interface that displays the game
public void GUI() {
    boardPanel();
    controls();
    frame = new JFrame("Game Of Life Generation Number: " + game.getGenNum());
    frame.setVisible(true);
    frame.setSize(850, 900);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(game, BorderLayout.CENTER);
    frame.add(controls, BorderLayout.SOUTH);
    }

// sets up the game of life board display
public void boardPanel() {
    boardPanel = new JPanel();
    boardPanel.setLayout(new BorderLayout());
    boardPanel.add(game, BorderLayout.CENTER);
    boardPanel.setBackground(Color.WHITE);
    boardPanel.addMouseListener(this);
    boardPanel.addMouseMotionListener(this);
}   

//sets up the control panel
public void controls(){
    this.controls = new JPanel();
    //controls.add(lastGen);
    //lastGen();
    //controls.add(genField);
    //controls.add(genReturn);
    //genReturn();
    //controls.add(nextGen);
    nextGen();
    clear();
    auto();
    controls.add(clear);
    controls.add(auto);
}


public void genField(){
    final int FIELD_WIDTH = 10;
    genField = new JTextField(FIELD_WIDTH);

}


public static void main(String[] args) {
    Display tester = new Display(30,30);
    tester.GUI();

}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int boxHeight = game.boxHeight();
    int boxWidth = game.boxWidth();
    int rowIndex = y / boxHeight;
    int columnIndex = x / boxWidth;
    if (game.getState(rowIndex, columnIndex) == 0) {
        game.setState(rowIndex, columnIndex, 1);
    } else {
        game.setState(rowIndex, columnIndex, 0);
    }
    game.repaint();
    System.out.println("Mouse pressed");
}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int boxHeight = game.boxHeight();
    int boxWidth = game.boxWidth();
    int rowIndex = y / boxHeight;
    int columnIndex = x / boxWidth;
    game.setState(rowIndex, columnIndex, 1);
    game.repaint();
    System.out.println("Mouse dragged");
}

@Override
public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub

}

}

这是我在另一个类中的paintComponent

public void paintComponent(Graphics g){
    int boxWidth = boxWidth();
    int boxHeight = boxHeight();
    for(int row=0; row<game.getRows(); row++){
        for(int col=0; col<game.getCols(); col++){
            g.setColor(Color.BLACK);
            g.drawRect(col*boxWidth, row*boxHeight, boxWidth, boxHeight);
            if(game.getState(row, col)==1){
                g.fillRect(col*boxWidth, row*boxHeight, boxWidth, boxHeight);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

使用addMouseListener()后立即添加以下代码行:

setFocusable(true);

这使得窗口可以从其默认的不可聚焦形式聚焦。