纯java代码游戏实现 java设计游戏代码带界面

JAVA能写游戏吗?Java可以写游戏的纯java代码游戏实现,但是一般来说不建议用Java开发游戏纯java代码游戏实现,因为Java这个语言是面向对象的语言对代码进行纯java代码游戏实现了大量的封装来达到模块组件可复用的目的纯java代码游戏实现,这也导致纯java代码游戏实现了它相对于面向过程的C , C这种更偏向于底层的语言来说运行速度是偏慢的,Java更偏向于做服务端方面的工作,做电商网站,公司企业级的后台系统,因为Java语言的特点使其开发的系统具有较高的稳定性和安全性以及最重要的可维护性 。
Java实现的趣味游戏连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols6;cols){
for(int rows = 0;rows5;rows){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols 1][rows 1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins) {
randoms=(int)(Math.random()*25 1);
for(int alike=1;alike=2;alike) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()) 100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i) {
for(int j=0;j=5;j) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i6;i){
for(int j = 0;j5;j){
if(grid[i 1][j 1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsgsecondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去 。仔细分析,不一条条注释
if((x0==x (y0==y 1||y0==y-1)) || ((x0==x 1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y 1;i=j ;i){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x 1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0n==x 1) {
remove();
}
}
}
}
}
for (i=0;i8;i) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x 1;j=i;j) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y 1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0n==y 1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0 1;i=j ;i){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0 1;j=i ;j) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols6;cols){
for(int rows = 0;rows5;rows){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols 1,rows 1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
请采纳答案,支持我一下 。
怎样用JAVA实现扫雷游戏要详细代码?还是只要启动?
java编写实现,代码如下:import Java.awt.*;
import java.awt.event.*;
import javax.Swing.*;
/*按扭类*/
class Bomb extends JButton
{
public int num_x,num_y;//第几号方块
public int BombRoundCount;//周围雷数
public boolean isBomb;//是否为雷
public boolean isClicked;//是否被点击
public int BombFlag;//探雷标记
public boolean isRight;//是否点击右键
public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/*窗口及算法实现类*/
class MainBomb extends JFrame implements ActionListener,MouseListener
{
public JTextField text;
public Label nowBomb,setBomb;
public int BlockNum,BombNum;//当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif");//踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif");//踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif");//雷标记
public Icon icon_question = new ImageIcon("question.gif");//疑惑是否有雷
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;
/*界面设计*/
public MainBomb()
{
super("扫雷Aaron2004制作 2004.8 ");
BlockNum = 64;
BombNum = 10;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text=new JTextField("10 ",3);
nowBomb = new Label("当前雷数" "" BombNum "");
setBomb= new Label("设置地雷数");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum = 10BombNum50 )
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}
}
} );
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"North");
mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i)
{
bombButton[ i ]=new Bomb[ (int)Math.sqrt(BlockNum) ];
}
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i)
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j)
{
bombButton[ i ][ j ]=new Bomb(i,j);
bombButton[ i ][ j ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i)
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j)
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,"Center");
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
}
/*布雷*/
public void startBomb()
{
for(int i=0;iBombNum;i)
{
int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
if(bombButton[ x ][ y ].isBomb==true)
i--;
else
bombButton[ x ][ y ].isBomb=true ;
}
}
/*重新开始*/
public void replay()
{
nowBomb.setText("当前雷数" "" BombNum "");
for(int i = 0 ; i(int)Math.sqrt(BlockNum) ; i)
for(int j = 0 ; j(int)Math.sqrt(BlockNum) ; j)
{
bombButton[ i ][ j ].isBomb=false;
bombButton[ i ][ j ].isClicked=false;
bombButton[ i ][ j ].setEnabled(true);
bombButton[ i ][ j ].setText("");
bombButton[ i ][ j ].setIcon(null);
}
startBomb();
}
/*是否挖完了所有的雷*/
public void isWin()
{
int findBomb=0;//找到的地雷数
for(int i = 0;i(int)Math.sqrt(BlockNum) ; i)
for(int j = 0;j(int)Math.sqrt(BlockNum ); j)
{
if(bombButton[ i ][ j ].isBomb == truebombButton[ i ][ j ].isRight == true)
findBomb;
}
if( findBomb == Integer.parseInt(text.getText().trim()) )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}
/*计算方块周围雷数 */
public void CountRoundBomb()
{
for (int i = 0; i(int)Math.sqrt(BlockNum); i) {
for (int j = 0; j(int)Math.sqrt(BlockNum); j) {
int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[ i ][ j ].isBomb != true) {
if ( (i - 1 = 0)(j - 1 = 0)) {
if (bombButton[i - 1][j - 1].isBomb == true) {
count= 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 = 0)) {
if (bombButton[i - 1][ j ].isBomb == true) {
count= 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 = 0)(j1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i - 1][j1] .isBomb == true) {
count= 1; //检测右上方是否为地雷
}
}
if ( (j - 1 = 0)) {
if (bombButton[ i ][j - 1] .isBomb == true) {
count= 1; //检测左边是否为地雷
}
}
if ( (i = 0)(j1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[ i ][j1].isBomb == true) {
count= 1; //右边
}
}
if ( (j - 1 = 0)(i1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i1][j - 1].isBomb == true) {
count= 1; //左下
}
}
if ( (i1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i1][ j ].isBomb == true) {
count= 1; //下
}
}
if ( (j1 = (int)Math.sqrt(BlockNum)-1)(i1 = Math.sqrt(BlockNum)-1)) {
if (bombButton[i1][j1].isBomb == true) {
count= 1; //右下
}
}
bombButton[ i ][ j ].BombRoundCount = count;
}
}
}
}
/**当选中的位置为空,则翻开周围的地图**/
public void isNull(Bomb[][] bombButton,Bomb ClickecButton)
{
int i,j;
i=ClickecButton.num_x;
j=ClickecButton.num_y;
if (ClickecButton.isBomb==true) {
}
else {
if ( (i - 1 = 0)(j - 1 = 0)) { //检测左上方空格是否是空
if (bombButton[i - 1][j - 1].isBomb == falsebombButton[i - 1][j - 1].isClicked == falsebombButton[i - 1][j - 1].isRight == false) {
bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount) "");
bombButton[i - 1][j - 1].setEnabled(false);
bombButton[i - 1][j - 1].isClicked=true;
}
}
if ( (i - 1 = 0)) { //检测上方空格是否为空
if (bombButton[i - 1][ j ] .isBomb == falsebombButton[i - 1][ j ].isClicked == falsebombButton[i - 1][ j ].isRight == false) {
bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount) "");
bombButton[i - 1][ j ].setEnabled(false);
bombButton[i - 1][ j ].isClicked=true;
}
}
if ( (i - 1 = 0)(j1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空
if (bombButton[i - 1][j1] .isBomb == falsebombButton[i - 1][j1].isClicked == falsebombButton[i - 1][j1].isRight == false) {
bombButton[i - 1][j1].setText((bombButton[i - 1][j1].BombRoundCount) "");
bombButton[i - 1][j1].setEnabled(false);
bombButton[i - 1][j1].isClicked=true;
}
}
if ( (j - 1 = 0)) { //检测左边是否为空
if (bombButton[ i ][j - 1].isBomb == falsebombButton[ i ][j - 1].isClicked == falsebombButton[ i ][j - 1].isRight == false) {
bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount) "");
bombButton[ i ][j - 1].setEnabled(false);
bombButton[ i ][j - 1].isClicked=true;
}
}
if ( (i = 0)(j1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空
if (bombButton[ i ][j1].isBomb == falsebombButton[ i ][j1].isClicked == falsebombButton[ i ][j1].isRight == false) {
bombButton[ i ][j1].setText((bombButton[ i ][j1].BombRoundCount) "");
bombButton[ i ][j1].setEnabled(false);
bombButton[ i ][j1].isClicked=true;
}
}
if ( (j - 1 = 0)(i1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空
if (bombButton[i1][j - 1].isBomb == falsebombButton[i1][j - 1].isClicked == falsebombButton[i1][j - 1].isRight == false) {
bombButton[i1][j - 1].setText((bombButton[i1][j - 1].BombRoundCount) "");
bombButton[i1][j - 1].setEnabled(false);
bombButton[i1][j - 1].isClicked=true;
}
}
if ( (i1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空
if (bombButton[i1][ j ].isBomb == falsebombButton[i1][ j ].isClicked == falsebombButton[i1][ j ].isRight == false) {
bombButton[i1][ j ].setText((bombButton[i1][ j ].BombRoundCount) "");
bombButton[i1][ j ].setEnabled(false);
bombButton[i1][ j ].isClicked=true;
}
}
if ( (j1 = ((int)Math.sqrt(BlockNum)-1) )(i1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空
if (bombButton[i1][j1].isBomb == falsebombButton[i1][j1].isClicked == falsebombButton[i1][j1].isRight == false) {
bombButton[i1][j1].setText((bombButton[i1][j1].BombRoundCount) "");
bombButton[i1][j1].setEnabled(false);
bombButton[i1][j1].isClicked=true;
}
}
if ( (i - 1 = 0)(j - 1 = 0))//检测左上
isNull(bombButton,bombButton[i - 1][j - 1]);
if ( (i - 1 = 0))
isNull( bombButton,bombButton[i - 1][ j ]);//检测上方
if ( (i - 1 = 0)(j1 = (int)Math.sqrt(BlockNum)-1))
isNull( bombButton,bombButton[i - 1][j1]);//检测右上
if ( (j - 1 = 0))
isNull(bombButton,bombButton[i][j - 1]);//检测左边
if ( (i = 0)(j1 = ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i][j1]);//检测右边
if ( (j - 1 = 0)(i1 = ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i1][j - 1]); //检测左下
if ( (i1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测下
isNull(bombButton,bombButton[i1][ j ]);
if ( (j1 = ((int)Math.sqrt(BlockNum)-1))(i1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测右下
isNull(bombButton,bombButton[i1][j1]);
}
}
public void actionPerformed(ActionEvent e)
{
CountRoundBomb();
if(((Bomb)e.getSource()).isBomb==false((Bomb)e.getSource()).isClicked == false)
{
((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount ) "");
((Bomb)e.getSource()).isClicked=true;
((Bomb)e.getSource()).setIcon(null);
((Bomb)e.getSource()).setEnabled(false);
if((((Bomb)e.getSource()).BombRoundCount) == 0)
isNull(bombButton,(Bomb)e.getSource());
isWin();
}
else if(((Bomb)e.getSource()).isBomb == true)
{
for(int i=0;i(int)Math.sqrt(BlockNum);i)
for(int j=0;j(int)Math.sqrt(BlockNum);j)
{
if(bombButton[ i ][ j ].isBomb == true)
bombButton[ i ][ j ].setIcon(icon_bomb);
}
((Bomb)e.getSource()).setIcon(icon_bomb_big);
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();
}
}
public void mouseClicked(MouseEvent e)
{
Bomb bombSource = (Bomb)e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if((right == true)(bombSource.isClicked == false))
{
bombSource.BombFlag = (bombSource.BombFlag1)%3;
if(bombSource.BombFlag == 1)
{
if(BombNum0bombSource.isRight == false ){
bombSource.setIcon(icon_flag);
bombSource.isRight = true;
BombNum--;
}
isWin();
nowBomb.setText("当前雷数" "" BombNum "");
}
else if(bombSource.BombFlag == 2)
{
if( (BombNum !=0 ) ||(BombNum ==0 (bombSource.getIcon()==icon_flag)) )
BombNum;
bombSource.setIcon(icon_question);
nowBomb.setText("当前雷数" "" BombNum "");
}
else if(bombSource.BombFlag == 0)
{
bombSource.setIcon(null);
bombSource.isRight = false;
}
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
}
/*主类*/
public class Main
{
public static void main(String args[])
{
(new MainBomb()).show();
}
}
利用 Java Swing 实现游戏开发Swing初体验 对于想学习Swing编程的朋友 我们特地为大家准备了一些小窍门 首先 下载并阅读代码是极有必要的 由于这是一篇关于Swing的教程 所以 我们只是尽可能讲解一些与Swing有关的内容 与Swing无关的内容一般不会涉及 例如算法部分 其次 受篇幅限制 也不可能在这里将每部分代码都写得完完整整的 所以 大家也需要对照完整代码来看 最后 为了使大家更容易把精力集中在Swing学习上 我们也将游戏开发中所需资源放在下载文件中 大家下载后便能够编译运行 看到执行结果 (下载游戏源文件)顶层容器 什么是顶层容器?当我们使用Java进行图形编程的时候 图在哪里绘制呢?我们需要一个能够提供图形绘制的容器 这个容器就被称为顶层容器 你也可以把它想象成一个窗口 顶层容器是进行图形编程的基础 一切图形化的东西 都必然包括在顶层容器中 在Swing中 我们有三种可以使用的顶层容器 它们分别是:JFrame:用来设计类似于Windows系统中的窗口形式的应用程序 JDialog:和JFrame类似 只不过JDialog是用来设计对话框 JApplet:用来设计可以在嵌入在网页中的Java小程序 如果需要使用Swing制作一个窗口类程序 我们的代码看起来应该是这样:import javax swing *;public class KyodaiUIextends JFrame {……}控件 控件是构成应用程序界面的基本元素 按钮 文本框 进度条等 这些都是控件 控件(这里我们只讨论可视化控件)又可以分为容器控件和非容器控件 从字面意义上来理解 容器控件就是能包含其他控件的特殊控件 例如 Java中的JPanel控件就属于容器型控件 我们可以在JPanel中放置按钮 文本框等非容器控件 你甚至可以在JPanel中再放置若干个JPanel控件(值得注意的是 顶层容器也是容器型控件 每一个窗口应用程序中有且只能有一个顶层容器控件 换句话说 顶层容器不能包括在其他的控件中) Java中的容器控件有很多 除刚才提到的JPanel外 还有JTabbedPane JScrollPane等 非容器控件有JButton JLabel JTextField等 如果你需要向某个容器型的控件中添加控件 你可以使用 add(Component p) 方法来实现 如:JPanel panel = new JPanel();JButton button = new JButton();panel add(button);布局 什么是布局?布局是Java中用来控制控件排列位置的一种界面管理系统 使用过其他可视化编程开发语言的人在初次接触Java界面设计时 总会感觉到Java界面设计很别扭:居然没有提供所见即所得的设置控件坐标的方法!然而 事实证明 Java本身提供的布局管理系统也一样能够出色地完成我们的需要 而且在跨平台时表现得更有优势 常用的布局有:BorderLayout:将界面分割为上下左右以及中间一块区域的管理系统 在BorderLayout布局中 最多你只能放 个控件 如果超过 个控件 建议还是选用其他的布局系统吧 GridLayout:GridLayout是将用户界面切割为棋盘一样的布局管理系统 如果我们要设计一个类似于Windows中自带的计算器软件 GridLayout无疑是最佳选择 FlowLayout:FlowLayout与上述两类布局管理系统不太一样 在FlowLayout中 你不必指定每个控件放在哪 你只需要把控件加入到FlowLayout中 FlowLayout就会根据你添加控件的顺序依次放置控件 如果空间不够 会自动换行 在对这几个布局管理系统有了基本认识后 我们就一起来进入界面设计吧 在仔细观察了QQ游戏中 连连看 的设定后 我们可以发现 整个界面分为三个区 顶部是系统菜单区 占地面积最大的是用户游戏区 另外还有一个用户交互区 每个区域中都由若干控件组成 这么多控件 我们从哪开始入手呢?由于容器控件中可以放置其他控件 因此 我们只需要先确定放置的容器控件就可以了 既然已经知道需要使用容器控件的个数 接下来让我们就进入布局管理系统的选择 用GridLayout?似乎有点勉强 用FlowLayout?还有更好的选择吗?对了 我想你一定想到了是BorderLayout吧 如下图 所示 动手之前 大家一定要注意的是 界面的设计要先考虑好尺寸 不管是主程序界面的大小还是每个区域的大小 如果没有设计好合适的尺寸 将来改动起来会十分痛苦 下面便是相应的源程序:import java awt *;import javax swing *;public class KyodaiUI extends JFrame {public KyodaiUI() {this setSize( ); //将窗体的大小设定为 * this setDefaultCloseOperation(JFrame EXIT_ON_CLOSE);this setResizable(false); //窗体不能改变大小this setTitle( 连连看 ); //设置标题JPanel toolBar = new JPanel();toolBar setBackground(Color white);toolBar setPreferredSize(new Dimension( ));JPanel actionPanel = new JPanel(); //新建JPanel型的控件actionPanel setBackground(Color yellow); //设置背景色actionPanel setPreferredSize(new Dimension( )); //设置大小JPanel contentPanel = new JPanel();contentPanel setBackground(Color blue);contentPanel setPreferredSize(new Dimension( ));this getContentPane() add(toolBar BorderLayout NORTH);this getContentPane() add(actionPanel BorderLayout EAST);this getContentPane() add(contentPanel BorderLayout CENTER);}public static void main(String[] args) throws HeadlessException {KyodaiUI kyodaiUI = new KyodaiUI();kyodaiUI show();}}让我们来看看上面这段程序是如何运行的 首先 extends JFrame表明了这是从JFrame中继承过来的 JFrame是最基本的顶层容器控件 实际上 在JDK中 以字母J打头的控件都是Swing控件 然后设置了容器的属性 其中 setDefaultCloseOperation(JFrame EXIT_ON_CLOSE)是用来告诉Java虚拟机 当用户点击窗体右上角的 关闭 按钮时 关闭该窗口进程 如果不这么做的话 你会发现虽然你可以点将窗口关闭 然而程序却没有退出 在接下来的代码中 我们为顶层容器添加了三个Panel容器 要注意的是 在AWT中 我们可以直接写为add(toolBar BorderLayout NORTH) 而在 Swing 中却一定要写成getContentPane() add(toolBar BorderLayout NORTH) 否则程序就会出错 现在大家可以放在编译运行看看 是不是和我的运行结果一样(见图 )?边框 虽然我们使用了不同前景色来区别不同的区域 然而却没有层次感 加上边框一定会漂亮许多 在Java中 所有以J打头的Swing控件都可以使用setBorder方法来为自己设置边框 边框有很多种 线型 凸起 凹下 空的 你甚至可以自由组合形成个人风格 所有的Border都必须使用javax swing BorderFactory中提供的静态方法来创建 比如:Border border = BorderFactory createBevelBorder(BevelBorder LOWERED new Color( ) new Color( ) new Color( ) new Color( ));现在 我们将toolBar setBackground(Color white)改为toolBar setBorder(border) 立体效果是不是已经出现了?实战??写上自己的大名现在我们已经有了一个能够运行的界面了 虽然它什么也做不了 但是请你别慌 罗马不是一天建成的 现在让我们在菜单区提供一个 关于 菜单 用来显示程序的信息 难道你不想让别人知道你的大名吗?Swing本身就提供了现成的按钮控件JButton 我们只需要创建一个新的按钮:JButton about = new JButton( 关于 );这个按钮该怎么放到菜单区而不是别的地方呢?我们可以加入下面的代码:toolBar add(about);咦 怎么点按钮没有反应?这是因为你还没有告诉程序点击按钮时要做什么事情呢 要为按钮添加事件响应 首先需要使用about addActionListener(this)来告诉程序监听按钮按下时的事件 由于ActionListener是一个程序接口 因此 我们在类的申明的地方也得做一点小小的修改:public class KyodaiUI extends JFrame implements ActionListener { }实现ActionListener接口是为了告诉程序我要进行事件处理了 当然 最后我们得添加响应事件的代码:public void actionPerformed(ActionEvent e) {if (e getSource() == about) {JOptionPane showMessageDialog(this 我的大名 关于 JOptionPane INFORMATION_MESSAGE);return ;}}其中 e getSource() 表示当前触发事件的控件 由于我们的程序中往往会有多个以上的控件 这些控件都有可能产生事件 所以我们必须使用这个方法来找到产生事情的控件小结 让我们一起来回顾一下今天所学的内容:首先我们了解了顶层容器 也知道了控件分为容器控件和非容器控件 同时还知道使用边框 最后 我们还小小的处理了一下按钮的事件 学而时习之 不亦说乎 就让我留点小小的作业 帮助大家巩固一下今天所学的内容:上面我们添加的按钮在菜单栏的中间 并不美观 lishixinzhi/Article/program/Java/hx/201311/25907
求一猜谜小游戏的java实现代码和包一起!这是我自己编的 , 猜4个不重复数字的小游戏 。
代码如下:
package com.entor.one.test2;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void zhuan(int temp,int input[])
{
input[0] = temp / 1000;
temp %= 1000;
input[1] = temp /100;
temp %= 100;
input[2] = temp /10;
temp %= 10;
input[3] = temp;
}
public static boolean if_xiangtong(int num[],int input[]){
//判断输入的数组 与随机生成的4个数字 , 是否完全相同
for(int i=0;i4;i){
if(num[i]!=input[i])
break;
if(i==3){
return true;
}
}
return false;
}
public static void right(int num[]){
System.out.print("\n回答正确,数字是:");
for(int i=0;i4;i)
System.out.print(num[i]);
}
public static void tishi(int num[],int input[]){
int numA=0;//2个数组中数字相同且位置相同的 数字数量
int numB=0;//2个数组中数字相同但位子不同的数字数量
//判断输入的数组 与随机生成的4个数字中,数字相同且位置相同数字数量
for(int i=0;i4;i)
if(num[i]==input[i])
numA;
//判断输入的数组 与随机生成的4个数字中,数字相同且位置相同数字数量
for(int i=0;i4;i)
for(int j=0;j4;j)
if(num[i]==input[j]i!=j)
numB;
System.out.println("回答错误,数字提示是:" numA "A" numB "B");
numA = numB = 0;
}
public static void main(String arg[]){
int num[] =new int[4];
int temp;
int input[] =new int[4];
Scanner s = new Scanner(System.in);
Random random = new Random();
boolean boo ;
while(true){
//随机生成4个不重复的数字
for(int i=0;i4;i)
{
num[i]=random.nextInt(9);
for(int j=i-1;j=0;j--)
while(num[j]==num[i])
num[i]= random.nextInt(9);
}
boo = false;
while(!boo){
System.out.print("\n请输入4个数字:");
temp = s.nextInt();
zhuan(temp,input);//将输入的数字转化为数组保存
boo = if_xiangtong(num,input);//判断输入的数组 与随机生成的4个数字,是否完全相同
if(boo)
right(num);//回答正确,输出结果
else
tishi(num,input); //回答错误 , 给予提示
}
}
}
}
【纯java代码游戏实现 java设计游戏代码带界面】纯java代码游戏实现的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java设计游戏代码带界面、纯java代码游戏实现的信息别忘了在本站进行查找喔 。

    推荐阅读