如何将 MouseListeners 添加到面板网格中的单个面板?
How do I add MouseListeners to an individual panel in a grid of panels?
我正在尝试创建黑白棋游戏,我正处于开始阶段,试图弄清楚如何让方块响应点击事件。到目前为止,这是我的代码。
明确地说,目前,我只是想更改网格中单击面板的背景颜色。我仍在学习 ActionListeners 的工作原理。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.MouseListener;
public class Reversi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ReversiFrame frame = new ReversiFrame();
frame.setSize(400,450);
frame.setResizable(false);
frame.setVisible(true);
}
}
/**
*
* Class for the frame
*/
class ReversiFrame extends JFrame{
/**
* Constructor for ReversiFrame
*/
public ReversiFrame(){
super("Reversi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu gameMenu = new JMenu("Game");
JMenu helpMenu = new JMenu("Help");
bar.add(gameMenu);
bar.add(helpMenu);
final JMenuItem newGame = new JMenuItem("New Game");
final JMenuItem exit = new JMenuItem("Exit");
final JMenuItem help = new JMenuItem("Help");
gameMenu.add(newGame);
gameMenu.add(exit);
helpMenu.add(help);
/**
* Exits program when exit menu item is clicked.
*/
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
ReversiPanel panel = new ReversiPanel();
add(panel);
}
}
class ReversiPanel extends JPanel{
public static final int GRID_ROWS = 8;
public static final int GRID_COLS = 8;
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private GridPanel [][] panels = new GridPanel[GRID_ROWS][GRID_COLS];
public ReversiPanel(){
setLayout(new GridLayout(GRID_ROWS,GRID_COLS));
setSize(HEIGHT, WIDTH);
for(int row = 0; row < GRID_ROWS; row++){
for(int col = 0; col < GRID_COLS; col++){
panels[row][col] = (new GridPanel(row, col));
setFocusable(true);
panels[row][col].addMouseListener(new MouseAdapter(){
/*public void mouseClicked(MouseEvent e){
((GridPanel)e.getSource()).getParent().setBackground(Color.red);
repaint();
}*/
});
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for(int row = 0; row < GRID_ROWS; row++){
for(int col = 0; col < GRID_COLS; col++){
panels[row][col].draw(g2);
}
}
}
}
class GridPanel extends JPanel{
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private boolean filled;
private int numberGridsFilled = 0;
private int x, y;
public GridPanel(int row, int col){
x = row * WIDTH;
y = + col * HEIGHT;
setSize(HEIGHT, WIDTH);
filled = false;
setBorder(BorderFactory.createLineBorder(Color.GRAY));
setBackground(Color.red);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
setBackground(Color.RED);
repaint();
}
});
}
public void draw(Graphics2D g2){
g2.setPaint(Color.GRAY);
Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
g2.draw(r);
}
public void changeColor(){
setBackground(Color.BLACK);
}
public void setColor() {
setBackground(Color.BLACK);
}
}
class Tile{
private Color color;
public Tile(Color color){
}
}
… trying to figure out how to have a square respond to a click event.
将正方形设为 a(可能未装饰)JButton
而不是 JPanel
。给按钮一个图标,and/or 文本,然后添加一个 ActionListener
。
带有动作侦听器的按钮将响应鼠标和键盘事件。
如 this answer to Add a complex image in the panel, with buttons around it in one customized user interface.
中所示
首先,我不会在你的paintComponent
中做panels[row][col].draw(g2);
,GridPanel
是从JPanel
延伸出来的,JPanel
很能画本身。
首先摆脱 paintComponent
方法并将 GridPanel
添加到 ReversiPanel
...
public ReversiPanel() {
setLayout(new GridLayout(GRID_ROWS, GRID_COLS));
for (int row = 0; row < GRID_ROWS; row++) {
for (int col = 0; col < GRID_COLS; col++) {
panels[row][col] = (new GridPanel(row, col));
add(panels[row][col]);
setFocusable(true);
panels[row][col].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
((GridPanel)e.getSource()).getParent().setBackground(Color.red);
repaint();
}
});
}
}
}
当组件添加到容器(附加到本机对等体)时,它有资格接收事件通知
现在,在您 GridPanel
中,将 draw
方法替换为 paintComponent
并将 getPreferredSize
覆盖为 return 所需的组件大小
class GridPanel extends JPanel {
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private boolean filled;
private int numberGridsFilled = 0;
private int row, col;
public GridPanel(int row, int col) {
row = row;
col = col;
filled = false;
setBorder(BorderFactory.createLineBorder(Color.GRAY));
setBackground(Color.red);
}
public Dimension getPreferredSize() {
return new Dimenions(WIDTH, HEIGHT);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setPaint(Color.GRAY);
Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
g2d.draw(r);
g2d.dispose();
}
我正在尝试创建黑白棋游戏,我正处于开始阶段,试图弄清楚如何让方块响应点击事件。到目前为止,这是我的代码。
明确地说,目前,我只是想更改网格中单击面板的背景颜色。我仍在学习 ActionListeners 的工作原理。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.MouseListener;
public class Reversi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ReversiFrame frame = new ReversiFrame();
frame.setSize(400,450);
frame.setResizable(false);
frame.setVisible(true);
}
}
/**
*
* Class for the frame
*/
class ReversiFrame extends JFrame{
/**
* Constructor for ReversiFrame
*/
public ReversiFrame(){
super("Reversi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu gameMenu = new JMenu("Game");
JMenu helpMenu = new JMenu("Help");
bar.add(gameMenu);
bar.add(helpMenu);
final JMenuItem newGame = new JMenuItem("New Game");
final JMenuItem exit = new JMenuItem("Exit");
final JMenuItem help = new JMenuItem("Help");
gameMenu.add(newGame);
gameMenu.add(exit);
helpMenu.add(help);
/**
* Exits program when exit menu item is clicked.
*/
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
ReversiPanel panel = new ReversiPanel();
add(panel);
}
}
class ReversiPanel extends JPanel{
public static final int GRID_ROWS = 8;
public static final int GRID_COLS = 8;
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private GridPanel [][] panels = new GridPanel[GRID_ROWS][GRID_COLS];
public ReversiPanel(){
setLayout(new GridLayout(GRID_ROWS,GRID_COLS));
setSize(HEIGHT, WIDTH);
for(int row = 0; row < GRID_ROWS; row++){
for(int col = 0; col < GRID_COLS; col++){
panels[row][col] = (new GridPanel(row, col));
setFocusable(true);
panels[row][col].addMouseListener(new MouseAdapter(){
/*public void mouseClicked(MouseEvent e){
((GridPanel)e.getSource()).getParent().setBackground(Color.red);
repaint();
}*/
});
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for(int row = 0; row < GRID_ROWS; row++){
for(int col = 0; col < GRID_COLS; col++){
panels[row][col].draw(g2);
}
}
}
}
class GridPanel extends JPanel{
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private boolean filled;
private int numberGridsFilled = 0;
private int x, y;
public GridPanel(int row, int col){
x = row * WIDTH;
y = + col * HEIGHT;
setSize(HEIGHT, WIDTH);
filled = false;
setBorder(BorderFactory.createLineBorder(Color.GRAY));
setBackground(Color.red);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
setBackground(Color.RED);
repaint();
}
});
}
public void draw(Graphics2D g2){
g2.setPaint(Color.GRAY);
Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
g2.draw(r);
}
public void changeColor(){
setBackground(Color.BLACK);
}
public void setColor() {
setBackground(Color.BLACK);
}
}
class Tile{
private Color color;
public Tile(Color color){
}
}
… trying to figure out how to have a square respond to a click event.
将正方形设为 a(可能未装饰)JButton
而不是 JPanel
。给按钮一个图标,and/or 文本,然后添加一个 ActionListener
。
带有动作侦听器的按钮将响应鼠标和键盘事件。
如 this answer to Add a complex image in the panel, with buttons around it in one customized user interface.
中所示首先,我不会在你的paintComponent
中做panels[row][col].draw(g2);
,GridPanel
是从JPanel
延伸出来的,JPanel
很能画本身。
首先摆脱 paintComponent
方法并将 GridPanel
添加到 ReversiPanel
...
public ReversiPanel() {
setLayout(new GridLayout(GRID_ROWS, GRID_COLS));
for (int row = 0; row < GRID_ROWS; row++) {
for (int col = 0; col < GRID_COLS; col++) {
panels[row][col] = (new GridPanel(row, col));
add(panels[row][col]);
setFocusable(true);
panels[row][col].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
((GridPanel)e.getSource()).getParent().setBackground(Color.red);
repaint();
}
});
}
}
}
当组件添加到容器(附加到本机对等体)时,它有资格接收事件通知
现在,在您 GridPanel
中,将 draw
方法替换为 paintComponent
并将 getPreferredSize
覆盖为 return 所需的组件大小
class GridPanel extends JPanel {
public static final int HEIGHT = 50;
public static final int WIDTH = 50;
private boolean filled;
private int numberGridsFilled = 0;
private int row, col;
public GridPanel(int row, int col) {
row = row;
col = col;
filled = false;
setBorder(BorderFactory.createLineBorder(Color.GRAY));
setBackground(Color.red);
}
public Dimension getPreferredSize() {
return new Dimenions(WIDTH, HEIGHT);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setPaint(Color.GRAY);
Rectangle2D r = new Rectangle2D.Double(x, y, WIDTH, HEIGHT);
g2d.draw(r);
g2d.dispose();
}