JPanel을 사용하여 Game Of Life에 대한 GUI를 만들면 클래스가 JFrame을 확장하고 MouseListener를 구현하지만, JLabel을 클릭하면 MouseListener가 한 번만 작동합니다.Java MouseListener MouseClicked는 한 번만 작동합니다.
어떻게해야할까요? 내 GameOfLife 클래스의 코드에도 문제가 없습니다. 메신저도 일부 문자열을 인쇄하려고하기 때문에 한 번만 작동하기 때문에.
감사합니다. 당신은 당신이 JLabels의 다차원 배열에있는 귀하의 모든 셀에 마우스 수신기를 지정하여 생성자에서
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.Border;
public class GameOfLifeWindow extends JFrame implements ActionListener, MouseListener
{
private static final int DEF_ROWS = 16;
private static final int DEF_COLS = 16;
private JLabel[][] cells;
GameOfLife gol;
private JPanel panClear;
private JPanel panCenter;
private JPanel panNextGen;
private JButton nextGen;
private JButton clear;
private JMenuBar menuBar;
private JMenu file;
private JMenuItem newGame;
private JMenuItem loadFile;
public GameOfLifeWindow()
{
super("Game Of Life");
gol = new GameOfLife(DEF_ROWS, DEF_COLS);
//sets layout and dimension
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().setPreferredSize(new Dimension(300,400));
//Sets the panels
panClear = new JPanel(new FlowLayout());
panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
panNextGen = new JPanel(new FlowLayout());
//Adds the panel to the JFrame
this.getContentPane().add(panClear, BorderLayout.NORTH);
this.getContentPane().add(panCenter, BorderLayout.CENTER);
this.getContentPane().add(panNextGen, BorderLayout.SOUTH);
//Sets the next generation button
this.nextGen = new JButton("Next Generation");
panNextGen.add(this.nextGen);
this.nextGen.addActionListener(this);
//Sets the clear button
ImageIcon btnIcon = new ImageIcon(getClass().getResource("images/clear.png"));
Image tmpImg = btnIcon.getImage();
BufferedImage bi = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(tmpImg, 0, 0, 30, 30, null);
this.clear = new JButton(new ImageIcon(bi));
this.clear.addActionListener(this);
panClear.add(clear);
//Set the GridLayout
this.updateGeneration();
//Set the mouse listener to each cell
for(int i = 0 ; i < this.cells.length ; i++)
for(int j = 0 ; j < this.cells[i].length ; j++)
this.cells[i][j].addMouseListener(this);
//Sets MenuBar
this.menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
//Sets the File menu
this.file = new JMenu("File");
menuBar.add(file);
//Sets the New Game in the file
this.newGame = new JMenuItem("New Game");
this.newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.CTRL_MASK));
this.newGame.addActionListener(this);
file.add(this.newGame);
//Sets the Load File in the file
this.loadFile = new JMenuItem("Load File");
this.loadFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK));
this.loadFile.addActionListener(this);
file.add(this.loadFile);
}
public void updateGeneration()
//updates the JLabels according to the Game Of Life Screen
{
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
this.getContentPane().remove(this.panCenter);
this.panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
this.cells = new JLabel[gol.getRows()][gol.getCols()];
this.panCenter.removeAll();
for(int i = 0 ; i < cells.length ; i++)
for(int j = 0 ; j < cells[i].length ; j++)
{
this.cells[i][j] = new JLabel();
this.cells[i][j] .setOpaque(true); // make the color visible
this.cells[i][j].setBorder(border);// sets borders
if(gol.isAlive(i, j))
this.cells[i][j].setBackground(Color.BLACK);
else
this.cells[i][j].setBackground(Color.white);
panCenter.add(this.cells[i][j]);
}
this.getContentPane().add(this.panCenter);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.nextGen)
{
this.gol.nexGeneration();
this.updateGeneration();
}
else if(e.getSource() == this.clear)
{
this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
this.updateGeneration();
}
else if(e.getSource() == this.newGame)
{
this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
this.updateGeneration();
}
else if(e.getSource() == this.loadFile)
{
String fileName = JOptionPane.showInputDialog("Please Enter A File Name");
this.gol = new GameOfLife(fileName);
System.out.println(gol.toString());
this.updateGeneration();
}
}
@Override
public void mouseClicked(MouseEvent e)
{
boolean found = false;
for(int i = 0 ; i < cells.length && !found ; i++)
for(int j = 0 ; j < cells[i].length && !found; j++)
if(e.getSource() == this.cells[i][j])
{
this.gol.setForGUI(i, j);
found = true;
}
this.updateGeneration();
System.out.println("asdsad");
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
괄호'{} '는 필요하지 않은 경우에도 사용하는 것이 좋습니다. –
좋습니다, 조언 해 주셔서 감사합니다. 코드에서 어떤 부분이 잘못되었을 수도 있습니다. – Pachu
알 수 없습니다. 코드가 컴파일되지 않습니다. 그것도 클래스가 누락되었습니다 –