--扫雷
package com;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.*;
public class MineGame extends JFrame {
// 定义一部分游戏参数
int btnwidth = 0;
int btnhigth = 0;
// 雷的数量
int mine = 0;
// 统计雷的个数
int minenumber = 0;
// 找到正确雷的数量
int findmines = 0;
// 标记的个数
int mark = 0;
// 按钮的总行数和总列数
int btnrows = 0;
int btncols = 0;
// 定义一个统计按钮左键的点击次数
int leftclicknumber = 0;
// 定义一个统计时间的容器
int time = 0;
// 定义一个计时器
Timer timer = null;
// 声明用于布局的面板
// JPanel toppanel = null;
// JPanel downpanel = null;
// JPanel leftpanel = null;
// JPanel rigthpanel = null;
JPanel centerpanel = null;
// 声明面板上的所有按钮
JButton[][] btns = null;
// 声明存放地雷坐标的数组
String[] mines = null;
// 声明一个显示时间的label
JLabel lblTime = null;
// 声明一个显示标记被数量的label
JLabel lblminenumber = null;
// 声明一个用于存放颜色的数组
Color[] colors = new Color[] { Color.blue, Color.green, Color.red,
Color.yellow, Color.black, Color.orange, Color.gray, Color.pink };
public static void main(String[] args) {
// TODO Auto-generated method stub
MineGame game = new MineGame();
}
// 初始化面板
private void initContorl() {
// toppanel = new JPanel();
// downpanel = new JPanel();
// leftpanel = new JPanel();
// rigthpanel = new JPanel();
centerpanel = new JPanel();
// 清空centerpanel面板的默认布局
centerpanel.setLayout(null);
centerpanel.setBorder(BorderFactory.createLineBorder(Color.red));
// 创建阿牛
// createBtns();
// 创建地雷
// createMines();
initPanel(16, 16, 40);
lblTime = new JLabel("0");
lblminenumber = new JLabel("0");
// 设置大小
lblTime.setSize(100, 30);
lblminenumber.setSize(100, 30);
// 设置字体和颜色
lblTime.setFont(new Font("楷体", Font.BOLD, 28));
lblTime.setForeground(Color.blue);
lblminenumber.setFont(new Font("楷体", Font.BOLD, 28));
lblminenumber.setForeground(Color.red);
// 设置边框
lblTime.setBorder(BorderFactory.createLineBorder(Color.red));
lblminenumber.setBorder(BorderFactory.createLineBorder(Color.cyan));
// 设置lblTime的坐标
lblTime.setLocation(60, 600);
lblminenumber.setLocation(450, 600);
// 设置显示时间图片
JLabel lbltimeimage = new JLabel(new ImageIcon("Images/time.png"));
lbltimeimage.setBounds(20, 600, 30, 30);
// 设置显示标记雷数量图片
JLabel lblminenumberimage = new JLabel(new ImageIcon("Images/mine.png"));
lblminenumberimage.setBounds(410, 600, 30, 30);
// 显示被标记的数量
lblminenumber.setText(String.valueOf(mark));
// 创建计时器(设置一秒触发一次)
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
time++;
lblTime.setText(String.valueOf(time));
}
});
centerpanel.add(lblTime);
centerpanel.add(lbltimeimage);
centerpanel.add(lblminenumber);
centerpanel.add(lblminenumberimage);
// 将不同的面板加到窗体中
// this.add(toppanel, BorderLayout.NORTH);
// this.add(leftpanel, BorderLayout.WEST);
this.add(centerpanel, BorderLayout.CENTER);
// this.add(rigthpanel, BorderLayout.EAST);
// this.add(downpanel, BorderLayout.SOUTH);
}
// 创建雷
// 随机产生40个雷的坐标
private void createMines() {
Random rd = new Random();
int count = 0;
while (count < mine) {
int row = rd.nextInt(btnrows);
int col = rd.nextInt(btncols);
// 地雷的坐标
String coordinate = row + "," + col;
// 判断是否坐标重复
if (!isHave(coordinate)) {
mines[count] = coordinate;
count++;
}
}
}
// 显示所有的雷
// 如果踩到地雷则显示地图上所有的雷。。
private void dieshow() {
for (int i = 0; i < btns.length; i++) {
for (int j = 0; j < btns[i].length; j++) {
if (btns[i][j] == null) {
continue;
}
JButton btn = btns[i][j];
for (int k = 0; k < mines.length; k++) {
if (mines[k].equals(btn.getActionCommand())) {
// 创建一个图片对象
ImageIcon image = new ImageIcon("Images/mines.png");
btn.setIcon(image);
}
}
}
}
}
// 判断雷的数量
// 判断随机产生的地雷的坐标是否存在
private boolean isHave(String location) {
boolean bl = false;
for (int i = 0; i < mines.length; i++) {
if (mines[i] != null) {
if (mines[i].equals(location)) {
return true;
}
}
}
return bl;
}
// 创建按钮
// 初始化所有的按钮
private void createBtns() {
for (int i = 0; i < btns.length; i++) {
for (int j = 0; j < btns[i].length; j++) {
JButton btn = new JButton();
// 设置按钮大小
btn.setSize(btnwidth, btnhigth);
// 设置按钮的单击事件
btn.setActionCommand(i + "," + j);
// 给按钮添加点击事件
btn.addActionListener(new ButtonClick());
// 添加注册鼠标右击事件
btn.addMouseListener(new ButtonMonse());
// 设置按钮在面板中的坐标
int row = i;
int col = j;
int x = col * btnwidth;
int y = row * btnhigth;
// 设置按钮在面板中的坐标
btn.setLocation(x, y);
// 将按钮添加到面板中
centerpanel.add(btn);
// 将按钮添加到二维数组中
btns[i][j] = btn;
}
}
}
// 构造函数
// 构造函数
public MineGame() {
// 初始化面板
initContorl();
// 添加菜单栏到面板中
addMenu();
// 踩雷
// dieshow();
this.setSize(610, 730);
this.setTitle("扫 雷");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 初始化面板
// 完成窗体按钮的添加以及雷数量的初始化
private void initPanel(int rows, int cols, int mine) {
this.btnrows = rows;
this.btncols = cols;
this.mine = mine;
// 设置中间面板的大小
centerpanel.setSize(600, 600);
this.btnwidth = centerpanel.getSize().width / cols;
this.btnhigth = centerpanel.getSize().height / rows;
// 定义按钮的大小
btns = new JButton[rows][cols];
// 定义雷的数量
this.mines = new String[mine];
// 清空容器中所有的按钮
centerpanel.removeAll();
// 添加按钮到容器中
createBtns();
// 创建指定类的数量
createMines();
// 重回面板
centerpanel.repaint();
}
// 添加菜单
// 完成菜单的添加
private void addMenu() {
// 创建一个菜单栏
JMenuBar menubar = new JMenuBar();
// 创建菜单按钮
JMenu menuone = new JMenu("游 戏(G)");
JMenu menutwo = new JMenu("帮 助(H)");
// 将菜单按钮添加到菜单栏中
menubar.add(menuone);
menubar.add(menutwo);
// 添加菜单项
JMenuItem itembase = new JMenuItem("初 级");
itembase.setActionCommand("base");
JMenuItem itemmiddle = new JMenuItem("中 级");
itemmiddle.setActionCommand("middle");
JMenuItem itemhigh = new JMenuItem("高 级");
itemhigh.setActionCommand("high");
JMenuItem itemexit = new JMenuItem("退 出");
itemexit.setActionCommand("exit");
itembase.addActionListener(new MenuItemClick());
itemmiddle.addActionListener(new MenuItemClick());
itemhigh.addActionListener(new MenuItemClick());
itemexit.addActionListener(new MenuItemClick());
// 将菜单项添加到菜单按钮中
menuone.add(itembase);
menuone.add(itemmiddle);
menuone.add(itemhigh);
menuone.addSeparator();
menuone.add(itemexit);
// 将菜单栏添加到窗体中
// toppanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// toppanel.add(menubar);
this.setJMenuBar(menubar);
}
// 判断有没有点中雷
private boolean isTreadMine(JButton btn) {
boolean result = false;
for (int i = 0; i < mines.length; i++) {
if (mines[i].equals(btn.getActionCommand())) {
return true;
}
}
return result;
}
// 获取附近的雷
// 检测按钮的周围有多少颗雷
private int nearMines(JButton btn) {
int result = 0;
// 需要获取周围所有的按钮
JButton[] nearbtns = getNearBtn(btn);
for (int i = 0; i < nearbtns.length; i++) {
if (nearbtns[i] != null) {
for (int j = 0; j < mines.length; j++) {
if (mines[j].equals(nearbtns[i].getActionCommand())) {
result++;
}
}
}
}
return result;
}
// 获取附近的按钮
// 获取一个按钮周围的所有按钮
private JButton[] getNearBtn(JButton btn) {
String command = btn.getActionCommand();
String[] strs = command.split(",");
int row = Integer.valueOf(strs[0]);
int col = Integer.valueOf(strs[1]);
JButton[] result = new JButton[8];
JButton lefttopbtn = null;
JButton topbtn = null;
JButton rigthtopbtn = null;
JButton rigthbtn = null;
JButton leftbtn = null;
JButton leftbottombtn = null;
JButton bottombtn = null;
JButton rigthbottombtn = null;
// 获取左上角的按钮
if (row - 1 >= 0 && col - 1 >= 0) {
lefttopbtn = btns[row - 1][col - 1];
}
// 获取上方
if (row - 1 >= 0) {
topbtn = btns[row - 1][col];
}
// 获取右上方的按钮
if (row - 1 >= 0 && col + 1 < btncols) {
rigthtopbtn = btns[row - 1][col + 1];
}
// 获取左边按钮
if (col - 1 >= 0) {
leftbtn = btns[row][col - 1];
}
// 获取右边的按钮
if (col + 1 < btncols) {
rigthbtn = btns[row][col + 1];
}
// 获取左下方的按钮
if (col - 1 >= 0 && row + 1 < btnrows) {
leftbottombtn = btns[row + 1][col - 1];
}
// 获取下方的按钮
if (row + 1 < btnrows) {
bottombtn = btns[row + 1][col];
}
// 获取右下方的按钮
if (row + 1 < btnrows && col + 1 < btncols) {
rigthbottombtn = btns[row + 1][col + 1];
}
result[0] = lefttopbtn;
result[1] = topbtn;
result[2] = rigthtopbtn;
result[3] = rigthbtn;
result[4] = leftbtn;
result[5] = leftbottombtn;
result[6] = bottombtn;
result[7] = rigthbottombtn;
return result;
}
// 单击按钮事件
// 定义一个用于处理按钮单击事件的内部类
private class ButtonClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 判断是否是第一次点击
leftclicknumber++;
if (leftclicknumber == 1) {
// 开始计时
timer.start();
}
// 获取所点击的按钮
JButton btnclick = (JButton) e.getSource();
// 判断有没有点中雷
if (isTreadMine(btnclick)) {
JOptionPane.showMessageDialog(null, "踩到雷了,游戏结束!");
// 踩雷
dieshow();
} else {
int minecount = nearMines(btnclick);
// JOptionPane.showMessageDialog(null, "周围有" + minecount +
// "颗雷!");
// btnclick.setText(String.valueOf(minecount));
// 将被点击的按钮从面板中移除
centerpanel.remove(btnclick);
// 将所点击的按钮从二维数组中移除
String[] strs = btnclick.getActionCommand().split(",");
int row = Integer.parseInt(strs[0]);
int col = Integer.parseInt(strs[1]);
btns[row][col] = null;
// 添加上一个JLabel元素表示数字
JLabel label = new JLabel(String.valueOf(minecount),
JLabel.CENTER);
// 设置Label相关的属性
label.setSize(btnwidth, btnhigth);
Point p = btnclick.getLocation();
// 设置位置
label.setLocation(p.x, p.y);
// 添加Label的边框
label.setBorder(BorderFactory.createLineBorder(Color.red, 1));
if (minecount > 0) {
// 设置文字大小
label.setFont(new Font("黑体", Font.BOLD, 28));
// 设置字体颜色
label.setForeground(colors[minecount - 1]);
} else {
label.setText("");
// 继续完成周围八个按钮的点击
JButton[] nearbtns = getNearBtn(btnclick);
for (int i = 0; i < nearbtns.length; i++) {
if (nearbtns[i] != null) {
// 用代码模拟鼠标点击
nearbtns[i].doClick();
}
}
}
centerpanel.add(label);
// 重绘UI面板
centerpanel.repaint();
}
}
}
// 鼠标右键事件
// 定义一个用于处理按钮鼠标操作事件的内部类
private class ButtonMonse implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (e.isMetaDown()) {
// 按下鼠标右键
// 在按下的按钮上插上一面小旗
ImageIcon image = new ImageIcon("Images/flag.jpg");
// 获取所点击的按钮
JButton btnclick = (JButton) e.getSource();
// 判断右键的按钮是否是一颗雷
if (isTreadMine(btnclick)) {
findmines++;
}
// 判断找到雷的数量是否和原来雷的数量一致
if (findmines == mine) {
JOptionPane.showMessageDialog(null, "恭喜你!已扫完所有的雷。。。");
}
// 判断按钮上是否有图片
if (btnclick.getIcon() == null) {
// 设置图片
btnclick.setIcon(image);
} else {
// 取消图片
btnclick.setIcon(null);
findmines--;
}
System.out.println(findmines);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
// 单击菜单事件
private class MenuItemClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String type = e.getActionCommand();
// 当点击的是退出按钮
if (type.equals("exit")) {
System.exit(0);
}
// 当点击的是初级按钮
if (type.equals("base")) {
initPanel(9, 9, 10);
}
// 当点击的是中级按钮
if (type.equals("middle")) {
initPanel(16, 16, 20);
}
// 当点击的是高级按钮
if (type.equals("high")) {
initPanel(16, 30, 99);
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
---弹钢琴
package com;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import javax.sound.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import sun.audio.*;
import javax.swing.*;
public class Playpaino extends JFrame {
// 声明窗体中所需的组件
Mypanel panel = null;
// 声明存放字母的Label
JLabel[] firstRow = new JLabel[10];
JLabel[] secondRow = new JLabel[9];
JLabel[] thirdRow = new JLabel[7];
// 申明一个存放所有声音文件的数组
String[] wavs = new String[] { "sound01.wav", "sound02.wav", "sound03.wav",
"sound04.wav", "sound05.wav", "sound06.wav", "sound07.wav",
"sound08.wav", "sound09.wav", "sound10.wav", "sound11.wav",
"sound12.wav", "sound13.wav", "sound14.wav", "sound15.wav",
"sound16.wav", "sound17.wav", "sound18.wav", "sound19.wav",
"sound20.wav", "sound21.wav", "sound22.wav", "sound23.wav",
"sound24.wav", "sound25.wav", "sound26.wav" };
// 申明一个选择曲目的下拉框
JComboBox cmbMusic = null;
// 申明一个显示曲目的内容的多行文本框
JTextArea txtContent = null;
// 申明一个文本框获取焦点
JTextField focus = null;
// 申明一个存放所有曲目的数组
String[] musics = new String[] { "月亮代表我的心", "北京欢迎你", "生日快乐", "小星星",
"笑傲江湖曲", "鐵血丹心", "同桌的你", "想唱就唱" };
// 申明一个存放歌曲内容的数组
String[] musiccontent = new String[] {
"LOQSONQS STUVTS QPOOO QPOOO PQPOMPQP LOQSONQS STUVTS QPOOO QPOOO PQPMNOPO QSQPOSN MNMNMLQ SQPOSN MNOOOPQP LOQSONQS STUVTS QPOOO QPOOO PQPMNOPO",
"POMOPQSPQTSSPO POMOPQSPQTSSQ PQPOSTQMQPPO QSVSTTS QQ SS QS TV WV SQ P S Q Q QS VS TV WV SQ SVT QP QS XW VV",
"EEFEHG EEFEIHC EELJHGF KKJHIC",
"OOSSTTS RRQQPPO SSRRQQP SSRRQQP OOSSTTS RRQQPPO",
"MMLJIH JIHFE EFEFHIJL MLJIHI MMLJIH JIHFE EFEFHIJL MLJIHH",
"JIHGFCF FEC FBC JIHGFCF IHF HIJ JMLMLJLI HFIJLKJ JLMLMLJLI HFIJEGF",
"LLLLJKLN MMMMKML LLLLNMLK KKKKJIH OOOOLMOQ PPPPONM NNNNNOPL NNOPONO OOOOLMOQ PPPPONM NNNNNOPL NNOPONO",
"ONOPOL JKKKLJ ONOPOL LMMMON ONOPQOLJ OPOLJ ONOPQOLO RQPOQ QRST OOPQP PQRS SRQPQ QRSTSS UUVUSPQ RQRS QRST OOPQP PQS QUUQV VUVTSOO TSRQRS TS QRST OOPQP PQRS SRQPQ QRSTSS UUVUSPQ RQRS QRST OOPQP PQS QUUQV VUVTSOO TSRQRS" };
public static void main(String[] args) {
// TODO Auto-generated method stub
Playpaino p = new Playpaino();
}
// 构造函数
public Playpaino() {
initControl();
this.setTitle("paly paino");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 初始化控件
private void initControl() {
panel = new Mypanel();
panel.setLayout(null);
this.setSize(700, 600);
// 注册键盘按下的事件
/*
* this.addKeyListener(new KeyListener() {
*
* @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated
* method stub
*
* }
*
* @Override public void keyReleased(KeyEvent e) { // TODO
* Auto-generated method stub
*
* String str = String.valueOf(e.getKeyChar());
*
* // System.out.println(str);
*
* for (int i = 0; i < firstRow.length; i++) {
*
* String name = firstRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + ".png"); firstRow[i].setIcon(icon); }
*
* }
*
* for (int i = 0; i < secondRow.length; i++) {
*
* String name = secondRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + ".png"); secondRow[i].setIcon(icon); }
*
* }
*
* for (int i = 0; i < thirdRow.length; i++) {
*
* String name = thirdRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + ".png"); thirdRow[i].setIcon(icon); }
*
* }
*
* try { // InputStream stream=new //
* FileInputStream("Images/raw/sound01.wav");
*
* int index = e.getKeyChar() - 'a';
*
* // System.out.println(index);
*
* File soundFile = new File("Images/raw/" + wavs[index]);
*
* AudioInputStream audioIn = AudioSystem
* .getAudioInputStream(soundFile);
*
* Clip clip = AudioSystem.getClip(); clip.open(audioIn); clip.start();
*
* } catch (Exception e2) { // TODO: handle exception
* e2.printStackTrace(); }
*
* }
*
* @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated
* method stub
*
* String str = String.valueOf(e.getKeyChar());
*
* // System.out.println(str);
*
* for (int i = 0; i < firstRow.length; i++) { String name =
* firstRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + "back.png"); firstRow[i].setIcon(icon); }
*
* }
*
* for (int i = 0; i < secondRow.length; i++) { String name =
* secondRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + "back.png"); secondRow[i].setIcon(icon); }
*
* }
*
* for (int i = 0; i < thirdRow.length; i++) { String name =
* thirdRow[i].getName();
*
* if (name.equals(str)) { ImageIcon icon = new ImageIcon("Images/" +
* str + "back.png"); thirdRow[i].setIcon(icon); }
*
* }
*
* } });
*/
this.add(panel);
initKeys();
// 添加下拉列表框
cmbMusic = new JComboBox(musics);
int x = this.getSize().width / 2 - cmbMusic.getSize().width / 2;
int y = 30;
// cmbMusic.setSize(120,25);
cmbMusic.setBounds(x, y, 120, 25);
// cmbMusic.setLocation(x, y);
// 添加一个选择项改变事件
cmbMusic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取选择项的索引
int index = cmbMusic.getSelectedIndex();
txtContent.setText(musiccontent[index]);
// 获取焦点
focus.requestFocus();
}
});
panel.add(cmbMusic);
// 添加多行文本框
txtContent = new JTextArea(4, 20);
Font font = txtContent.getFont();
txtContent.setFont(new Font(font.getName(), font.getStyle(), 25));
txtContent.setForeground(Color.blue);
int xt = this.getSize().width / 2 - txtContent.getSize().width / 2;
int yt = 60;
txtContent.setBounds(xt, yt, 300, 100);
panel.add(txtContent);
// 获取焦点文本
focus = new JTextField();
focus.setSize(100, 20);
focus.setLocation(-100, -20);
panel.add(focus);
focus.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
String str = String.valueOf(e.getKeyChar());
// System.out.println(str);
for (int i = 0; i < firstRow.length; i++) {
String name = firstRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str + ".png");
firstRow[i].setIcon(icon);
}
}
for (int i = 0; i < secondRow.length; i++) {
String name = secondRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str + ".png");
secondRow[i].setIcon(icon);
}
}
for (int i = 0; i < thirdRow.length; i++) {
String name = thirdRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str + ".png");
thirdRow[i].setIcon(icon);
}
}
try {
// InputStream stream=new
// FileInputStream("Images/raw/sound01.wav");
int index = e.getKeyChar() - 'a';
// System.out.println(index);
File soundFile = new File("Raw/" + wavs[index]);
AudioInputStream audioIn = AudioSystem
.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (Exception e2) {
// TODO: handle exception
// e2.printStackTrace();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
String str = String.valueOf(e.getKeyChar());
// System.out.println(str);
for (int i = 0; i < firstRow.length; i++) {
String name = firstRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str
+ "back.png");
firstRow[i].setIcon(icon);
}
}
for (int i = 0; i < secondRow.length; i++) {
String name = secondRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str
+ "back.png");
secondRow[i].setIcon(icon);
}
}
for (int i = 0; i < thirdRow.length; i++) {
String name = thirdRow[i].getName();
if (name.equals(str)) {
ImageIcon icon = new ImageIcon("Images/" + str
+ "back.png");
thirdRow[i].setIcon(icon);
}
}
// 获取按下的按钮的值
String key = String.valueOf(e.getKeyChar());
// 从文本框中获取第一个字符
String firststr = txtContent.getText().substring(0, 1);
// 比较,如果相等
if (key.equalsIgnoreCase(firststr)) {
// 则删除
String text = txtContent.getText();
String newstr = text.substring(1);
txtContent.setText(newstr);
}
// 从文本框中获取最后一个字符
int sum=0;
String laststr = txtContent.getText().substring(0, txtContent.getText().length()-1);
if(key.equalsIgnoreCase(laststr)){
for(int i=0;i<musics.length;i++){
i=musics.length;
}
}
// 删除所对应按下的字母
//removeLetter(e.getKeyChar(), txtContent.getText());
}
});
}
// 删除所对应按下的字母
private void removeLetter(char ch, String str) {
// 定义删除后的文本内容
String content = null;
// 将字符大写转换为小写
String Letter = str.toLowerCase();
if (Letter.charAt(0) == ch) {
content = str.substring(1);
txtContent.setText(content);
// System.out.println(content);
}
}
// 初始化所有的按钮
private void initKeys() {
// firstRow
JLabel lblQ = new JLabel();
lblQ.setSize(70, 100);
lblQ.setName("q");
ImageIcon IconQ = new ImageIcon("Images/q.png");
lblQ.setIcon(IconQ);
JLabel lblW = new JLabel();
lblW.setSize(70, 100);
lblW.setName("w");
ImageIcon IconW = new ImageIcon("Images/w.png");
lblW.setIcon(IconW);
JLabel lblE = new JLabel();
lblE.setSize(70, 100);
lblE.setName("e");
ImageIcon IconE = new ImageIcon("Images/e.png");
lblE.setIcon(IconE);
JLabel lblR = new JLabel();
lblR.setSize(70, 100);
lblR.setName("r");
ImageIcon IconR = new ImageIcon("Images/r.png");
lblR.setIcon(IconR);
JLabel lblT = new JLabel();
lblT.setSize(70, 100);
lblT.setName("t");
ImageIcon IconT = new ImageIcon("Images/t.png");
lblT.setIcon(IconT);
JLabel lblY = new JLabel();
lblY.setSize(70, 100);
lblY.setName("y");
ImageIcon IconY = new ImageIcon("Images/y.png");
lblY.setIcon(IconY);
JLabel lblU = new JLabel();
lblU.setSize(70, 100);
lblU.setName("u");
ImageIcon IconU = new ImageIcon("Images/u.png");
lblU.setIcon(IconU);
JLabel lblI = new JLabel();
lblI.setSize(70, 100);
lblI.setName("i");
ImageIcon IconI = new ImageIcon("Images/i.png");
lblI.setIcon(IconI);
JLabel lblO = new JLabel();
lblO.setSize(70, 100);
lblO.setName("o");
ImageIcon IconO = new ImageIcon("Images/o.png");
lblO.setIcon(IconO);
JLabel lblP = new JLabel();
lblP.setSize(70, 100);
lblP.setName("p");
ImageIcon IconP = new ImageIcon("Images/p.png");
lblP.setIcon(IconP);
// secondRow
JLabel lblA = new JLabel();
lblA.setSize(70, 100);
lblA.setName("a");
ImageIcon IconA = new ImageIcon("Images/a.png");
lblA.setIcon(IconA);
JLabel lblS = new JLabel();
lblS.setSize(70, 100);
lblS.setName("s");
ImageIcon IconS = new ImageIcon("Images/s.png");
lblS.setIcon(IconS);
JLabel lblD = new JLabel();
lblD.setSize(70, 100);
lblD.setName("d");
ImageIcon IconD = new ImageIcon("Images/d.png");
lblD.setIcon(IconD);
JLabel lblF = new JLabel();
lblF.setSize(70, 100);
lblF.setName("f");
ImageIcon IconF = new ImageIcon("Images/f.png");
lblF.setIcon(IconF);
JLabel lblG = new JLabel();
lblG.setSize(70, 100);
lblG.setName("g");
ImageIcon IconG = new ImageIcon("Images/g.png");
lblG.setIcon(IconG);
JLabel lblH = new JLabel();
lblH.setSize(70, 100);
lblH.setName("h");
ImageIcon IconH = new ImageIcon("Images/h.png");
lblH.setIcon(IconH);
JLabel lblJ = new JLabel();
lblJ.setSize(70, 100);
lblJ.setName("j");
ImageIcon IconJ = new ImageIcon("Images/j.png");
lblJ.setIcon(IconJ);
JLabel lblK = new JLabel();
lblK.setSize(70, 100);
lblK.setName("k");
ImageIcon IconK = new ImageIcon("Images/k.png");
lblK.setIcon(IconK);
JLabel lblL = new JLabel();
lblL.setSize(70, 100);
lblL.setName("l");
ImageIcon IconL = new ImageIcon("Images/l.png");
lblL.setIcon(IconL);
// thirdRow
JLabel lblZ = new JLabel();
lblZ.setSize(70, 100);
lblZ.setName("z");
ImageIcon IconZ = new ImageIcon("Images/z.png");
lblZ.setIcon(IconZ);
JLabel lblX = new JLabel();
lblX.setSize(70, 100);
lblX.setName("x");
ImageIcon IconX = new ImageIcon("Images/x.png");
lblX.setIcon(IconX);
JLabel lblC = new JLabel();
lblC.setSize(70, 100);
lblC.setName("c");
ImageIcon IconC = new ImageIcon("Images/c.png");
lblC.setIcon(IconC);
JLabel lblV = new JLabel();
lblV.setSize(70, 100);
lblV.setName("v");
ImageIcon IconV = new ImageIcon("Images/v.png");
lblV.setIcon(IconV);
JLabel lblB = new JLabel();
lblB.setSize(70, 100);
lblB.setName("b");
ImageIcon IconB = new ImageIcon("Images/b.png");
lblB.setIcon(IconB);
JLabel lblN = new JLabel();
lblN.setSize(70, 100);
lblN.setName("n");
ImageIcon IconN = new ImageIcon("Images/n.png");
lblN.setIcon(IconN);
JLabel lblM = new JLabel();
lblM.setSize(70, 100);
lblM.setName("m");
ImageIcon IconM = new ImageIcon("Images/m.png");
lblM.setIcon(IconM);
firstRow[0] = lblQ;
firstRow[1] = lblW;
firstRow[2] = lblE;
firstRow[3] = lblR;
firstRow[4] = lblT;
firstRow[5] = lblY;
firstRow[6] = lblU;
firstRow[7] = lblI;
firstRow[8] = lblO;
firstRow[9] = lblP;
secondRow[0] = lblA;
secondRow[1] = lblS;
secondRow[2] = lblD;
secondRow[3] = lblF;
secondRow[4] = lblG;
secondRow[5] = lblH;
secondRow[6] = lblJ;
secondRow[7] = lblK;
secondRow[8] = lblL;
thirdRow[0] = lblZ;
thirdRow[1] = lblX;
thirdRow[2] = lblC;
thirdRow[3] = lblV;
thirdRow[4] = lblB;
thirdRow[5] = lblN;
thirdRow[6] = lblM;
// 添加到panel中
for (int i = 0; i < firstRow.length; i++) {
int x = firstRow[i].getSize().width * i;
int y = 250;
firstRow[i].setLocation(x, y);
panel.add(firstRow[i]);
}
for (int i = 0; i < secondRow.length; i++) {
int x = secondRow[i].getSize().width * i + 35;
int y = 350;
secondRow[i].setLocation(x, y);
panel.add(secondRow[i]);
}
for (int i = 0; i < thirdRow.length; i++) {
int x = thirdRow[i].getSize().width * i + 70;
int y = 450;
thirdRow[i].setLocation(x, y);
panel.add(thirdRow[i]);
}
}
// 创建一个自己的panel类,继承JPanel
private class Mypanel extends JPanel {
// 重写面板的绘制方法
public void paintComponent(Graphics g) {
// 准备要绘制的背景图
ImageIcon bgImage = new ImageIcon("Images/bgpic.png");
// 得到图片对象
Image image = bgImage.getImage();
// 绘制图片
g.drawImage(image, 0, 0, this.getSize().width,
this.getSize().height, this);
}
}
}