当前位置: 代码迷 >> Java相关 >> [原创]好久没来了,来顶下
  详细解决方案

[原创]好久没来了,来顶下

热度:234   发布时间:2006-08-09 12:27:41.0
[原创]好久没来了,来顶下

数字华容道

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hrdao extends JFrame implements ActionListener
{JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("文件(F)");
JMenuItem kaishi = new JMenuItem("开始",'K');
JMenuItem exit = new JMenuItem("退出",'E');

private static JButton[] Button1;
private GridLayout layout1;
private static Container cp;
private JPanel panel;
private static JLabel win; //用来显示胜利信息.
private static int i,weizhiP,Gesu,width,height;
public Hrdao(int ROW,int COL)
{
super("数字华容道");
super.setJMenuBar(menubar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gesu=ROW*COL;
menu1.add(kaishi);
menu1.add(exit);
menu1.setMnemonic('F');
menubar.add(menu1);
kaishi.addActionListener(this);
exit.addActionListener(this);
layout1=new GridLayout(ROW,COL);
Button1=new JButton[Gesu];
cp=getContentPane();
panel=new JPanel(layout1);
win=new JLabel("你真棒!");
win.setFont(new Font("仿宋",Font.BOLD,14));
for (i=0 ;i<Gesu;i++)
{ if (i==Gesu-1) Button1[i]=new JButton(" ");
else Button1[i]=new JButton(String.valueOf(i+1));
Button1[i].addActionListener(this);
panel.add(Button1[i]);
}
weizhiP=Gesu-1;
cp.add(panel,"Center");

}

public void actionPerformed(ActionEvent e1)
{ if (e1.getSource()==kaishi) {Start();return;}
if (e1.getSource()==exit) {System.exit(0);return;}
for (i=0 ;i<Gesu;i++)
{ if (e1.getSource()==Button1[i])
{ if (!Button1[i].getLabel().equals(" "))
{if ((Button1[weizhiP].getX()==Button1[i].getX() || Button1[weizhiP].getY()==Button1[i].getY()) && (Button1[weizhiP].getX()+width==Button1[i].getX() || Button1[weizhiP].getX()-width==Button1[i].getX() || Button1[weizhiP].getY()+height==Button1[i].getY() || Button1[weizhiP].getY()-height==Button1[i].getY()))
{Button1[weizhiP].setLabel(Button1[i].getLabel());Button1[i].setLabel(" ");weizhiP=i;break;}
}
}
}
win();
}

void win()
{ for(i=0;i<Gesu-1;i++)
if (Integer.parseInt(Button1[i].getLabel())!=(i+1)) break;
if (i==Gesu-1) System.out.println("你真棒!");
}


void Start()
{int tmp=(int)(Math.random()*(Gesu-1))+1,p=0,tmpB;
Button1[0].setLabel(String.valueOf(tmp));
while(p<=(Gesu-3))
{ tmp=(int)(Math.random()*(Gesu-1))+1;
for(i=0;i<=p;i++)
{tmpB=Integer.parseInt(Button1[i].getLabel());
if (tmpB==tmp) break;
}
if (i>p) {Button1[i].setLabel(String.valueOf(tmp));p++;}
}
Button1[Gesu-1].setLabel(" ");
weizhiP=Gesu-1;
}

public static void main(String[] args)
{
Hrdao hrd=new Hrdao(4,4); //设置长宽
hrd.setVisible(true);
hrd.setLocation(320,250);
hrd.setResizable(false);
hrd.pack();
width=Button1[0].getWidth();height=Button1[0].getHeight();
win.setLocation(hrd.getWidth()/2,hrd.getHeight()/2);
}
}

搜索更多相关的解决方案: private  import  JMenuItem  awt  

----------------解决方案--------------------------------------------------------

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class PinTuha extends JFrame implements ActionListener
{JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("文件(F)");
JMenuItem kaishi = new JMenuItem("开始",'K');

static JButton button1[] = new JButton[28];
int img[]=new int[28];
JLabel label1,label2;
int i;
static int width,height;
int place[]={25,26,27}; //存放空位的位置.


public PinTuha(int width,int height)
{ super("轻松拼图");
super.setJMenuBar(menubar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu1.add(kaishi);
menu1.setMnemonic('F');
menubar.add(menu1);
kaishi.addActionListener(this);
Container c = getContentPane();
c.setLayout(null);

for(i=0;i<28;i++)
{if (i>=25) img[i]=25; else img[i]=i;
button1[i]=new JButton(new ImageIcon(img[i]+".jpg"));
button1[i].setSize(width,height);
button1[i].setLocation(0+i%5*width,0+i/5*height);
button1[i].setVisible(true);
button1[i].addActionListener(this);
c.add(button1[i]);
}

label1=new JLabel(new ImageIcon("pig.jpg"));
label1.setSize(250,200);
label1.setLocation(300,0);
label1.setVisible(true);
c.add(label1);
label2=new JLabel("加油,努力哈!");
label2.setSize(50,100);
label2.setLocation(180,215);
label2.setVisible(true);
c.add(label2);
setSize(555,295);
setVisible(true);


}

void Start()
{int p=0,i=0,tmp;
while(p!=25)
{tmp=(int)(Math.random()*25);
for(i=0;i<p;i++)
{int tmpB=img[i];
if (tmpB==tmp) break;
}
if (i==p) {img[i]=tmp; button1[i].setIcon(new ImageIcon(img[i]+".jpg")); p++;}
}
for(int j=0;j<place.length;j++)
{place[j]=25+j;
img[place[j]]=25; button1[place[j]].setIcon(new ImageIcon(img[place[j]]+".jpg"));
}
}

public void actionPerformed(ActionEvent e1)
{if (e1.getSource()==kaishi) {Start(); return;}
out:for (i=0 ;i<28;i++)
{ if (e1.getSource()==button1[i])
{ if (img[i]!=25)
{for(int j=0;j<place.length;j++)
{if ((button1[place[j]].getX()==button1[i].getX() || button1[place[j]].getY()==button1[i].getY()) && (button1[place[j]].getX()+width==button1[i].getX() || button1[place[j]].getX()-width==button1[i].getX() || button1[place[j]].getY()+height==button1[i].getY() || button1[place[j]].getY()-height==button1[i].getY()))
{img[place[j]]=img[i]; img[i]=25;
button1[place[j]].setIcon(new ImageIcon(img[place[j]]+".jpg")); button1[i].setIcon(new ImageIcon(img[i]+".jpg"));
place[j]=i; break out;
}
}
}
}
}
win();
}

void win()
{ for(i=0;i<25;i++)
if (img[i]!=i) break;
if (i==25) {label2.setText("哇,你太棒了!");}
else {label2.setText("加油,\n努力哈!");}
}

public static void main(String[] args)
{PinTuha f = new PinTuha(50,40);
width=button1[0].getWidth();height=button1[0].getHeight();
}
}

根据上面那个原理做的拼图游戏
有3个小猪可以移动
[attach]10630[/attach]

[此贴子已经被作者于2006-8-9 12:32:41编辑过]


----------------解决方案--------------------------------------------------------
收藏ing。。。。
----------------解决方案--------------------------------------------------------
强悍,顶!!!!!!!!
----------------解决方案--------------------------------------------------------
唉 代码我现在怎么就看不进去呢

----------------解决方案--------------------------------------------------------
还没看呢,
但是顶了
----------------解决方案--------------------------------------------------------
谁会用Java编写会弹跳的小球
----------------解决方案--------------------------------------------------------

大家帮帮忙


----------------解决方案--------------------------------------------------------

大家来帮帮我
项目名称:会弹跳的小球
a) 设计内容:编一个简单的小游戏:会弹跳的小球,界面如下:
b) 设计要求:每点击一次start按钮,就产生一个黑色小球,小球个数不受限制。然后小球开始随机运动,当第一个小球运动到某一预先设定的点时便静止不动,其余小球均在运动到该点时消失。点击close按钮,退出游戏。
用Java做
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
  相关解决方案