麻烦帮我补一下java代码!关于棋盘的,不用图形界面
privateintboardSize;privateString[][]board;publicChessBoard(intsize){boardSize=size;}...
private int boardSize;
private String[][] board;
public ChessBoard(int size){
boardSize=size;
}
/**
* 初始化棋盘
*/
public void initBoard(){
}
初始化棋盘要怎么写? 展开
private String[][] board;
public ChessBoard(int size){
boardSize=size;
}
/**
* 初始化棋盘
*/
public void initBoard(){
}
初始化棋盘要怎么写? 展开
1个回答
推荐于2016-10-20
展开全部
前面
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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询