求解,这是一个JAVA扫雷游戏,可是代码中没有雷,请问雷的那段代码是什么,高手指教
importjava.awt.event.*;importjava.awt.*;importjavax.swing.*;importjavax.swing.border....
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Block extends JButton implements MouseListener
{
boolean 雷=false,
周围无雷=true;
int 周围的雷数=0;
int 探雷标记=-1;
JLabel label;
Point p;
public Block()
{
label=new JLabel();
setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setHorizontalAlignment(JLabel.CENTER);
label.setLayout(new BorderLayout());
label.add(this,BorderLayout.CENTER);
p=new Point(0,0);
addMouseListener(this);
}
public void 设置是否是雷(boolean b)
{
雷=b;
}
public boolean 判断是否是雷()
{
return 雷;
}
public void 设置是雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围无雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围有雷时的名字(String name)
{
label.setText(name);
}
public String 获取名字()
{
return label.getText();
}
public void 设置周围的雷数(int n)
{
周围的雷数=n;
}
public int 获取周围的雷数()
{
return 周围的雷数;
}
public void 设置在雷区中的位置(int i,int j)
{
p=new Point(i,j);
}
public Point 获取在雷区中的位置()
{
return p;
}
public int 获取探雷标记()
{
return 探雷标记;
}
public void mousePressed(MouseEvent e)
{
boolean right=SwingUtilities.isRightMouseButton(e);
if((right==true)&&(this.isOpaque()==true))
{
探雷标记=(探雷标记+1)%3;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(探雷标记==0)
{
g.setColor(Color.red);
g.fillRect(6,4,8,6);
g.drawLine(13,10,14,18);
}
if(探雷标记==1)
{
g.setColor(Color.blue);
g.drawString("?",6,15);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
} 展开
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Block extends JButton implements MouseListener
{
boolean 雷=false,
周围无雷=true;
int 周围的雷数=0;
int 探雷标记=-1;
JLabel label;
Point p;
public Block()
{
label=new JLabel();
setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setHorizontalAlignment(JLabel.CENTER);
label.setLayout(new BorderLayout());
label.add(this,BorderLayout.CENTER);
p=new Point(0,0);
addMouseListener(this);
}
public void 设置是否是雷(boolean b)
{
雷=b;
}
public boolean 判断是否是雷()
{
return 雷;
}
public void 设置是雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围无雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围有雷时的名字(String name)
{
label.setText(name);
}
public String 获取名字()
{
return label.getText();
}
public void 设置周围的雷数(int n)
{
周围的雷数=n;
}
public int 获取周围的雷数()
{
return 周围的雷数;
}
public void 设置在雷区中的位置(int i,int j)
{
p=new Point(i,j);
}
public Point 获取在雷区中的位置()
{
return p;
}
public int 获取探雷标记()
{
return 探雷标记;
}
public void mousePressed(MouseEvent e)
{
boolean right=SwingUtilities.isRightMouseButton(e);
if((right==true)&&(this.isOpaque()==true))
{
探雷标记=(探雷标记+1)%3;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(探雷标记==0)
{
g.setColor(Color.red);
g.fillRect(6,4,8,6);
g.drawLine(13,10,14,18);
}
if(探雷标记==1)
{
g.setColor(Color.blue);
g.drawString("?",6,15);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
} 展开
展开全部
这个类就是雷的代码,它把雷当成一个对象,在其它地方调用,每次new Block(),就是创建一颗雷的。
下面给你加个测试方法,你慢慢看吧。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Block extends JButton implements MouseListener
{
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setLayout(new GridLayout(3,3));
Block b1 = new Block();
Block b2 = new Block();
b2.设置是否是雷(true);
Block b3 = new Block();
Block b4 = new Block();
b4.设置是否是雷(true);
Block b5 = new Block();
Block b6 = new Block();
b6.设置是否是雷(true);
Block b7 = new Block();
Block b8 = new Block();
Block b9 = new Block();
b1.setText(""+b1.获取周围的雷数());
b2.setText(""+b2.获取周围的雷数());
b3.setText(""+b3.获取周围的雷数());
b4.setText(""+b4.获取周围的雷数());
b5.setText(""+b5.获取周围的雷数());
b6.setText(""+b6.获取周围的雷数());
b7.setText(""+b7.获取周围的雷数());
b8.setText(""+b8.获取周围的雷数());
b9.setText(""+b9.获取周围的雷数());
jf.add(b1);
jf.add(b2);
jf.add(b3);
jf.add(b4);
jf.add(b5);
jf.add(b6);
jf.add(b7);
jf.add(b8);
jf.add(b9);
jf.setSize(300, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
boolean 雷=false,
周围无雷=true;
int 周围的雷数=0;
int 探雷标记=-1;
JLabel label;
Point p;
public Block()
{
label=new JLabel();
setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setHorizontalAlignment(JLabel.CENTER);
label.setLayout(new BorderLayout());
label.add(this,BorderLayout.CENTER);
p=new Point(0,0);
addMouseListener(this);
}
public void 设置是否是雷(boolean b)
{
雷=b;
}
public boolean 判断是否是雷()
{
return 雷;
}
public void 设置是雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围无雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围有雷时的名字(String name)
{
label.setText(name);
}
public String 获取名字()
{
return label.getText();
}
public void 设置周围的雷数(int n)
{
周围的雷数=n;
}
public int 获取周围的雷数()
{
return 周围的雷数;
}
public void 设置在雷区中的位置(int i,int j)
{
p=new Point(i,j);
}
public Point 获取在雷区中的位置()
{
return p;
}
public int 获取探雷标记()
{
return 探雷标记;
}
public void mousePressed(MouseEvent e)
{
boolean right=SwingUtilities.isRightMouseButton(e);
if((right==true)&&(this.isOpaque()==true))
{
探雷标记=(探雷标记+1)%3;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(探雷标记==0)
{
g.setColor(Color.red);
g.fillRect(6,4,8,6);
g.drawLine(13,10,14,18);
}
if(探雷标记==1)
{
g.setColor(Color.blue);
g.drawString("?",6,15);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}
下面给你加个测试方法,你慢慢看吧。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Block extends JButton implements MouseListener
{
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setLayout(new GridLayout(3,3));
Block b1 = new Block();
Block b2 = new Block();
b2.设置是否是雷(true);
Block b3 = new Block();
Block b4 = new Block();
b4.设置是否是雷(true);
Block b5 = new Block();
Block b6 = new Block();
b6.设置是否是雷(true);
Block b7 = new Block();
Block b8 = new Block();
Block b9 = new Block();
b1.setText(""+b1.获取周围的雷数());
b2.setText(""+b2.获取周围的雷数());
b3.setText(""+b3.获取周围的雷数());
b4.setText(""+b4.获取周围的雷数());
b5.setText(""+b5.获取周围的雷数());
b6.setText(""+b6.获取周围的雷数());
b7.setText(""+b7.获取周围的雷数());
b8.setText(""+b8.获取周围的雷数());
b9.setText(""+b9.获取周围的雷数());
jf.add(b1);
jf.add(b2);
jf.add(b3);
jf.add(b4);
jf.add(b5);
jf.add(b6);
jf.add(b7);
jf.add(b8);
jf.add(b9);
jf.setSize(300, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
boolean 雷=false,
周围无雷=true;
int 周围的雷数=0;
int 探雷标记=-1;
JLabel label;
Point p;
public Block()
{
label=new JLabel();
setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setBorder(BorderFactory.createMatteBorder(1,
1,
0,
0,
Color.lightGray));
label.setHorizontalAlignment(JLabel.CENTER);
label.setLayout(new BorderLayout());
label.add(this,BorderLayout.CENTER);
p=new Point(0,0);
addMouseListener(this);
}
public void 设置是否是雷(boolean b)
{
雷=b;
}
public boolean 判断是否是雷()
{
return 雷;
}
public void 设置是雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围无雷时的图标(Icon icon)
{
label.setIcon(icon);
}
public void 设置周围有雷时的名字(String name)
{
label.setText(name);
}
public String 获取名字()
{
return label.getText();
}
public void 设置周围的雷数(int n)
{
周围的雷数=n;
}
public int 获取周围的雷数()
{
return 周围的雷数;
}
public void 设置在雷区中的位置(int i,int j)
{
p=new Point(i,j);
}
public Point 获取在雷区中的位置()
{
return p;
}
public int 获取探雷标记()
{
return 探雷标记;
}
public void mousePressed(MouseEvent e)
{
boolean right=SwingUtilities.isRightMouseButton(e);
if((right==true)&&(this.isOpaque()==true))
{
探雷标记=(探雷标记+1)%3;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(探雷标记==0)
{
g.setColor(Color.red);
g.fillRect(6,4,8,6);
g.drawLine(13,10,14,18);
}
if(探雷标记==1)
{
g.setColor(Color.blue);
g.drawString("?",6,15);
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询