java游戏界面制作_求助:一个java小游戏编程实例,主要的界面做出来,但是游戏的中按钮怎么走得方法不知道怎么写。...

这篇博客介绍了一个Java小游戏的界面制作,通过Swing库创建了多个按钮,并设置了按钮的图标和监听事件。文章重点在于按钮的事件处理,当按钮被点击时,如何实现按钮间的交互逻辑,但并未详细说明如何实现按钮的移动效果。
摘要由CSDN通过智能技术生成

展开全部

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Game1 extends javax.swing.JFrame {

public Game1() {

initComponents();

}

private void initComponents() {

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jButton5 = new javax.swing.JButton();

jButton6 = new javax.swing.JButton();

jButton7 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));

jButton1.setText("jButton1");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));

jButton2.setText("jButton2");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

jButton3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg"));

jButton3.setText("jButton3");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

}

});

jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2.jpg"));

jButton4.setText("jButton4");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

}

});

jButton5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N

jButton5.setText("jButton5");

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton5ActionPerformed(evt);

}

});

jButton6.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N

jButton6.setText("jButton6");

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton6ActionPerformed(evt);

}

});

jButton7.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg")); // NOI18N

jButton7.setText(" ");

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton7ActionPerformed(evt);

}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(132, 132, 132)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jButton6, 0, 0, Short.MAX_VALUE)

.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 69, Short.MAX_VALUE))

.addContainerGap(199, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(101, 101, 101)

.addComponent(jButton7)

.addGap(29, 29, 29)

.addComponent(jButton6)

.addGap(30, 30, 30)

.addComponent(jButton5)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton4)

.addGap(18, 18, 18)

.addComponent(jButton3)

.addGap(18, 18, 18)

.addComponent(jButton2)

.addGap(18, 18, 18)

.addComponent(jButton1))

);

pack();

}//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if(a==0)

{

if(a!=b)

{

e69da5e887aa3231313335323631343130323136353331333264663766if(b==2)

{

jButton2.setIcon(f1);

jButton1.setIcon(f3);

a=2;

b=0;

}

else

{

if(c==2)

{

jButton3.setIcon(f1);

jButton1.setIcon(f3);

c=0;

a=2;

}

}

}

}

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

if(b==0)

{

if(b!=c)

{

if(c==2)

{

jButton3.setIcon(f1);

jButton2.setIcon(f3);

b=2;

c=0;

}

else

{

if(d==2)

{

jButton4.setIcon(f1);

jButton2.setIcon(f3);

d=0;

b=2;

}

}

}

}

if(b==1)

{

if(a==2)

{

jButton1.setIcon(f2);

jButton2.setIcon(f3);

b=2;

a=1;

}

}

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

if(c==0)

{

if(c!=d)

{

if(d==2)

{

jButton4.setIcon(f1);

jButton3.setIcon(f3);

c=2;

d=0;

}

else

{

if(e==2)

{

jButton5.setIcon(f1);

jButton3.setIcon(f3);

e=0;

c=2;

}

}

}

}

if(c==1)

{

if(b==0&&a==2)

{

jButton1.setIcon(f2);

jButton3.setIcon(f3);

c=2;

a=1;

}

if(b==2)

{

jButton2.setIcon(f2);

jButton3.setIcon(f3);

c=2;

b=1;

}

}

}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

if(d==0)

{

if(d!=e)

{

if(e==2)

{

jButton5.setIcon(f1);

jButton4.setIcon(f3);

d=2;

e=0;

}

else

{

if(f==2)

{

jButton6.setIcon(f1);

jButton4.setIcon(f3);

f=0;

d=2;

}

}

}

}

if(d==1)

{

if(c==0&&b==2)

{

jButton2.setIcon(f2);

jButton4.setIcon(f3);

d=2;

b=1;

}

if(c==2)

{

jButton3.setIcon(f2);

jButton4.setIcon(f3);

d=2;

c=1;

}

}

}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

if(e==0)

{

if(e!=f)

{

if(f==2)

{

jButton6.setIcon(f1);

jButton5.setIcon(f3);

e=2;

f=0;

}

else

{

if(g==2)

{

jButton7.setIcon(f1);

jButton5.setIcon(f3);

g=0;

e=2;

}

}

}

}

if(e==1)

{

if(d==0&&c==2)

{

jButton3.setIcon(f2);

jButton5.setIcon(f3);

e=2;

c=1;

}

if(d==2)

{

jButton4.setIcon(f2);

jButton5.setIcon(f3);

e=2;

d=1;

}

}}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

if(f==0&&g==2)

{

jButton7.setIcon(f1);

jButton6.setIcon(f3);

f=2;

g=0;

}

if(f==1&&e==2)

{

jButton5.setIcon(f2);

jButton6.setIcon(f3);

f=2;

e=1;

}

if(f==1&&e==0&&d==2)

{

jButton4.setIcon(f2);

jButton6.setIcon(f3);

d=1;

f=2;

}

}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {

if(g==1&&f==2)

{

jButton6.setIcon(f2);

jButton7.setIcon(f3);

g=2;

f=1;

}

if(g==1&&f==0&&e==2)

{

jButton5.setIcon(f2);

jButton7.setIcon(f3);

g=2;

e=1;

}

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Game1().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton5;

private javax.swing.JButton jButton6;

private javax.swing.JButton jButton7;

// End of variables declaration

int a=0;int b=0;int c=0;int d=2;int e=1;int f=1;int g=1;

String s="C:\\Users\\Administrator\\Desktop\\游戏.jpg";

String s1="C:\\Users\\Administrator\\Desktop\\游戏1.jpg";

String s2="C:\\Users\\Administrator\\Desktop\\游戏2.jpg";

ImageIcon f1=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏.jpg");

ImageIcon f2=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏1.jpg");

ImageIcon f3=new ImageIcon("C:\\Users\\Administrator\\Desktop\\游戏2.jpg");

}//我对“点击按钮要隔一个按钮移,必须两个相邻按钮箭头相对”的理解是两个箭头相对的按钮后要有空位才可以移动,不知道是不是对的,而且我不会把按钮做成圆形,你可以用你的界面,如果我理解不对,里面的方法思想应该可以解决移动问题,还有,向下的按钮图案我存为

游戏.jpg;向上的我存为游戏1.jpg;空位按钮我存为游戏2.jpg

更多追问追答

追问

非常感谢!

我得慢慢看看,还有能不能把按钮的图片发给我

追答

那按钮图片就是你发的图我用截图工具截下来的图片

追问

哦,那谢谢,不过我自己的思路跟你得有点不一样,看你得有点看不懂。

点击按钮要隔一个按钮移,必须两个相邻按钮箭头相对”的理解是两个箭头相对的按钮后要有空位才可以移动,你得理解是对的,我没有说清楚。

这个步骤,我的思路是怎样判断它相邻的按钮的方向是与之相反的,然后在隔一个跳。我自己写得代码可以给你。

本回答被提问者采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值