2 u012254404 u012254404 于 2014.07.24 22:15 提问

我想实现一个五子棋棋盘,但现在无法做到点击鼠标就可以落子!求助!

package frame;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
public class WuFrame {
public static void main(String[] args) {
new MyFrame().launchFrame();
}
}
class MyFrame extends Frame{
ArrayList pointsArrayList=null;
public void launchFrame() {
// TODO Auto-generated constructor stub
setLayout(new BorderLayout());
setSize(700, 700);
Panel panelLeft=new Panel();
panelLeft.setBackground(Color.WHITE);
Button button=new Button("Start");
panelLeft.add(button);
add(panelLeft, BorderLayout.WEST);
PanelRight panelRight=new PanelRight();
panelRight.setBackground(Color.WHITE);
add(panelRight);
setVisible(true);
this.addMouseListener( new MouseMonitor());
}
public void addPoint(Point point) {
pointsArrayList.add(point);
}
class PanelRight extends Panel{
/**
* 把一个frame分成左右两个panel,右边的panel有paint方法,可以用来画出一个15*15的表格以及反应鼠标的点击事件
*/
private Graphics graphics=this.getGraphics();
public void paint(Graphics g) {
Color color=g.getColor();

Point squarelocation=new Point(0,0);
g.setColor(Color.BLACK);
//g.drawRect(squarelocation.x, squarelocation.y, 40, 40);
for(int i=0;i<14;i++){
for (int j = 0; j <14; j++) {
g.drawRect(squarelocation.x+i*40, squarelocation.y+j*40, 40, 40);
}
}
}
}
class MouseMonitor extends MouseAdapter{
public void MousePressed(MouseEvent event) {
MyFrame frame=(MyFrame)event.getSource();
frame.addPoint(new Point(event.getX(),event.getY()));
frame.repaint();
PanelRight panelRight=new PanelRight();
panelRight.graphics.setColor(Color.black);
panelRight.graphics.fillOval(event.getX(), event.getY(), 20, 20);
}

}

}

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.20 11:15
Csdn user default icon
上传中...
上传图片
插入图片