java通过代码显示特定窗体,如何把这两段代码在一个窗体显示,类似于windows自带的扫雷一样...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

第一段是菜单栏:

package saolei;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

public class caidan extends JFrame implements ActionListener{

private static final int DEFAULT_WIDTH=500;

private static final int DEFAULT_HEIGHT=500;

private JLabel label=new JLabel();

private JMenuItem openItem =new JMenuItem("重新开始");

private JMenuItem exitItem =new JMenuItem("关闭");

private JMenuItem help =new JMenuItem("帮助");

private JMenuItem user1 =new JMenuItem("初级");

private JMenuItem user2 =new JMenuItem("中级");

private JMenuItem user3 =new JMenuItem("高级");

public caidan(){

setTitle("扫雷");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

add(label);

JMenuBar menuBar=new JMenuBar();

setJMenuBar(menuBar);

JMenu menu=new JMenu("游戏");

JMenu menu2=new JMenu("帮助");

menuBar.add(menu);

menuBar.add(menu2);

menu.add(openItem);

openItem.addActionListener(this);

menu.add(user1);

user1.addActionListener(this);

menu.add(user2);

user2.addActionListener(this);

menu.add(user3);

user3.addActionListener(this);

menu.add(exitItem);

exitItem.addActionListener(this);

menu2.add(help);

help.addActionListener(this);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==openItem){

}

if(e.getSource()==user1){

}

if(e.getSource()==user2){

}

if(e.getSource()==user3){

}

if(e.getSource()==help){

}

if(e.getSource()==exitItem){

System.exit(0);

}

}

public static void main(String[] args) {

new caidan();

}}

第二段是扫雷的功能实现:

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

class Test extends caidan implements ActionListener, Runnable,

MouseListener {

private final int loEMPTY = 0;

private final int loMINE = 1;

private final int loCHECKED = 2;

private final int loMINE_COUNT = 10;

private final int loBUTTON_BORDER = 50;

private final int loMINE_SIZE = 10;

private final int loSTART_X = 20;

private final int loSTART_Y = 50;

private boolean flag;

private JButton[][] jb;

private JLabel jl;

private JLabel showTime;

private int[][] map;

private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 },

{ 1, -1 }, { 0, -1 }, { -1, -1 } };

public void makeloMINE() {

int i = 0, tx, ty;

for (; i < loMINE_COUNT;) {

tx = (int) (Math.random() * loMINE_SIZE);

ty = (int) (Math.random() * loMINE_SIZE);

if (map[tx][ty] == loEMPTY) {

map[tx][ty] = loMINE;

i++;

}

}

}

public void makeButton() {

for (int i = 0; i < loMINE_SIZE; i++) {

for (int j = 0; j < loMINE_SIZE; j++) {

jb[i][j] = new JButton();

jb[i][j].addActionListener(this);

jb[i][j].addMouseListener(this);

jb[i][j].setName(i + "_" + j);

jb[i][j].setBounds(j * loBUTTON_BORDER + loSTART_X, i

* loBUTTON_BORDER + loSTART_Y, loBUTTON_BORDER, loBUTTON_BORDER);

this.add(jb[i][j]);

}

}

}

public void init() {

flag = false;

jl.setText("一共有" + loMINE_COUNT + "个雷");

jl.setVisible(true);

jl.setBounds(20, 20, 500, 30);

this.add(jl);

showTime.setText("已用时:0 秒");

showTime.setBounds(400, 20, 100, 30);

this.add(showTime);

makeloMINE();

makeButton();

this.setSize(550, 600);

this.setLocation(700, 100);

this.setResizable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

public Test(String title) {

super();

this.setLayout(null); //不使用布局管理器,每个控件的位置用setBounds设定

jb = new JButton[loMINE_SIZE][loMINE_SIZE];

jl = new JLabel();

showTime = new JLabel();

map = new int[loMINE_SIZE][loMINE_SIZE]; // 将按钮映射到数组中

}

public static void main(String[] args) {

Test test = new Test("Hello loMINEr!");

test.init();

test.run();

}

@Override

public void actionPerformed(ActionEvent e) {

Object obj = e.getSource();

int x, y;

if ((obj instanceof JButton) == false) {

showMessage("错误", "内部错误");

return;

}

String[] tmp_str = ((JButton) obj).getName().split("_");

x = Integer.parseInt(tmp_str[0]);

y = Integer.parseInt(tmp_str[1]);

if (map[x][y] == loMINE) {

showMessage("死亡", "你踩到地雷啦~~~");

flag = true;

showloMINE();

return;

}

dfs(x, y, 0);

checkSuccess();

}

private void checkSuccess() {

int cnt = 0;

for (int i = 0; i < loMINE_SIZE; i++) {

for (int j = 0; j < loMINE_SIZE; j++) {

if (jb[i][j].isEnabled()) {

cnt++;

}

}

}

if (cnt == loMINE_COUNT) {

String tmp_str = showTime.getText();

tmp_str = tmp_str.replaceAll("[^0-9]", "");

showMessage("胜利", "本次扫雷共用时:" + tmp_str + "秒");

flag = true;

showloMINE();

}

}

private int dfs(int x, int y, int d) {

map[x][y] = loCHECKED;

int i, tx, ty, cnt = 0;

for (i = 0; i < 8; i++) {

tx = x + mv[i][0];

ty = y + mv[i][1];

if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE) {

if (map[tx][ty] == loMINE) {

cnt++;

} else if (map[tx][ty] == loEMPTY) {

;

} else if (map[tx][ty] == loCHECKED) {

;

}

}

}

if (cnt == 0) {

for (i = 0; i < 8; i++) {

tx = x + mv[i][0];

ty = y + mv[i][1];

if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE

&& map[tx][ty] != loCHECKED) {

dfs(tx, ty, d + 1);

}

}

} else {

jb[x][y].setText(cnt + "");

}

jb[x][y].setEnabled(false);

return cnt;

}

private void showMessage(String title, String info) {

jl.setText(info);

System.out.println("in functino showMessage() : " + info);

}

public void run() {

int t = 0;

while (true) {

if (flag) {

break;

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

t++;

showTime.setText("已用时:" + t + " 秒");

}

}

private void showloMINE() {

for (int i = 0; i < loMINE_SIZE; i++) {

for (int j = 0; j < loMINE_SIZE; j++) {

if (map[i][j] == loMINE) {

jb[i][j].setText("雷");

}

}

}

}

public void mouseClicked(MouseEvent e) {

if (e.getButton() == 3) {

Object obj = e.getSource();

if ((obj instanceof JButton) == false) {

showMessage("错误", "内部错误");

return;

}

String[] tmp_str = ((JButton) obj).getName().split("_");

int x = Integer.parseInt(tmp_str[0]);

int y = Integer.parseInt(tmp_str[1]);

if ("{1}quot".equals(jb[x][y].getText())) {

jb[x][y].setText("");

} else {

jb[x][y].setText("{1}quot");

}

}

}

public void mousePressed(MouseEvent e) {

}

@Override

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

@Override

public void mouseExited(MouseEvent e) {

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值