如何把這個JAVA程式改成Android可用的程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main2 extends JFrame implements MouseListener,ActionListener
{ //大小//寬 //高 //機率
private int w=50,abcw=5,abch=5,bp=20;
private int n,m,x,y,k,b,p,yes,no,ze;
private int xyz[][];
private String r[]={"","!!!","?"};
private int t[]={10,10,-20};
private JButton button[][];
private JButton buttonRE ;
private JTextField ps ,sethy,setwx,setbp;
private JLabel l1,l2,l3;
private Container c;
public static void main(String args[]){
Main2 app=new Main2();
}
public void actionPerformed(ActionEvent e){
this.change();
this.re();
}
public Main2(){
super("小遊戲:踩地雷");
c=getContentPane();
c.setLayout(null);
l1=new JLabel("||寬度||");
l1.setBounds(0, w*3, w*2, w);
l2=new JLabel("||高度||");
l2.setBounds(0, w*5, w*2, w);
l3=new JLabel("地雷密度:");
l3.setBounds(w*2, w, w*3, w);
setwx=new JTextField("5");
setwx.setBounds(0,w*4, w*2, w);
sethy=new JTextField("5");
sethy.setBounds(0, w*6, w*2, w);
setbp=new JTextField("20");
setbp.setBounds(w*5, w, w*2, w);
ps=new JTextField("");
ps.setBounds(0, 0, w*7, w);
ps.setBackground(Color.lightGray);
buttonRE=new JButton();
buttonRE.setBounds(0, w, w*2,w*2);
buttonRE.setMargin(new java.awt.Insets(0, 0, 0, 0));
buttonRE.addActionListener(this);
c.add(buttonRE);
c.add(ps);
c.add(setwx);
c.add(sethy);
c.add(setbp);
c.add(l1);
c.add(l2);
c.add(l3);
this.re();
setLocation(100,100);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void change(){
for(int i=1;i<=abcw;i++){
for(int j=1;j<=abch;j++){
button[i][j].setVisible(false);
}
}
try{
abcw=Integer.parseInt(setwx.getText());
if(abcw<3){
abcw=3;
setwx.setText(""+abcw);}
}catch(NumberFormatException e){
abcw=3;
setwx.setText(""+abcw);}
try{
abch=Integer.parseInt(sethy.getText());
if(abch<3 ){
abch=3;
sethy.setText(""+abch);}
}catch(NumberFormatException e){
abch=3;
sethy.setText(""+abch);}
try{
bp=Integer.parseInt(setbp.getText());
if(bp<0 || bp>100){
bp=20;
setbp.setText(""+bp);}
}catch(NumberFormatException e){
bp=20;
setbp.setText(""+bp);}
}
public void re(){
this.xyz=new int[abcw+2][abch+2];
this.button=new JButton[abcw+2][abch+2];
n=w*2;
m=w*2;
for(int i=1;i<=abcw;i++){
for(int j=1;j<=abch;j++){
button[i][j]=new JButton();
button[i][j].setLocation(n, m);
button[i][j].setSize(w,w);
button[i][j].setMargin(new java.awt.Insets(0, 0, 0, 0));
c.add(button[i][j]);
button[i][j].addMouseListener(this);
m+=w;
}
n+=w;
m=w*2;
}
b=abch*abcw*bp/100;
p=abch*abcw-b;
setSize((abcw+4)*w+8,(abch+4)*w+34);//調整視窗大小
c.setBackground(Color.gray);
buttonRE.setText("重來");
ps.setText("寬&高最小3,密度0~100%");
for(int i=0;i<=abcw+1;i++){
for(int j=0;j<=abch+1;j++){
if(i==0 || i==abcw+1 || j==0 || j==abch+1){
this.xyz[i][j]=200;}
else{
this.xyz[i][j]=0;//不含邊線歸0
button[i][j].setBackground(Color.lightGray);
button[i][j].setText("");}
}
}
int g;
for(int i=1;i<=b;i++){
do
g = (int)(Math.random() * (abch*abcw));//隨機埋地雷
while (xyz[g%abcw+1][g/abcw+1] >= 9);
xyz[g%abcw+1][g/abcw+1]=9;//本位
xyz[g%abcw+2][g/abcw+1]+=1;//東
xyz[g%abcw+1][g/abcw+2]+=1;//南
xyz[g%abcw+0][g/abcw+1]+=1;//西
xyz[g%abcw+1][g/abcw+0]+=1;//北
xyz[g%abcw+2][g/abcw+0]+=1;//東北
xyz[g%abcw+2][g/abcw+2]+=1;//東南
xyz[g%abcw+0][g/abcw+2]+=1;//西南
xyz[g%abcw+0][g/abcw+0]+=1;//西北
//button[g%abcw+1][g/abcw+1].setBackground(Color.red);//判斷用
}
for(int i=1;i<=abcw;i++){
for(int j=1;j<=abch;j++){
if(xyz[i][j]>8){
xyz[i][j]=9;
}
//button[i][j].setText(""+xyz[i][j]);判斷用
}
}
}
public void boom(){
yes=0;
no=0;
c.setBackground(Color.WHITE);
for(int i=1;i<=abcw;i++){
for(int j=1;j<=abch;j++){
if(xyz[i][j]==9 || xyz[i][j]==29){
button[i][j].setBackground(Color.red);
button[i][j].setText("爆");}
if(xyz[i][j]==19){
button[i][j].setBackground(Color.red);
button[i][j].setText("對");
yes+=1;}
if(xyz[i][j]<19 && xyz[i][j]>9){
button[i][j].setBackground(Color.yellow);
button[i][j].setText("錯");
no+=1;}
xyz[i][j]+=200;
}
}
buttonRE.setText("再來");
ps.setText("失敗了!!答對"+yes+"個=答錯"+no+"個");
}
public void win(){
buttonRE.setText("繼續");
ps.setText("恭喜完成任務!!");
for(int i=1;i<=abcw;i++){
for(int j=1;j<=abch;j++){
if(xyz[i][j]%10==9){
button[i][j].setBackground(Color.red);
button[i][j].setText("對");}
xyz[i][j]+=200;
}
}
c.setBackground(Color.green);
}
public void mouseClicked(MouseEvent e) {
x=(this.getMousePosition().x-3-w*2)/w+1;
y=(this.getMousePosition().y-29-w*2)/w+1;
if(xyz[x][y]>100){
;//按過了、無動作
}else{
if(e.getButton()==2 || e.getButton()==3){
k=xyz[x][y]/10;//更換模式
xyz[x][y]+=t[(k)%3];
button[x][y].setText(r[(k+1)%3]);
if(k==0){
b-=1;
ps.setText("預估剩餘 "+b+" 個地雷");}
if(k==1){
b+=1;
ps.setText("預估剩餘 "+b+" 個地雷");}
}else{
if(xyz[x][y]==9){
this.boom();//爆了
button[x][y].setBackground(Color.blue);
}else{
this.rerun(x, y);
}
}
}
if(p<1)
this.win();
}
public int rerun(int x ,int y){
if(xyz[x][y]>9){
return 1 ;}
else{
if(xyz[x][y]==0){
ps.setText("預估剩餘 "+b+" 個地雷");//顯示安全
button[x][y].setBackground(Color.pink);
button[x][y].setText(""+xyz[x][y]);
xyz[x][y]=200;
p-=1;
this.rerun(x-1, y-1);
this.rerun(x, y-1);
this.rerun(x+1, y-1);
this.rerun(x-1, y);
this.rerun(x+1, y);
this.rerun(x-1, y+1);
this.rerun(x, y+1);
this.rerun(x+1, y+1);
return 1;
}
else{
ps.setText("預估剩餘 "+b+" 個地雷");//顯示安全
button[x][y].setBackground(Color.pink);
button[x][y].setText(""+xyz[x][y]);
xyz[x][y]=200;
p-=1;
return 1;
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}