我正在尝试使用以下代码制作井字游戏板。我不明白什么
button.addActionListener(this);
正在做什么以及为什么this
在那里使用。
package guicollection;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawTicTacToe extends GUIcollection
implements ActionListener {
private JButton button;
private JPanel pane1;
void drawTicTacToe() {
DrawTicTacToe frame = new DrawTicTacToe();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
pane1 = new JPanel();
pane1.setPreferredSize(new Dimension(300, 200));
pane1.setBackground(Color.yellow);
window.add(pane1);
button = new JButton("Prss me");
window.add(button);
**button.addActionListener(this);**
}
public void actionPerformed(ActionEvent event) {
Graphics paper = pane1.getGraphics();
paper.drawLine(90, 30, 90, 120);
paper.drawLine(135, 30, 135, 120);
paper.drawLine(60, 60, 165, 60);
paper.drawLine(60, 90, 165, 90);
}
}
为什么我不能this
用 actionPerformed 替换?