
这个程序实现还要一个关联文件夹“image”象棋棋子 部分代码如下:(代码太长 给个邮箱 我发给你)
import javaawt;
import javaawtevent;
import javaxswing;
import javautil;
import javaio;
//主类
public class Chess{
public static void main(String args[]){
new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");
}
}
//主框架类
class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{
//玩家
JLabel play[] = new JLabel[32];
//棋盘
JLabel image;
//窗格
Container con;
//工具栏
JToolBar jmain;
//重新开始
JButton anew;
//悔棋
JButton repent;
//退出
JButton exit;
//当前信息
JLabel text;
//保存当前 *** 作
Vector Var;
//规则类对象(使于调用方法)
ChessRule rule;
/
单击棋子
chessManClick = true 闪烁棋子 并给线程响应
chessManClick = false 吃棋子 停止闪烁 并给线程响应
/
boolean chessManClick;
/
控制玩家走棋
chessPlayClick=1 黑棋走棋
chessPlayClick=2 红棋走棋 默认红棋
chessPlayClick=3 双方都不能走棋
/
int chessPlayClick=2;
//控制棋子闪烁的线程
Thread tmain;
//把第一次的单击棋子给线程响应
static int Man,i;
ChessMainFrame(){
new ChessMainFrame("中国象棋");
}
/
构造函数
初始化图形用户界面
/
ChessMainFrame(String Title){
//获行客格引用
con = thisgetContentPane();
consetLayout(null);
//实例化规则类
rule = new ChessRule();
Var = new Vector();
//创建工具栏
jmain = new JToolBar();
text = new JLabel("欢迎使用象棋对弈系统");
//当鼠标放上显示信息
textsetToolTipText("信息提示");
anew = new JButton(" 新 游 戏 ");
anewsetToolTipText("重新开始新的一局");
exit = new JButton(" 退 出 ");
exitsetToolTipText("退出象棋程序程序");
repent = new JButton(" 悔 棋 ");
repentsetToolTipText("返回到上次走棋的位置");
//把组件添加到工具栏
jmainsetLayout(new GridLayout(0,4));
jmainadd(anew);
jmainadd(repent);
jmainadd(exit);
jmainadd(text);
jmainsetBounds(0,0,558,30);
conadd(jmain);
//添加棋子标签
drawChessMan();
//注册按扭监听
anewaddActionListener(this);
repentaddActionListener(this);
exitaddActionListener(this);
//注册棋子移动监听
for (int i=0;i<32;i++){
conadd(play[i]);
play[i]addMouseListener(this);
}
//添加棋盘标签
conadd(image = new JLabel(new ImageIcon("image\\MainGIF")));
imagesetBounds(0,30,558,620);
imageaddMouseListener(this);
//注册窗体关闭监听
thisaddWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we){
Systemexit(0);
}
}
);
//窗体居中
Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();
Dimension frameSize = thisgetSize();
if (frameSizeheight > screenSizeheight){
frameSizeheight = screenSizeheight;
}
if (frameSizewidth > screenSizewidth){
frameSizewidth = screenSizewidth;
}
thissetLocation((screenSizewidth - frameSizewidth) / 2 - 280 ,(screenSizeheight - frameSizeheight ) / 2 - 350);
//设置
thissetIconImage(new ImageIcon("image\\红将GIF")getImage());
thissetResizable(false);
thissetTitle(Title);
thissetSize(558,670);
thisshow();
}
/
添加棋子方法
/
public void drawChessMan(){
//流程控制
int i,k;
//图标
Icon in;
//黑色棋子
//车
in = new ImageIcon("image\\黑车GIF");
for (i=0,k=24;i<2;i++,k+=456){
play[i] = new JLabel(in);
play[i]setBounds(k,56,55,55);
play[i]setName("车1");
}
//马
in = new ImageIcon("image\\黑马GIF");
for (i=4,k=81;i<6;i++,k+=342){
play[i] = new JLabel(in);
play[i]setBounds(k,56,55,55);
play[i]setName("马1");
}
//相
in = new ImageIcon("image\\黑象GIF");
for (i=8,k=138;i<10;i++,k+=228){
play[i] = new JLabel(in);
play[i]setBounds(k,56,55,55);
play[i]setName("象1");
}
//士
in = new ImageIcon("image\\黑士GIF");
for (i=12,k=195;i<14;i++,k+=114){
play[i] = new JLabel(in);
play[i]setBounds(k,56,55,55);
play[i]setName("士1");
}
//卒
in = new ImageIcon("image\\黑卒GIF");
for (i=16,k=24;i<21;i++,k+=114){
play[i] = new JLabel(in);
play[i]setBounds(k,227,55,55);
play[i]setName("卒1" + i);
}
//炮
in = new ImageIcon("image\\黑炮GIF");
for (i=26,k=81;i<28;i++,k+=342){
play[i] = new JLabel(in);
play[i]setBounds(k,170,55,55);
play[i]setName("炮1" + i);
}
//将
in = new ImageIcon("image\\黑将GIF");
play[30] = new JLabel(in);
play[30]setBounds(252,56,55,55);
play[30]setName("将1");
//红色棋子
//车
in = new ImageIcon("image\\红车GIF");
for (i=2,k=24;i<4;i++,k+=456){
play[i] = new JLabel(in);
play[i]setBounds(k,569,55,55);
play[i]setName("车2");
}
//马
in = new ImageIcon("image\\红马GIF");
for (i=6,k=81;i<8;i++,k+=342){
play[i] = new JLabel(in);
play[i]setBounds(k,569,55,55);
play[i]setName("马2");
}
//相
in = new ImageIcon("image\\红象GIF");
for (i=10,k=138;i<12;i++,k+=228){
play[i] = new JLabel(in);
play[i]setBounds(k,569,55,55);
play[i]setName("象2");
}
//士
in = new ImageIcon("image\\红士GIF");
for (i=14,k=195;i<16;i++,k+=114){
play[i] = new JLabel(in);
play[i]setBounds(k,569,55,55);
play[i]setName("士2");
}
//兵
in = new ImageIcon("image\\红卒GIF");
for (i=21,k=24;i<26;i++,k+=114){
play[i] = new JLabel(in);
play[i]setBounds(k,398,55,55);
play[i]setName("卒2" + i);
}
//炮
in = new ImageIcon("image\\红炮GIF");
for (i=28,k=81;i<30;i++,k+=342){
play[i] = new JLabel(in);
play[i]setBounds(k,455,55,55);
play[i]setName("炮2" + i);
}
//帅
in = new ImageIcon("image\\红将GIF");
play[31] = new JLabel(in);
play[31]setBounds(252,569,55,55);
play[31]setName("帅2");
}
/
线程方法控制棋子闪烁
/
}
public class Test {
public static void main(String[] args)throws WeightException{
WeigthCheck wc = new WeigthCheck();
float f = FloatparseFloat(args[0]);
if(wccheck(f)){
Systemoutprintln("您可以参加游戏。");
}else{
throw new WeightException("你不能参加游戏");
}
}
}
public class WeigthCheck{
public boolean check(float f){
boolean flag = false;
if(40 < f && f < 100){
flag = true;
}
return flag;
}
}
public class WeightException extends Exception{
public WeightException(){
super();
}
public WeightException(String s){
super(s);
}
}
具体代码如下:
import javaxswing;
import javaawtevent;
import javaawt;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器10:JAVA版");
jfaddWindowListener(new WindowAdapter(){
public void windowClosing(){
Systemexit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtfsetEditable(false);
String str="123+456-7890=/";
for(int i=0;i<allButtonslength;i++){
allButtons[i]=new JButton(strsubstring(i,i+1));
}
}
public void init(){
//完成布局
jfsetLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanelsetLayout(new FlowLayout());
centerPanelsetLayout(new GridLayout(4,4));
southPanelsetLayout(new FlowLayout());
northPaneladd(jtf);
for(int i=0;i<16;i++){
centerPaneladd(allButtons[i]);
}
southPaneladd(clearButton);
jfadd(northPanel,BorderLayoutNORTH);
jfadd(centerPanel,BorderLayoutCENTER);
jfadd(southPanel,BorderLayoutSOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtfaddActionListener(this);
for(int i=0;i<allButtonslength;i++){
allButtons[i]addActionListener(this);
}
clearButtonaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CalculatorthisjtfsetText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=egetActionCommand();
if(action=="+"||action=="-"||action==""||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",FontBOLD,24);
jtfsetFont(f);
jtfsetBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i<16;i++){
allButtons[i]setFont(f);
allButtons[i]setForeground(ColorRED);
}
}
public void showMe(){
init();
setFontAndColor();
jfpack();
jfsetVisible(true);
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator()showMe();
}
}
public class Test {
public static void main(String[] args) {
Trapezoid t = new Trapezoid(3, 4, 5);
Systemoutprintln(tarea());
}
}
class Trapezoid {
private double topline;
private double baseline;
private double height;
public Trapezoid() {
}
public Trapezoid(double topline, double baseline, double height) {
thistopline = topline;
thisbaseline = baseline;
thisheight = height;
}
public double area() {
return ((topline + baseline) height) / 2;
}
}
package cncommiccjavatwo; //运行时根据自身情况更改
import javautilScanner;
public class AskAnwser {
public static void getMultiple() {
Systemoutprintln("现在进入乘法联系");
int a, b, answer, result;
int count = 0;
for(int i = 0; i < 10; ++i)
{
a =(int)(Mathrandom() 10);
b =(int)(Mathrandom() 10);
answer = a b;
Systemoutprintln("a = " + a + ", b = " + b + ", 请输入答案");
Scanner in = new Scanner(Systemin);
result = innextInt();
if(result == answer)
count++;
}
Systemoutprintln("答题完毕");
if(count >= 6)
Systemoutprintln("恭喜,对了" + count + "题");
else
Systemoutprintln("不及格, 对了" + count + "题");
startPro();
}
public static void getDivide() {
Systemoutprintln("现在进入除法联系");
int a, b, answer1, answer2, result1, result2;
int count = 0;
for(int i = 0; i < 10; ++i)
{
a =(int)(Mathrandom() 10);
b =(int)(Mathrandom() 10);
answer1 = a / b;
answer2 = a % b;
Systemoutprintln("a = " + a + ", b = " + b + ", 请输入答案商和余数,中间加一个空格,比如(1 2)");
Scanner in = new Scanner(Systemin);
result1 = innextInt();
result2 = innextInt();
if(result1 == answer1 && result2 == answer2)
count++;
}
Systemoutprintln("答题完毕");
if(count >= 6)
Systemoutprintln("恭喜,对了" + count + "题");
else
Systemoutprintln("不及格, 对了" + count + "题");
startPro();
}
public static void startPro() {
int count = 1;
while(count == 1){
Systemoutprintln("请选择您要进行的题目类型(输入1或2):1乘法 2除法");
Scanner in = new Scanner(Systemin);
int temp = innextInt();
if(temp == 1)
getMultiple();
else if(temp == 2)
getDivide();
else {
Systemoutprintln("输入错误");
startPro();
}
}
}
public static void main(String[] args) {
startPro();
}
}
以上就是关于求java 象棋小程序 一小段代码全部的内容,包括:求java 象棋小程序 一小段代码、java小程序、如何用JAVA语言编写计算器小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)