有人能帮我看一下,为什么我在java中画的五子棋的棋盘添加不到窗体上,谢谢
主题框架(由于字数限制下面的代码没有导包)packagecom.qyh.login;publicclassLoginFace{publicstaticvoidmain(S...
主题框架(由于字数限制下面的代码没有导包)
package com.qyh.login;
public class LoginFace{
public static void main(String[] args) {
new BodyFrame();
}
}
class BodyFrame extends JPanel{
private JFrame jf;
private JPanel jp;
private JMenu jm_game;//游戏
private JMenuBar jmb;
private JMenuItem jmi_start;//开始选项
private JMenuItem jmi_end;//结束选项
private JMenuItem jmi_regrat;//悔棋选项
private JMenu jm_xx;//选项
private JMenuItem jmi_help;
private JMenuItem jmi_about;
private ChessBoard chessboard;
public BodyFrame(){
setLogin();
}
public void setLogin(){
jf = new JFrame("五子棋");
jf.setLayout(null);
jf.setSize(850, 650);
jm_game = new JMenu("游戏");
jmi_start = new JMenuItem("开始");
jm_game.add(jmi_start);
jmi_regrat = new JMenuItem("悔棋");
jm_game.add(jmi_regrat);
jmi_end = new JMenuItem("结束");
jm_game.add(jmi_end);
jm_xx = new JMenu("选项");
jmi_help = new JMenuItem("帮助");
jm_xx.add(jmi_help);
jmi_about = new JMenuItem("关于");
jm_xx.add(jmi_about);
jmb = new JMenuBar();
jmb.add(jm_game);
jmb.add(jm_xx);
jf.setJMenuBar(jmb);
chessboard = new ChessBoard();
jf.add(chessboard);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int sw = (int)d.getWidth();
int sh = (int)d.getHeight();
int x =(sw-jf.getWidth())/2;
int y = (sh-jf.getHeight())/2;
jf.setLocation(x, y);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
画棋盘的类
public class ChessBoard extends JPanel implements MouseListener{
public static final int MARGIN = 30;//边距
public static final int GRID_SPAN = 35;//网格间距
public static final int ROWS = 10;//棋盘行数
public static final int COLS = 10;//棋盘列数
Point[] chessList = new Point[(ROWS+1)*(COLS+1)];//棋子初始为空
boolean isBlack = true;//黑棋先下
boolean gameOver = false;//游戏是否结束,默认为结束
int chessCount;//棋子的个数
int xIndex,yIndex;//棋子的位置
public ChessBoard(){
setBackground(Color.ORANGE);
}
//绘制棋盘
public void paintComponent(Graphics g){
super.paintComponent(g);
//画横线
for (int i = 0; i < ROWS; i++) {
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
//画纵线
for (int i = 0; i < COLS; i++) {
g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
package com.qyh.login;
public class LoginFace{
public static void main(String[] args) {
new BodyFrame();
}
}
class BodyFrame extends JPanel{
private JFrame jf;
private JPanel jp;
private JMenu jm_game;//游戏
private JMenuBar jmb;
private JMenuItem jmi_start;//开始选项
private JMenuItem jmi_end;//结束选项
private JMenuItem jmi_regrat;//悔棋选项
private JMenu jm_xx;//选项
private JMenuItem jmi_help;
private JMenuItem jmi_about;
private ChessBoard chessboard;
public BodyFrame(){
setLogin();
}
public void setLogin(){
jf = new JFrame("五子棋");
jf.setLayout(null);
jf.setSize(850, 650);
jm_game = new JMenu("游戏");
jmi_start = new JMenuItem("开始");
jm_game.add(jmi_start);
jmi_regrat = new JMenuItem("悔棋");
jm_game.add(jmi_regrat);
jmi_end = new JMenuItem("结束");
jm_game.add(jmi_end);
jm_xx = new JMenu("选项");
jmi_help = new JMenuItem("帮助");
jm_xx.add(jmi_help);
jmi_about = new JMenuItem("关于");
jm_xx.add(jmi_about);
jmb = new JMenuBar();
jmb.add(jm_game);
jmb.add(jm_xx);
jf.setJMenuBar(jmb);
chessboard = new ChessBoard();
jf.add(chessboard);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int sw = (int)d.getWidth();
int sh = (int)d.getHeight();
int x =(sw-jf.getWidth())/2;
int y = (sh-jf.getHeight())/2;
jf.setLocation(x, y);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
画棋盘的类
public class ChessBoard extends JPanel implements MouseListener{
public static final int MARGIN = 30;//边距
public static final int GRID_SPAN = 35;//网格间距
public static final int ROWS = 10;//棋盘行数
public static final int COLS = 10;//棋盘列数
Point[] chessList = new Point[(ROWS+1)*(COLS+1)];//棋子初始为空
boolean isBlack = true;//黑棋先下
boolean gameOver = false;//游戏是否结束,默认为结束
int chessCount;//棋子的个数
int xIndex,yIndex;//棋子的位置
public ChessBoard(){
setBackground(Color.ORANGE);
}
//绘制棋盘
public void paintComponent(Graphics g){
super.paintComponent(g);
//画横线
for (int i = 0; i < ROWS; i++) {
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
//画纵线
for (int i = 0; i < COLS; i++) {
g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
} 展开
展开全部
前面
jf.setLayout(null);
则所添加的控件,都要设置
chessboard.setBounds(0, 0, jf.getWidth(), jf.getHeight());
可以一次设置,也可以放在doLayout()事件中。
——这是完全自定义的布局。
package fiveInLine;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class BodyFrame extends JPanel {
private JFrame jf;
private JPanel jp;
private JMenu jm_game;// 游戏
private JMenuBar jmb;
private JMenuItem jmi_start;// 开始选项
private JMenuItem jmi_end;// 结束选项
private JMenuItem jmi_regrat;// 悔棋选项
private JMenu jm_xx;// 选项
private JMenuItem jmi_help;
private JMenuItem jmi_about;
private ChessBoard chessboard;
public BodyFrame() {
setLogin();
}
public void setLogin() {
jf = new JFrame("五子棋");
jf.setLayout(null);
jf.setSize(850, 650);
jm_game = new JMenu("游戏");
jmi_start = new JMenuItem("开始");
jm_game.add(jmi_start);
jmi_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("开始........");
}
});
jmi_regrat = new JMenuItem("悔棋");
jm_game.add(jmi_regrat);
jmi_end = new JMenuItem("结束");
jm_game.add(jmi_end);
jmi_end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("结束........");
System.exit(0);
}
});
jm_xx = new JMenu("选项");
jmi_help = new JMenuItem("帮助");
jm_xx.add(jmi_help);
jmi_about = new JMenuItem("关于");
jm_xx.add(jmi_about);
jmb = new JMenuBar();
jmb.add(jm_game);
jmb.add(jm_xx);
jf.setJMenuBar(jmb);
chessboard = new ChessBoard();
chessboard.setBounds(0, 0, jf.getWidth(), jf.getHeight());
jf.getContentPane().add(chessboard);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int sw = (int) d.getWidth();
int sh = (int) d.getHeight();
int x = (sw - jf.getWidth()) / 2;
int y = (sh - jf.getHeight()) / 2;
jf.setLocation(x, y);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
另外,画线时,封闭
// 画横线
for (int i = 0; i <= ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS * GRID_SPAN, MARGIN + i * GRID_SPAN);
}
// 画纵线
for (int i = 0; i <= COLS; i++) {
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + ROWS * GRID_SPAN);
}
jf.setLayout(null);
则所添加的控件,都要设置
chessboard.setBounds(0, 0, jf.getWidth(), jf.getHeight());
可以一次设置,也可以放在doLayout()事件中。
——这是完全自定义的布局。
package fiveInLine;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class BodyFrame extends JPanel {
private JFrame jf;
private JPanel jp;
private JMenu jm_game;// 游戏
private JMenuBar jmb;
private JMenuItem jmi_start;// 开始选项
private JMenuItem jmi_end;// 结束选项
private JMenuItem jmi_regrat;// 悔棋选项
private JMenu jm_xx;// 选项
private JMenuItem jmi_help;
private JMenuItem jmi_about;
private ChessBoard chessboard;
public BodyFrame() {
setLogin();
}
public void setLogin() {
jf = new JFrame("五子棋");
jf.setLayout(null);
jf.setSize(850, 650);
jm_game = new JMenu("游戏");
jmi_start = new JMenuItem("开始");
jm_game.add(jmi_start);
jmi_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("开始........");
}
});
jmi_regrat = new JMenuItem("悔棋");
jm_game.add(jmi_regrat);
jmi_end = new JMenuItem("结束");
jm_game.add(jmi_end);
jmi_end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("结束........");
System.exit(0);
}
});
jm_xx = new JMenu("选项");
jmi_help = new JMenuItem("帮助");
jm_xx.add(jmi_help);
jmi_about = new JMenuItem("关于");
jm_xx.add(jmi_about);
jmb = new JMenuBar();
jmb.add(jm_game);
jmb.add(jm_xx);
jf.setJMenuBar(jmb);
chessboard = new ChessBoard();
chessboard.setBounds(0, 0, jf.getWidth(), jf.getHeight());
jf.getContentPane().add(chessboard);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int sw = (int) d.getWidth();
int sh = (int) d.getHeight();
int x = (sw - jf.getWidth()) / 2;
int y = (sh - jf.getHeight()) / 2;
jf.setLocation(x, y);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
另外,画线时,封闭
// 画横线
for (int i = 0; i <= ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS * GRID_SPAN, MARGIN + i * GRID_SPAN);
}
// 画纵线
for (int i = 0; i <= COLS; i++) {
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + ROWS * GRID_SPAN);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询