Swing是一个用于开发Java应用程序用户界面的开发工具包。它以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风
格。Swing开发人员只用很少的代码就可以利用Swing丰富、灵活的功能和模块化组件来创建优雅的用户界面。 工具包中所有的包都是以swing作为名称,例如javax.swing,javax.swing.event。。。。
AWT和Swing都是JavaSE的组成部分,但Swing提供了更为丰富的功能与控件,如 tabbed panel, scroll panes, trees, tables和lists等
而且因为Swing使用Java2D实现,所有代码都是java,无需依赖于native system,因此,其look&feel可以在不同系统建保持一致 - 但你同时又可以调整让其以native system的风
格显示,可以说更加灵活。
Swing采用了MVC架构。
开发GUI(Graphical User Interface)程序,而没有一个UI designer的话会是非常低效的,MyEclipse提供了一个针对Swing的Swing GUI Designer 10.6 [2],但是目前网络上多数指向它的链接都无法访问,据说其MyEclipse已经停止开发它了,所以感觉价值不是很高。但是google又一次大度的给出了Java GUI Design方面的神器:WindowsBuilder, 支持Swing和SWT,相当强大。
不多说了 , 直接上源码!!!!。。。。
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
public class MainPanel extends JPanel {
private StatusPanel statusPanel = null;
private MineAreaPanel mineAreaPanel = null;
/**
* 地雷总数量
*/
private int totalMineCount = 10;
// 剩余地雷的数量
private int remianMineCount = 10;
// 记时
private int time = 0;
// 地雷图片
private Icon[] imgMine = null;
// 地雷数量图片
private Icon[] imgMineArray = null;
// 计时器
private Timer timer = null;
// 游戏结束
private boolean end = false;
private ActionListener alTimer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
time++;
statusPanel.updateTime();
}
};
public MainPanel() {
BorderLayout bl = new BorderLayout(1, 3);
this.setLayout(bl);
imgMine = new Icon[2];
imgMine[0] = new ImageIcon("img/detonate.png");
imgMine[1] = new ImageIcon("img/mine.png");
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5);
this.setBorder(border);
statusPanel = new StatusPanel();
mineAreaPanel = new MineAreaPanel();
mineAreaPanel.putMine();
this.add(statusPanel, BorderLayout.NORTH);
this.add(mineAreaPanel, BorderLayout.CENTER);
timer = new Timer(1000, alTimer);
timer.start();
}
/**
* 状态面板
*
* @author Administrator
*
*/
private class StatusPanel extends JPanel {
private JLabel lblRemain = null;
private JLabel lblFace = null;
private JLabel lblTimer = null;
private Icon[] faces = null;
public StatusPanel() {
GridLayout gl = new GridLayout();
this.setLayout(gl);
Border border = BorderFactory
.createBevelBorder(BevelBorder.LOWERED);
this.setBorder(border);
faces = new Icon[3];
faces[0] = new ImageIcon("img/laugh.png");
faces[1] = new ImageIcon("img/cry.png");
faces[2] = new ImageIcon("img/win.png");
lblRemain = new JLabel();
lblRemain.setText(String.format("%03d", remianMineCount));
Font font = new Font(Font.SERIF, Font.BOLD, 20);
lblRemain.setFont(font);
lblRemain.setOpaque(true);
lblRemain.setBackground(Color.BLACK);
lblRemain.setForeground(Color.RED);
lblRemain.setHorizontalAlignment(JLabel.CENTER);
lblFace = new JLabel(faces[0]);
lblTimer = new JLabel();
lblTimer.setText(String.format("%03d", time));
lblTimer.setFont(font);
lblTimer.setOpaque(true);
lblTimer.setBackground(Color.BLACK);
lblTimer.setForeground(Color.RED);
lblTimer.setHorizontalAlignment(JLabel.CENTER);
this.add(lblRemain);
this.add(lblFace);
this.add(lblTimer);
}
public void updateTime() {
lblTimer.setText(String.format("%03d", time));
}
public void gameFinish() {
lblFace.setIcon(faces[1]);
}
}
/**
* 块的鼠标监听
*/
private MouseListener mlBlock = new MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
Block block = (Block) e.getSource();
if (!end) {
block.setDisplay(true);
if (block.isMine()) {
block.setIcon(imgMine[0]);
end = true;
gameFinish();
} else {
block.setIcon(imgMineArray[block.getRoundMineCount()]);
}
}
};
};
private class MineAreaPanel extends JPanel {
private int rows = 20;
private int columns = 20;
private Block[][] blocks = null;
public MineAreaPanel() {
GridLayout gl = new GridLayout(rows, columns);
this.setLayout(gl);
Border border = BorderFactory
.createBevelBorder(BevelBorder.LOWERED);
this.setBorder(border);
imgMineArray = new Icon[10];
for (int i = 0; i < 9; i++) {
imgMineArray[i] = new ImageIcon("img/N_" + i + ".png");
}
imgMineArray[9] = new ImageIcon("img/no_open.png");
blocks = new Block[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
blocks[i][j] = new Block(imgMineArray[9]);
blocks[i][j].addMouseListener(mlBlock);
this.add(blocks[i][j]);
}
}
}
/**
* 放置地雷
*/
public void putMine() {
int count = 0;
while (count < totalMineCount) {
int i = (int) (Math.random() * rows);
int j = (int) (Math.random() * columns);
if (!blocks[i][j].isMine()) {
blocks[i][j].setMine(true);
count++;
}
}
//统计数量
}
/**
* 计算每个块周围地雷的数量
*/
private void calculateMineCount() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int roundMineCount = 0;
// 计算
if (i == 0) {
if (j == 0) {
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j + 1].isMine()) {
roundMineCount++;
}
} else if (j == columns - 1) {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j - 1].isMine()) {
roundMineCount++;
}
} else {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j + 1].isMine()) {
roundMineCount++;
}
}
} else if (i == rows - 1) {
if (j == 0) {
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j + 1].isMine()) {
roundMineCount++;
}
} else if (j == columns - 1) {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j - 1].isMine()) {
roundMineCount++;
}
} else {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j + 1].isMine()) {
roundMineCount++;
}
}
} else {
if (j == 0) {
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j + 1].isMine()) {
roundMineCount++;
}
} else if (j == columns - 1) {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j - 1].isMine()) {
roundMineCount++;
}
} else {
if (blocks[i][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i - 1][j + 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j - 1].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j].isMine()) {
roundMineCount++;
}
if (blocks[i + 1][j + 1].isMine()) {
roundMineCount++;
}
}
}
blocks[i][j].setRoundMineCount(roundMineCount);
}
}
}
/**
* 游戏结束,翻出所有隐藏的地雷
*/
public void gameFinish() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (blocks[i][j].isMine() && !blocks[i][j].isDisplay()) {
blocks[i][j].setIcon(imgMine[1]);
}
}
}
}
}
private class Block extends JLabel {
private boolean mine = false;
private int roundMineCount;
private boolean display = false;
public boolean isDisplay() {
return display;
}
public void setDisplay(boolean display) {
this.display = display;
}
public boolean isMine() {
return mine;
}
public void setMine(boolean mine) {
this.mine = mine;
}
public int getRoundMineCount() {
return roundMineCount;
}
public void setRoundMineCount(int roundMineCount) {
this.roundMineCount = roundMineCount;
}
public Block(Icon icon) {
super(icon);
}
}
protected void gameFinish() {
timer.stop();
mineAreaPanel.gameFinish();
statusPanel.gameFinish();
}
}
import javax.swing.JFrame;
public class MainFrame extends JFrame {
public MainFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainPanel pnl = new MainPanel();
this.getContentPane().add(pnl);
this.pack();
}
/**
* @param args
*/
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
效果图: