我正在尝试制作一款黑白棋游戏,而我正处于初级阶段,试图弄清楚如何让一个方块响应点击事件。到目前为止,这是我的代码。
要清楚,此刻,我只是想改变网格中单击面板的背景颜色。我还在学习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){
}
}
答案 0 :(得分:4)
首先,我不会panels[row][col].draw(g2);
paintComponent
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
并覆盖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();
}
以返回所需的组件大小
{{1}}
答案 1 :(得分:3)
...试图找出如何使用方格响应点击事件。
将方块设为(可能未修饰)JButton
而不是JPanel
。为按钮添加图标和/或文本,然后添加ActionListener
。
带有动作侦听器的按钮将响应鼠标和键盘事件。
E.G。如this answer至Add a complex image in the panel, with buttons around it in one customized user interface中所示。