import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QQ extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];//建立25個JButton
static JLabel labels[]=new JLabel [10];
public static void main(String args[])
{
QQ myfrm=new QQ();
String numbers[] =new String[25];
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(700,700);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
buttons[i].addActionListener(myfrm);
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p2 = new JPanel(flow); //實作 panel 2
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
int i,ran;
String numbers[] = new String[25];
String j;
for (i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
//上面迴圈是為了在numbers[ ] 裡面放進0~24的數字
for (i=0; i<=24;i++)
{
ran=(int)(Math.random()*(25-i)); // 產生亂數
buttons[i].setLabel(numbers[ran]); // 根據亂數,把numbers[ ] 裡面的數字 讓buttons[ ] 標題改變
//以下開始做對調
j=numbers[ran];
numbers[ran]=numbers[25-i-1];
numbers[25-i-1]=j;
}
}
}
TONY*Java*
塵世中一個迷糊小書僮
2015年5月14日 星期四
2015年5月8日 星期五
0508 --- 按鈕顯示亂數
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class homework6
{
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame("作業GridLayout)");
GridLayout f1=new GridLayout(4, 13);
Container c=frame.getContentPane();
c.setLayout(f1);
int [] num = new int[52]; //宣告一個數字陣列,用來記錄產生過的亂數
Arrays.fill(num,-1); //將陣列內容全都設為-1
int i=1;
do{
int j=(int)(Math.random()*9);
if(num[j] == 0) continue; //判斷是否亂數取出的數字是否出現過
num[j] = 0;//將出現過的數字索引值的陣列內容設為0,代表出現過
c.add(new JButton(""+j+""));
i++;
}while(i<=9);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class homework6
{
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame("作業GridLayout)");
GridLayout f1=new GridLayout(4, 13);
Container c=frame.getContentPane();
c.setLayout(f1);
int [] num = new int[52]; //宣告一個數字陣列,用來記錄產生過的亂數
Arrays.fill(num,-1); //將陣列內容全都設為-1
int i=1;
do{
int j=(int)(Math.random()*9);
if(num[j] == 0) continue; //判斷是否亂數取出的數字是否出現過
num[j] = 0;//將出現過的數字索引值的陣列內容設為0,代表出現過
c.add(new JButton(""+j+""));
i++;
}while(i<=9);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
2015年4月24日 星期五
4*4按鈕顯示數字
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(4,4));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[17];
for (int i = 1; i <= 16; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
button[10].setActionCommand("d10");
button[11].setActionCommand("d11");
button[12].setActionCommand("d12");
button[13].setActionCommand("d13");
button[14].setActionCommand("d14");
button[15].setActionCommand("d15");
button[16].setActionCommand("d16");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
String cmd10 = e.getActionCommand();
String cmd11 = e.getActionCommand();
String cmd12 = e.getActionCommand();
String cmd13 = e.getActionCommand();
String cmd14 = e.getActionCommand();
String cmd15 = e.getActionCommand();
String cmd16 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
if (cmd10 == "d10") {
System.out.println("10");
}
if (cmd11 == "d11") {
System.out.println("11");
}
if (cmd12 == "d12") {
System.out.println("12");
}
if (cmd13 == "d13") {
System.out.println("13");
}
if (cmd14 == "d14") {
System.out.println("14");
}
if (cmd15 == "d15") {
System.out.println("15");
}
if (cmd16 == "d16") {
System.out.println("16");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(4,4));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[17];
for (int i = 1; i <= 16; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
button[10].setActionCommand("d10");
button[11].setActionCommand("d11");
button[12].setActionCommand("d12");
button[13].setActionCommand("d13");
button[14].setActionCommand("d14");
button[15].setActionCommand("d15");
button[16].setActionCommand("d16");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
String cmd10 = e.getActionCommand();
String cmd11 = e.getActionCommand();
String cmd12 = e.getActionCommand();
String cmd13 = e.getActionCommand();
String cmd14 = e.getActionCommand();
String cmd15 = e.getActionCommand();
String cmd16 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
if (cmd10 == "d10") {
System.out.println("10");
}
if (cmd11 == "d11") {
System.out.println("11");
}
if (cmd12 == "d12") {
System.out.println("12");
}
if (cmd13 == "d13") {
System.out.println("13");
}
if (cmd14 == "d14") {
System.out.println("14");
}
if (cmd15 == "d15") {
System.out.println("15");
}
if (cmd16 == "d16") {
System.out.println("16");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2015年4月23日 星期四
0424 --- 5*5矩陣亂數產生
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];//建立25個JButton
static JLabel labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[] =new String[25];
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(700,700);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p2 = new JPanel(flow); //實作 panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
int i,ran;
String numbers[] = new String[25];
String j;
for (i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
//上面迴圈是為了在numbers[ ] 裡面放進0~24的數字
for (i=0; i<=24;i++)
{
ran=(int)(Math.random()*(25-i)); // 產生亂數
buttons[i].setLabel(numbers[ran]); // 根據亂數,把numbers[ ] 裡面的數字 讓buttons[ ] 標題改變
//以下開始做對調
j=numbers[ran];
numbers[ran]=numbers[25-i-1];
numbers[25-i-1]=j;
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];//建立25個JButton
static JLabel labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[] =new String[25];
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(700,700);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p2 = new JPanel(flow); //實作 panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
int i,ran;
String numbers[] = new String[25];
String j;
for (i=0;i<25;i++)
{
numbers[i]=String.valueOf(i);
}
//上面迴圈是為了在numbers[ ] 裡面放進0~24的數字
for (i=0; i<=24;i++)
{
ran=(int)(Math.random()*(25-i)); // 產生亂數
buttons[i].setLabel(numbers[ran]); // 根據亂數,把numbers[ ] 裡面的數字 讓buttons[ ] 標題改變
//以下開始做對調
j=numbers[ran];
numbers[ran]=numbers[25-i-1];
numbers[25-i-1]=j;
}
}
}
0424 --- 小算盤
import java.awt.*;
import java.awt.event.*;
public class calculating
{
private static Frame frm=new Frame("小算盤");
private static Panel pn1=new Panel(new GridLayout(4,3));
private static Panel pn2=new Panel(new GridLayout(4,1));
private static Label lab=new Label("0",Label.RIGHT);
//依序為清除、加、減、乘、除、等於
private static Button cn,ad,sub,mul,div,amo;
//0~9的數字
private static Button digits[]=new Button[10];
private static long num;//存放結果
private static byte op;//代表運算子
public static void main(String args[])
{
frm.setLayout(null);
frm.setBounds(450,250,160,180);
frm.setResizable(false);
lab.setBounds(20,30,120,20);
lab.setBackground(new Color(240,220,190));
pn1.setBounds(20,60,90,105);
pn2.setBounds(110,60,30,105);
//0~9數字鈕
for(int i=9;i>=0;i--){
digits[i]=new Button(Integer.toString(i));
pn1.add(digits[i]);
digits[i].addActionListener(new ActLis());
}
//清除鈕
cn=new Button("C");
pn1.add(cn);
cn.addActionListener(new ActLis());
//等於鈕
amo=new Button("=");
pn1.add(amo);
amo.addActionListener(new ActLis());
//加鈕
ad=new Button("+");
pn2.add(ad);
ad.addActionListener(new ActLis());
//減鈕
sub=new Button("-");
pn2.add(sub);
sub.addActionListener(new ActLis());
//乘鈕
mul=new Button("*");
pn2.add(mul);
mul.addActionListener(new ActLis());
//除鈕
div=new Button("/");
pn2.add(div);
div.addActionListener(new ActLis());
frm.addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
frm.add(lab);
frm.add(pn1);
frm.add(pn2);
frm.setVisible(true);
}
public static class ActLis implements ActionListener
{
public void actionPerformed(ActionEvent e)throws NumberFormatException,ArithmeticException{
long result;//存放由字串轉成的數值
Button btn=(Button) e.getSource();
try{
//處理數值1-9
for(int i=0;i<=9;i++){
if(btn==digits[i]){
output_digit(digits[i]);
break;
}
}
if(btn==cn){
result=0L;//把儲存的結果歸0
num=0L;
op=0;
lab.setText(Long.toString(num));
}else if(btn==ad){//加
save_num(ad);
op=1;
}else if(btn==sub){//減
save_num(sub);
op=2;
}else if(btn==mul){//乘
save_num(mul);
op=3;
}else if(btn==div){//除
save_num(div);
op=4;
}else if(btn==amo){
result=Long.parseLong(lab.getText());
switch(op){
case 1:
num+=result;
break;
case 2:
num-=result;
break;
case 3:
num*=result;
break;
case 4:
num/=result;
break;
default:
}
result=0L;
//輸出運算後的結果到顯示器
lab.setText(Long.toString(num));
}
}catch(NumberFormatException ne){
//捕捉例外
}catch(ArithmeticException ae){
//捕捉被除數是零的例外
}
}
//輸出數值到顯示器
private void output_digit(Button btn){
lab.setText(Long.toString(Long.parseLong(lab.getText()+btn.getLabel())));
}
//把第一組數值儲存起來
private void save_num(Button oper){
num=Long.parseLong(lab.getText());
lab.setText(Long.toString(0L));
}
}
}
import java.awt.event.*;
public class calculating
{
private static Frame frm=new Frame("小算盤");
private static Panel pn1=new Panel(new GridLayout(4,3));
private static Panel pn2=new Panel(new GridLayout(4,1));
private static Label lab=new Label("0",Label.RIGHT);
//依序為清除、加、減、乘、除、等於
private static Button cn,ad,sub,mul,div,amo;
//0~9的數字
private static Button digits[]=new Button[10];
private static long num;//存放結果
private static byte op;//代表運算子
public static void main(String args[])
{
frm.setLayout(null);
frm.setBounds(450,250,160,180);
frm.setResizable(false);
lab.setBounds(20,30,120,20);
lab.setBackground(new Color(240,220,190));
pn1.setBounds(20,60,90,105);
pn2.setBounds(110,60,30,105);
//0~9數字鈕
for(int i=9;i>=0;i--){
digits[i]=new Button(Integer.toString(i));
pn1.add(digits[i]);
digits[i].addActionListener(new ActLis());
}
//清除鈕
cn=new Button("C");
pn1.add(cn);
cn.addActionListener(new ActLis());
//等於鈕
amo=new Button("=");
pn1.add(amo);
amo.addActionListener(new ActLis());
//加鈕
ad=new Button("+");
pn2.add(ad);
ad.addActionListener(new ActLis());
//減鈕
sub=new Button("-");
pn2.add(sub);
sub.addActionListener(new ActLis());
//乘鈕
mul=new Button("*");
pn2.add(mul);
mul.addActionListener(new ActLis());
//除鈕
div=new Button("/");
pn2.add(div);
div.addActionListener(new ActLis());
frm.addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
frm.add(lab);
frm.add(pn1);
frm.add(pn2);
frm.setVisible(true);
}
public static class ActLis implements ActionListener
{
public void actionPerformed(ActionEvent e)throws NumberFormatException,ArithmeticException{
long result;//存放由字串轉成的數值
Button btn=(Button) e.getSource();
try{
//處理數值1-9
for(int i=0;i<=9;i++){
if(btn==digits[i]){
output_digit(digits[i]);
break;
}
}
if(btn==cn){
result=0L;//把儲存的結果歸0
num=0L;
op=0;
lab.setText(Long.toString(num));
}else if(btn==ad){//加
save_num(ad);
op=1;
}else if(btn==sub){//減
save_num(sub);
op=2;
}else if(btn==mul){//乘
save_num(mul);
op=3;
}else if(btn==div){//除
save_num(div);
op=4;
}else if(btn==amo){
result=Long.parseLong(lab.getText());
switch(op){
case 1:
num+=result;
break;
case 2:
num-=result;
break;
case 3:
num*=result;
break;
case 4:
num/=result;
break;
default:
}
result=0L;
//輸出運算後的結果到顯示器
lab.setText(Long.toString(num));
}
}catch(NumberFormatException ne){
//捕捉例外
}catch(ArithmeticException ae){
//捕捉被除數是零的例外
}
}
//輸出數值到顯示器
private void output_digit(Button btn){
lab.setText(Long.toString(Long.parseLong(lab.getText()+btn.getLabel())));
}
//把第一組數值儲存起來
private void save_num(Button oper){
num=Long.parseLong(lab.getText());
lab.setText(Long.toString(0L));
}
}
}
0424 --- 踩地雷遊戲
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game extends JFrame implements ActionListener{
static int mapHeight = 9, mapWidth = 9, bombNum = 10;
static int frameWidth = 400, frameHeight = 400;
private boolean[][] isTurned;
private boolean gameOver;
private boolean gameRunning;
private int[][] aroundBombNum;
private boolean[][] map;//炸彈or空地
private boolean[][] isPressed;
private JButton[][] buttons;
private JPanel centerButtonPanel;
private JLabel gameMessage;
public Game()
{
super("踩地雷");
setSize(frameWidth, frameHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
gameMessage = new JLabel("遊戲進行中");//訊息顯示
add(gameMessage, BorderLayout.NORTH);
centerButtonPanel = new JPanel();
centerButtonPanel.setLayout(new GridLayout(mapHeight, mapWidth));
buttons = new JButton[mapHeight][mapWidth];
//按鈕
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.WHITE);
buttons[i][j].setActionCommand(i+" "+j);
centerButtonPanel.add(buttons[i][j]);
}
add(centerButtonPanel, BorderLayout.CENTER);
JButton re = new JButton("重玩");
re.setActionCommand("regame");
re.addActionListener(this);
add(re, BorderLayout.SOUTH);
gameRunning = false;
}
public void resetGame(int sx,int sy)//清空地圖、隨機地圖、重設按鈕文字
{
gameOver = false;
aroundBombNum = new int[mapHeight][mapWidth];//清空地圖
map = new boolean[mapHeight][mapWidth];
isPressed = new boolean[mapHeight][mapWidth];
isTurned = new boolean[mapHeight][mapWidth];
int bombCount = 0;
while(bombCount < bombNum)
{
int x = (int)(Math.random()*mapHeight);
int y = (int)(Math.random()*mapWidth);
if(sx!=x|sy!=y)
if(!map[x][y])//沒炸彈
{
bombCount++;//放了炸彈
map[x][y] = true;
//向周圍宣布有炸彈
for(int i=x-1; i<=x+1; i++)
for(int j = y-1; j<=y+1; j++)
{
if(i>=0&&i<mapHeight&&j>=0&&j<mapWidth)
aroundBombNum[i][j]++;
}
}
}
System.out.println("地圖建置成功");
printMap();
}
private void clean()
{
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j].setText("");
buttons[i][j].setBackground(Color.WHITE);
}
}
private void printMap()
{
System.out.println("Bombs");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
if(map[i][j])
System.out.print("*");
else
System.out.print("_");
}
System.out.println();
}
System.out.println("Arounds");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
System.out.print(aroundBombNum[i][j]);
}
System.out.println();
}
}
public void freshMessage(String newMessage)
{
System.out.println(newMessage);
gameMessage.setText(newMessage);
}
public void click(int x, int y)//炸彈: 爆炸| 土地 0擴散 1~8不擴散
{
if(map[x][y])//炸彈
{
freshMessage("踩到地雷! 爆炸!");
buttons[x][y].setBackground(Color.RED);
gameOver = true;
return ;
}
else
if(aroundBombNum[x][y]!=0)
{
turn(x,y);
}
else
{
int[][] direct = {{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};//上 左 下 右 左上 左下 右上 右下
int[] queue_x = new int[mapHeight*mapWidth];
int[] queue_y = new int[mapHeight*mapWidth];
int pop = 0, push = 0;
queue_x[push] = x;//填入第一個
queue_y[push] = y;
push++;
while(pop < push)
{
int tx = queue_x[pop];
int ty = queue_y[pop];
//如果 此格是0就繼續擴散
if(aroundBombNum[tx][ty]==0)
for(int s=0; s<8; s++)
{
int fx = tx+direct[s][0];
int fy = ty+direct[s][1];
boolean spread = false;
if(inRange(fx,fy)&&!isTurned[fx][fy])
{
isTurned[fx][fy] = true;
queue_x[push] = fx;
queue_y[push] = fy;
push++;
}
}
pop++;
/*System.out.println("Pop"+pop);
for(int i=0; i<push; i++)
{
System.out.print(" "+"["+queue_x[i]+"]"+ "["+queue_y[i]+"]");
}
System.out.println();*/
}
for(int i=0; i<push; i++)
turn(queue_x[i],queue_y[i]);
}
}
private static boolean inRange(int x, int y)
{
return (x>=0&&x<mapHeight&&y>=0&&y<mapWidth);
}
public void turn(int x, int y)//翻開
{
buttons[x][y].setBackground(Color.LIGHT_GRAY);
if(aroundBombNum[x][y]>0)
buttons[x][y].setText(Integer.toString(aroundBombNum[x][y]));
isTurned[x][y] = true;
isPressed[x][y] = true;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command =e.getActionCommand();
if(command.equals("regame"))
{
clean();
gameRunning = false;
freshMessage("遊戲進行中");
}
else
{
String[] press = command.split(" ");
int x = Integer.parseInt(press[0]);
int y = Integer.parseInt(press[1]);
if(!gameRunning)
{
resetGame(x,y);
gameRunning = true;
}
if(!isPressed[x][y]&&!gameOver)
{
System.out.println("Invoke click "+x+" "+y);
click(x,y);
isPressed[x][y] = true;
}
}
}
public static void main(String[] args)
{
Game frame = new Game();
frame.setVisible(true);
}
}
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game extends JFrame implements ActionListener{
static int mapHeight = 9, mapWidth = 9, bombNum = 10;
static int frameWidth = 400, frameHeight = 400;
private boolean[][] isTurned;
private boolean gameOver;
private boolean gameRunning;
private int[][] aroundBombNum;
private boolean[][] map;//炸彈or空地
private boolean[][] isPressed;
private JButton[][] buttons;
private JPanel centerButtonPanel;
private JLabel gameMessage;
public Game()
{
super("踩地雷");
setSize(frameWidth, frameHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
gameMessage = new JLabel("遊戲進行中");//訊息顯示
add(gameMessage, BorderLayout.NORTH);
centerButtonPanel = new JPanel();
centerButtonPanel.setLayout(new GridLayout(mapHeight, mapWidth));
buttons = new JButton[mapHeight][mapWidth];
//按鈕
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.WHITE);
buttons[i][j].setActionCommand(i+" "+j);
centerButtonPanel.add(buttons[i][j]);
}
add(centerButtonPanel, BorderLayout.CENTER);
JButton re = new JButton("重玩");
re.setActionCommand("regame");
re.addActionListener(this);
add(re, BorderLayout.SOUTH);
gameRunning = false;
}
public void resetGame(int sx,int sy)//清空地圖、隨機地圖、重設按鈕文字
{
gameOver = false;
aroundBombNum = new int[mapHeight][mapWidth];//清空地圖
map = new boolean[mapHeight][mapWidth];
isPressed = new boolean[mapHeight][mapWidth];
isTurned = new boolean[mapHeight][mapWidth];
int bombCount = 0;
while(bombCount < bombNum)
{
int x = (int)(Math.random()*mapHeight);
int y = (int)(Math.random()*mapWidth);
if(sx!=x|sy!=y)
if(!map[x][y])//沒炸彈
{
bombCount++;//放了炸彈
map[x][y] = true;
//向周圍宣布有炸彈
for(int i=x-1; i<=x+1; i++)
for(int j = y-1; j<=y+1; j++)
{
if(i>=0&&i<mapHeight&&j>=0&&j<mapWidth)
aroundBombNum[i][j]++;
}
}
}
System.out.println("地圖建置成功");
printMap();
}
private void clean()
{
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j].setText("");
buttons[i][j].setBackground(Color.WHITE);
}
}
private void printMap()
{
System.out.println("Bombs");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
if(map[i][j])
System.out.print("*");
else
System.out.print("_");
}
System.out.println();
}
System.out.println("Arounds");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
System.out.print(aroundBombNum[i][j]);
}
System.out.println();
}
}
public void freshMessage(String newMessage)
{
System.out.println(newMessage);
gameMessage.setText(newMessage);
}
public void click(int x, int y)//炸彈: 爆炸| 土地 0擴散 1~8不擴散
{
if(map[x][y])//炸彈
{
freshMessage("踩到地雷! 爆炸!");
buttons[x][y].setBackground(Color.RED);
gameOver = true;
return ;
}
else
if(aroundBombNum[x][y]!=0)
{
turn(x,y);
}
else
{
int[][] direct = {{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};//上 左 下 右 左上 左下 右上 右下
int[] queue_x = new int[mapHeight*mapWidth];
int[] queue_y = new int[mapHeight*mapWidth];
int pop = 0, push = 0;
queue_x[push] = x;//填入第一個
queue_y[push] = y;
push++;
while(pop < push)
{
int tx = queue_x[pop];
int ty = queue_y[pop];
//如果 此格是0就繼續擴散
if(aroundBombNum[tx][ty]==0)
for(int s=0; s<8; s++)
{
int fx = tx+direct[s][0];
int fy = ty+direct[s][1];
boolean spread = false;
if(inRange(fx,fy)&&!isTurned[fx][fy])
{
isTurned[fx][fy] = true;
queue_x[push] = fx;
queue_y[push] = fy;
push++;
}
}
pop++;
/*System.out.println("Pop"+pop);
for(int i=0; i<push; i++)
{
System.out.print(" "+"["+queue_x[i]+"]"+ "["+queue_y[i]+"]");
}
System.out.println();*/
}
for(int i=0; i<push; i++)
turn(queue_x[i],queue_y[i]);
}
}
private static boolean inRange(int x, int y)
{
return (x>=0&&x<mapHeight&&y>=0&&y<mapWidth);
}
public void turn(int x, int y)//翻開
{
buttons[x][y].setBackground(Color.LIGHT_GRAY);
if(aroundBombNum[x][y]>0)
buttons[x][y].setText(Integer.toString(aroundBombNum[x][y]));
isTurned[x][y] = true;
isPressed[x][y] = true;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command =e.getActionCommand();
if(command.equals("regame"))
{
clean();
gameRunning = false;
freshMessage("遊戲進行中");
}
else
{
String[] press = command.split(" ");
int x = Integer.parseInt(press[0]);
int y = Integer.parseInt(press[1]);
if(!gameRunning)
{
resetGame(x,y);
gameRunning = true;
}
if(!isPressed[x][y]&&!gameOver)
{
System.out.println("Invoke click "+x+" "+y);
click(x,y);
isPressed[x][y] = true;
}
}
}
public static void main(String[] args)
{
Game frame = new Game();
frame.setVisible(true);
}
}
0424 --- 按按鈕顯示數字
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));
JButton button[]=new JButton[10];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));
JButton button[]=new JButton[10];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
訂閱:
文章 (Atom)