急求Java小游戏代码注释!!!!!
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Test
{
public static void main(String[] args)
{
App ap=new App();
ap.show();
}
}
class App extends JFrame
{
MainPanel mp;
public App()
{
mp=new MainPanel();
this.getContentPane().add(mp);
this.setSize(400,450);
this.setTitle("小游戏");
}
}
class MainPanel extends JPanel
{
ButtonPanel bp=new ButtonPanel();
CtrlPanel rp=new CtrlPanel();
public MainPanel()
{
this.setLayout(new BorderLayout());
rp.btnstart.addActionListener(new StartListener());
this.add(bp,"Center");
this.add(rp,"South");
}
class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="重新开始")
{
bp.ColorInit();
}
}
}
}
class ButtonPanel extends JPanel
{
JButton[][] b=new JButton[5][5];
public ButtonPanel()
{
this.setLayout(new GridLayout(5,5));
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
b[i][j]=new JButton();
b[i][j].setActionCommand(""+(i+1)+(j+1));
b[i][j].addActionListener(new MyButtonListener());
this.add(b[i][j]);
}
}
this.ColorInit();
}
public void ColorInit()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
b[i][j].setBackground(Color.DARK_GRAY);
}
}
}
class MyButtonListener implements ActionListener
{
int r,c;
public void actionPerformed(ActionEvent e)
{
int i=Integer.parseInt(e.getActionCommand());
r=i/10-1;
c=i%10-1;
this.changer();
}
public void btnChange(JButton b)
{
if(b.getBackground()==Color.DARK_GRAY)
{
b.setBackground(Color.pink);
}
else
{
b.setBackground(Color.DARK_GRAY);
}
}
public void changer()
{
this.btnChange(b[r][c]);
if(r>0)
this.btnChange(b[r-1][c]);
if(r<4)
this.btnChange(b[r+1][c]);
if(c>0)
this.btnChange(b[r][c-1]);
if(c<4)
this.btnChange(b[r][c+1]);
}
}
}
class CtrlPanel extends JPanel
{
JButton btnstart;
public CtrlPanel()
{
btnstart=new JButton("重新开始");
this.add(btnstart);
}
}
要求写出每个部分实现的功能以及方法,最好能写出每句都能写出注释,谢谢!
或者给我新的Java小游戏代码,同样要求有注释,程序越简单越好,代码最好不超过100条!谢谢! 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Test
{
public static void main(String[] args)
{
App ap=new App();
ap.show();
}
}
class App extends JFrame
{
MainPanel mp;
public App()
{
mp=new MainPanel();
this.getContentPane().add(mp);
this.setSize(400,450);
this.setTitle("小游戏");
}
}
class MainPanel extends JPanel
{
ButtonPanel bp=new ButtonPanel();
CtrlPanel rp=new CtrlPanel();
public MainPanel()
{
this.setLayout(new BorderLayout());
rp.btnstart.addActionListener(new StartListener());
this.add(bp,"Center");
this.add(rp,"South");
}
class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="重新开始")
{
bp.ColorInit();
}
}
}
}
class ButtonPanel extends JPanel
{
JButton[][] b=new JButton[5][5];
public ButtonPanel()
{
this.setLayout(new GridLayout(5,5));
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
b[i][j]=new JButton();
b[i][j].setActionCommand(""+(i+1)+(j+1));
b[i][j].addActionListener(new MyButtonListener());
this.add(b[i][j]);
}
}
this.ColorInit();
}
public void ColorInit()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
b[i][j].setBackground(Color.DARK_GRAY);
}
}
}
class MyButtonListener implements ActionListener
{
int r,c;
public void actionPerformed(ActionEvent e)
{
int i=Integer.parseInt(e.getActionCommand());
r=i/10-1;
c=i%10-1;
this.changer();
}
public void btnChange(JButton b)
{
if(b.getBackground()==Color.DARK_GRAY)
{
b.setBackground(Color.pink);
}
else
{
b.setBackground(Color.DARK_GRAY);
}
}
public void changer()
{
this.btnChange(b[r][c]);
if(r>0)
this.btnChange(b[r-1][c]);
if(r<4)
this.btnChange(b[r+1][c]);
if(c>0)
this.btnChange(b[r][c-1]);
if(c<4)
this.btnChange(b[r][c+1]);
}
}
}
class CtrlPanel extends JPanel
{
JButton btnstart;
public CtrlPanel()
{
btnstart=new JButton("重新开始");
this.add(btnstart);
}
}
要求写出每个部分实现的功能以及方法,最好能写出每句都能写出注释,谢谢!
或者给我新的Java小游戏代码,同样要求有注释,程序越简单越好,代码最好不超过100条!谢谢! 展开
展开全部
package org.zcq100.test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGame {
public static void main(String[] args) {
App ap = new App(); //调用App()开始运行程序
ap.show();
}
}
class App extends JFrame {
MainPanel mp;
public App() {
mp = new MainPanel();
this.getContentPane().add(mp);
this.setSize(400, 450);
this.setTitle("小游戏");
}
}
/**
* 主面板
* 显示格子
* @author Administrator
*
*/
class MainPanel extends JPanel {
ButtonPanel bp = new ButtonPanel();
CtrlPanel rp = new CtrlPanel();
public MainPanel() {
this.setLayout(new BorderLayout());
rp.btnstart.addActionListener(new StartListener());
this.add(bp, "Center");
this.add(rp, "South");
}
class StartListener implements ActionListener {
/**
* 重新开始按钮的事件
* 调用按钮面板里面的颜色初始化方法
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "重新开始") {
bp.ColorInit();
}
}
}
}
class ButtonPanel extends JPanel {
JButton[][] b = new JButton[5][5];
/**
* 按钮界面的构造器
* 设置布局方式为Grid布局,并生成5*5的格子,
* 在每个格子生成一个按钮,
* 为每个按钮添加一个监听事件
*/
public ButtonPanel() {
this.setLayout(new GridLayout(5, 5));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j] = new JButton();
b[i][j].setActionCommand("" + (i + 1) + (j + 1));
b[i][j].addActionListener(new MyButtonListener());
this.add(b[i][j]);
}
}
this.ColorInit();
}
/**
* 面板初始化时候给所有的格子都绘上深灰色
* i.j分别是行和列
*/
public void ColorInit() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j].setBackground(Color.DARK_GRAY);
}
}
}
/**
* 按钮上监听的时事件,监听点击
* @author Administrator
*
*/
class MyButtonListener implements ActionListener {
int r, c;
/**
* 需要改变颜色的行和列
* r row
* c colunm
* 调用change()来改变颜色
*/
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(e.getActionCommand());
r = i / 10 - 1;
c = i % 10 - 1;
this.changer();
}
/**
* 传一个按钮控件进去
* 判断颜色,如果是深灰则变为粉红
* 否则义相反
* @param b
*/
public void btnChange(JButton b) {
if (b.getBackground() == Color.DARK_GRAY) {
b.setBackground(Color.pink);
} else {
b.setBackground(Color.DARK_GRAY);
}
}
/**
* 这个方法是根据点击的按钮判断周围需要
* 不能超越数组的下标
*/
public void changer() {
this.btnChange(b[r][c]);
if (r > 0) //行号大于0
this.btnChange(b[r - 1][c]);
if (r < 4)
this.btnChange(b[r + 1][c]);
if (c > 0)//列号大于0
this.btnChange(b[r][c - 1]);
if (c < 4)//列好小余0
this.btnChange(b[r][c + 1]);
}
}
}
/**
* 控制面板
* @author Administrator
*下面的开始按钮
*/
class CtrlPanel extends JPanel {
JButton btnstart;
public CtrlPanel() {
btnstart = new JButton("重新开始");
this.add(btnstart);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGame {
public static void main(String[] args) {
App ap = new App(); //调用App()开始运行程序
ap.show();
}
}
class App extends JFrame {
MainPanel mp;
public App() {
mp = new MainPanel();
this.getContentPane().add(mp);
this.setSize(400, 450);
this.setTitle("小游戏");
}
}
/**
* 主面板
* 显示格子
* @author Administrator
*
*/
class MainPanel extends JPanel {
ButtonPanel bp = new ButtonPanel();
CtrlPanel rp = new CtrlPanel();
public MainPanel() {
this.setLayout(new BorderLayout());
rp.btnstart.addActionListener(new StartListener());
this.add(bp, "Center");
this.add(rp, "South");
}
class StartListener implements ActionListener {
/**
* 重新开始按钮的事件
* 调用按钮面板里面的颜色初始化方法
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "重新开始") {
bp.ColorInit();
}
}
}
}
class ButtonPanel extends JPanel {
JButton[][] b = new JButton[5][5];
/**
* 按钮界面的构造器
* 设置布局方式为Grid布局,并生成5*5的格子,
* 在每个格子生成一个按钮,
* 为每个按钮添加一个监听事件
*/
public ButtonPanel() {
this.setLayout(new GridLayout(5, 5));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j] = new JButton();
b[i][j].setActionCommand("" + (i + 1) + (j + 1));
b[i][j].addActionListener(new MyButtonListener());
this.add(b[i][j]);
}
}
this.ColorInit();
}
/**
* 面板初始化时候给所有的格子都绘上深灰色
* i.j分别是行和列
*/
public void ColorInit() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
b[i][j].setBackground(Color.DARK_GRAY);
}
}
}
/**
* 按钮上监听的时事件,监听点击
* @author Administrator
*
*/
class MyButtonListener implements ActionListener {
int r, c;
/**
* 需要改变颜色的行和列
* r row
* c colunm
* 调用change()来改变颜色
*/
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(e.getActionCommand());
r = i / 10 - 1;
c = i % 10 - 1;
this.changer();
}
/**
* 传一个按钮控件进去
* 判断颜色,如果是深灰则变为粉红
* 否则义相反
* @param b
*/
public void btnChange(JButton b) {
if (b.getBackground() == Color.DARK_GRAY) {
b.setBackground(Color.pink);
} else {
b.setBackground(Color.DARK_GRAY);
}
}
/**
* 这个方法是根据点击的按钮判断周围需要
* 不能超越数组的下标
*/
public void changer() {
this.btnChange(b[r][c]);
if (r > 0) //行号大于0
this.btnChange(b[r - 1][c]);
if (r < 4)
this.btnChange(b[r + 1][c]);
if (c > 0)//列号大于0
this.btnChange(b[r][c - 1]);
if (c < 4)//列好小余0
this.btnChange(b[r][c + 1]);
}
}
}
/**
* 控制面板
* @author Administrator
*下面的开始按钮
*/
class CtrlPanel extends JPanel {
JButton btnstart;
public CtrlPanel() {
btnstart = new JButton("重新开始");
this.add(btnstart);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询