修改Board类,增加绘图方法
public void paint(Graphics g)
{
super.paint(g);
repaint();
}
修改后的Board类,见下文:
package com.fivechess.view;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Board extends JFrame {
public Board() {
setLayout(null); //取消原来布局
setBounds(300,50,800,700);
//init1();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setTitle(“雷雷的五子棋”);
}
/**
* 绘图
* @param g
*/
public void paint(Graphics g)
{
super.paint(g);
repaint();
}
}
============================================================
//汇制棋盘的父类
package com.fivechess.view;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
/*
-
汇制棋盘的代码
-
*/
public class ChessBoard2 extends JPanel {public Image chessBoardImage;
public ChessBoard2() {
chessBoardImage = Toolkit.getDefaultToolkit().getImage(“images/chessBoard.jpg”);
}//汇制棋盘
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(chessBoardImage, 0, 0,null);
}
}
=================================================================
//汇制棋盘的类
package com.fivechess.view;
public class PCChessBoard2 extends ChessBoard2{
}
=================================================================
修改PCBoard 类
package com.fivechess.view;
public class PCBoard extends Board {
private PCChessBoard2 pcchessboard2;//棋盘
public PCBoard() {
init();
}
public void init() {
pcchessboard2=new PCChessBoard2();
pcchessboard2.setBounds(200, 50, 600, 700);
pcchessboard2.setVisible(true);
add(pcchessboard2);
}
}
再次运行,进入后的界面,如下图: