小弟刚刚学JAVA不久自己编了一段程序 想改成APPLET却总失败哪位大大能帮忙改下这段APPLICAITON谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class MainBomb extends JFrame 
{
private JButton keyButton[]=new JButton[9];

int b=0;int c=0;    public MainBomb()
    {
    
     Listener nListener = new Listener();
     Listener2 bListener = new Listener2();
    
    
     Container container = getContentPane();
container.setLayout(new BorderLayout());
JPanel p1=new JPanel(); 
p1.setLayout(new GridLayout(3,3)); 
int k= (int)(Math.random()*9);//set the Button and Bomb

  for(int i=0;i<=8;i++)
        {
  keyButton[i]=new JButton();
         p1.add(keyButton[i]);
         keyButton[i].setBackground(Color.gray); 
      if(i==k)
      {
      keyButton[k].addActionListener(bListener);
      }
      else
      {
    keyButton[i].addActionListener(nListener);
      }
         
           }
  
 
   
          container.add(p1,BorderLayout.CENTER);
    }
         
          
         
 
   public static void main(String[] args)
   {
         String strLine = System.getProperty("line.separator"); 
         JOptionPane.showMessageDialog(null,"Welcome to Mini-MineSweeper"+ strLine +
          "The object of the game is click on all"  + strLine +
          "the squares EXCEPT the one with the bomb." + strLine +
          "(There is only one bomb).To choose a square" + strLine +
          "To display please simply click on the square.",
          "Message",JOptionPane.INFORMATION_MESSAGE);
         
       
         MainBomb frame = new MainBomb ();//set the main frame
         frame.setTitle("Boom!");
         frame.setSize(400,200);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setVisible(true);
        
    
     Dimension screenSize=
     Toolkit.getDefaultToolkit().getScreenSize(); //let the frame be center
     int screenWidth = screenSize.width;
            int screenHight = screenSize.height;
            
            int x=(screenWidth - frame.getWidth())/2;
            int y=(screenHight - frame.getHeight())/2;
            
            frame.setLocation(x,y);
            
           
     
     }   private class  Listener implements ActionListener//the win listener
    {
 public void actionPerformed(ActionEvent e)
 {
JButton n = (JButton)e.getSource();
n.setBackground(Color.white);
c++;
 if (c==8){   
 face2 a2= new  face2();//set the happy face
  
    a2.setTitle("Boom!");
         a2.setSize(400,200);
         a2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         a2.setVisible(true);
        
    
     Dimension screenSize2=
     Toolkit.getDefaultToolkit().getScreenSize();
     int screenWidth2 = screenSize2.width;
            int screenHight2 = screenSize2.height;
            
            int x2=(screenWidth2 - a2.getWidth())/2;
            int y2=(screenHight2 - a2.getHeight())/2;
            
           a2.setLocation(x2,y2);
           setVisible(false); 
        
  int chose1 = JOptionPane.showConfirmDialog (null,"Congratulations!You win!"+
"Would you like to play again?","Message",JOptionPane.YES_NO_OPTION);// the yes or no choose   if (chose1==JOptionPane.YES_OPTION)
  {
  a2.setVisible(false); 
  c=0;
MainBomb frame = new MainBomb ();//the game  starts again
 
         frame.setTitle("Boom!");
         frame.setSize(400,200);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setVisible(true);
         Dimension screenSize=
     Toolkit.getDefaultToolkit().getScreenSize();
     int screenWidth = screenSize.width;
            int screenHight = screenSize.height;
            
            int x=(screenWidth - frame.getWidth())/2;
            int y=(screenHight - frame.getHeight())/2;
            
            frame.setLocation(x,y);
            
 }
  else
  System.exit(0); }
 }
    }
   
   private class  Listener2 implements ActionListener//the lose listener
   {
 public void actionPerformed(ActionEvent e)
 {
JButton n = (JButton)e.getSource();
n.setBackground(Color.black);
b++;
 if (b==1&&c!=8){
 face a1= new  face();//set the sad face
  
    a1.setTitle("Boom!");
         a1.setSize(400,200);
         a1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         a1.setVisible(true);
         
    
     Dimension screenSize1=
     Toolkit.getDefaultToolkit().getScreenSize();
     int screenWidth1 = screenSize1.width;
            int screenHight1= screenSize1.height;
            
            int x1=(screenWidth1 - a1.getWidth())/2;
            int y1=(screenHight1 - a1.getHeight())/2;
            
           a1.setLocation(x1,y1);
           
         
          
  int chose = JOptionPane.showConfirmDialog (null,"BOOooM!!!"+
  "Would you like to play again?","Message",JOptionPane.YES_NO_OPTION);
  setVisible(false); 

  if (chose==JOptionPane.YES_OPTION)
  {
  a1.setVisible(false); 

  c=0;
 
  MainBomb frame = new MainBomb ();
 
         frame.setTitle("Boom!");
         frame.setSize(400,200);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setVisible(true);
         Dimension screenSize=
     Toolkit.getDefaultToolkit().getScreenSize();
     int screenWidth = screenSize.width;
            int screenHight= screenSize.height;
            
            int x=(screenWidth - frame.getWidth())/2;
            int y=(screenHight - frame.getHeight())/2;
            
            frame.setLocation(x,y);
 
  
            
 }
  else
  System.exit(0); }
 }
   }
      class face extends JFrame//draw the happy face
   {
   public face()
   {
  
   getContentPane().add(new Ovals());
       
      }
   class Ovals extends JPanel
 {
 public void paintComponent(Graphics g)
  { 
  super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
  g.setColor(Color.red);
  g.fillOval(120,10,30,30);
  g.fillOval(230,10,30,30);
  g.drawArc(110,100,160, 100, 0, 180);
  }
 
  } 
   }
   
   class face2 extends JFrame//draw the sad face
   {
   public face2()
   {
  
   getContentPane().add(new Ovals2());
       
      }
   class Ovals2 extends JPanel
 {
 public void paintComponent(Graphics g)
  { 
  super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
  g.setColor(Color.red);
  g.fillOval(120,10,30,30);
  g.fillOval(230,10,30,30);
  g.drawArc(110,50,160, 100, 180, 180);
  }
 
  } 
   }
}

解决方案 »

  1.   

    PS:我改成APPLET后程序无法实现循环
      

  2.   

    初学Java,那最好通过阅读别人的源代码自己找出解决问题的方法,给你个例子 你的JDK安装路径\demo\jfc\SwingSet2,一个既能在浏览器中以APPLET方式运行(双击网页文件SwingSet2.html)又能像普通桌面环境下运行的程序一样运行(双击SwingSet2.jar)的经典例子。
      

  3.   


    我现在是能改成APPLET并运行 但这个程序要求是程序结束后循环改成APPLET后这个循环就实现不了了
      

  4.   

    你代码中没有Applet部分,改后的代码贴出来。
      

  5.   


    APPLET已经改的很乱了 彻底晕了。。我还有个疑问就是 一般都是把MAIN函数转换成INTT但原来那个MainBomb()函数怎么办呢??在APPLET里改成什么呢?
      

  6.   

    这个是现在改的 我还有问题就是 APPLET本身有个框体 那每次扫雷成功失败后出现的笑脸怎么画在APPLET的框体上呢?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;public class MyApplets extends JApplet 
    {

    private JButton keyButton[]=new JButton[9];

    int b=0;int c=0;


    public void init()
       {
     
             String strLine = System.getProperty("line.separator"); 
             JOptionPane.showMessageDialog(null,"Welcome to Mini-MineSweeper"+ strLine +
              "The object of the game is click on all"  + strLine +
              "the squares EXCEPT the one with the bomb." + strLine +
              "(There is only one" +
              " bomb).To choose a square" + strLine +
              "To display please simply click on the square.",
              "Message",JOptionPane.INFORMATION_MESSAGE);
             
           
              
       }
       
     
      
       
        
    public MyApplets()
        {
        
         Listener nListener = new Listener();
         Listener2 bListener = new Listener2();
        
        
         Container container = getContentPane();
    container.setLayout(new BorderLayout());
    JPanel p1=new JPanel(); 
    p1.setLayout(new GridLayout(3,3)); 
    int k= (int)(Math.random()*9);//set the Button and Bomb

      for(int i=0;i<=8;i++)
            {
      keyButton[i]=new JButton();
             p1.add(keyButton[i]);
             keyButton[i].setBackground(Color.gray); 
          if(i==k)
          {
          keyButton[k].addActionListener(bListener);
          }
          else
          {
        keyButton[i].addActionListener(nListener);
          }
             
               }
      
     
       
              container.add(p1,BorderLayout.CENTER);
        }

             
     
       
          public class  Listener implements ActionListener//the win listener
        {
      
     public void actionPerformed(ActionEvent e)
     {
    JButton n = (JButton)e.getSource();
    n.setBackground(Color.white);
    c++;
     if (c==8){   
     face2 a2= new  face2();//set the happy face
      
        a2.setTitle("Boom!");
             a2.setSize(400,200);
             a2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
             a2.setVisible(true);
            
        
         Dimension screenSize2=
         Toolkit.getDefaultToolkit().getScreenSize();
         int screenWidth2 = screenSize2.width;
                int screenHight2 = screenSize2.height;
                
                int x2=(screenWidth2 - a2.getWidth())/2;
                int y2=(screenHight2 - a2.getHeight())/2;
                
               a2.setLocation(x2,y2);
               setVisible(false); 
            
      int chose1 = JOptionPane.showConfirmDialog (null,"Congratulations!You win!"+
    "Would you like to play again?","Message",JOptionPane.YES_NO_OPTION);// the yes or no choose   if (chose1==JOptionPane.YES_OPTION)
      {
      a2.setVisible(false); 
      c=0;
      

      JFrame MyApplet = new JFrame ();//set the main frame
             MyApplet.setTitle("Boom!");
             MyApplet.setSize(400,200);
             MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
             MyApplet.setVisible(true);
            
        
         Dimension screenSize=
         Toolkit.getDefaultToolkit().getScreenSize(); //let the frame be center
         int screenWidth = screenSize.width;
                int screenHight = screenSize.height;
                
                int x=(screenWidth - MyApplet.getWidth())/2;
                int y=(screenHight - MyApplet.getHeight())/2;
                
                MyApplet.setLocation(x,y);

                
     }
      else
      System.exit(0); }
     }
      
        }
       
       public class  Listener2 implements ActionListener//the lose listener
       {
     public void actionPerformed(ActionEvent e)
     {
    JButton n = (JButton)e.getSource();
    n.setBackground(Color.black);
    b++;
     if (b==1&&c!=8){
     face a1= new  face();//set the sad face
      
        a1.setTitle("Boom!");
             a1.setSize(400,200);
             a1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
             a1.setVisible(true);
             
        
         Dimension screenSize1=
         Toolkit.getDefaultToolkit().getScreenSize();
         int screenWidth1 = screenSize1.width;
                int screenHight1= screenSize1.height;
                
                int x1=(screenWidth1 - a1.getWidth())/2;
                int y1=(screenHight1 - a1.getHeight())/2;
                
               a1.setLocation(x1,y1);
               
             
              
      int chose = JOptionPane.showConfirmDialog (null,"BOOooM!!!"+
      "Would you like to play again?","Message",JOptionPane.YES_NO_OPTION);
      setVisible(false); 

      if (chose==JOptionPane.YES_OPTION)
      {
      a1.setVisible(false); 

      c=0;
     
      JFrame MyApplet = new JFrame ();//set the main frame
             MyApplet.setTitle("Boom!");
             MyApplet.setSize(400,200);
             MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
             MyApplet.setVisible(true);
            
        
         Dimension screenSize=
         Toolkit.getDefaultToolkit().getScreenSize(); //let the frame be center
         int screenWidth = screenSize.width;
                int screenHight = screenSize.height;
                
                int x=(screenWidth - MyApplet.getWidth())/2;
                int y=(screenHight - MyApplet.getHeight())/2;
                
                MyApplet.setLocation(x,y);
     
      
                
     }
      else
      System.exit(0); }
     }
       }
           class face extends JFrame//draw the happy face
       {
       public face()
       {
      
       getContentPane().add(new Ovals());
           
          }
       class Ovals extends JPanel
     {
     public void paintComponent(Graphics g)
      { 
      super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
      g.setColor(Color.red);
      g.fillOval(120,10,30,30);
      g.fillOval(230,10,30,30);
      g.drawArc(110,100,160, 100, 0, 180);
      }
     
      } 
       }
       
      class face2 extends JFrame//draw the sad face
       {
       public face2()
       {
      
       getContentPane().add(new Ovals2());
           
          }
       class Ovals2 extends JPanel
     {
     public void paintComponent(Graphics g)
      { 
      super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
      g.setColor(Color.red);
      g.fillOval(120,10,30,30);
      g.fillOval(230,10,30,30);
      g.drawArc(110,50,160, 100, 180, 180);
      }
     
      } 
       }

       
    }
                  
            
      

  7.   

    是够晕,先格式化一下:import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;public class MyApplets extends JApplet { private JButton keyButton[] = new JButton[9];
    int b = 0;
    int c = 0; public void init() {
    String strLine = System.getProperty("line.separator");
    JOptionPane.showMessageDialog(null, "Welcome to Mini-MineSweeper" + strLine
    + "The object of the game is click on all" + strLine
    + "the squares EXCEPT the one with the bomb." + strLine
    + "(There is only one" + " bomb).To choose a square" + strLine
    + "To display please simply click on the square.", "Message",
    JOptionPane.INFORMATION_MESSAGE);
    } public MyApplets() {
    Listener nListener = new Listener();
    Listener2 bListener = new Listener2();
    Container container = getContentPane();
    container.setLayout(new BorderLayout());
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 3));
    int k = (int)(Math.random() * 9);// set the Button and Bomb
    for (int i = 0; i <= 8; i++) {
    keyButton[i] = new JButton();
    p1.add(keyButton[i]);
    keyButton[i].setBackground(Color.gray);
    if (i == k) {
    keyButton[k].addActionListener(bListener);
    } else {
    keyButton[i].addActionListener(nListener);
    }
    }
    container.add(p1, BorderLayout.CENTER);
    } public class Listener implements ActionListener {  //the win listener public void actionPerformed(ActionEvent e) {
    JButton n = (JButton)e.getSource();
    n.setBackground(Color.white);
    c++;
    if (c == 8) {
    face2 a2 = new face2();// set the happy face
    a2.setTitle("Boom!");
    a2.setSize(400, 200);
    a2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    a2.setVisible(true);
    Dimension screenSize2 = Toolkit.getDefaultToolkit().getScreenSize();
    int screenWidth2 = screenSize2.width;
    int screenHight2 = screenSize2.height;
    int x2 = (screenWidth2 - a2.getWidth()) / 2;
    int y2 = (screenHight2 - a2.getHeight()) / 2;
    a2.setLocation(x2, y2);
    setVisible(false);
    int chose1 = JOptionPane.showConfirmDialog(null,
    "Congratulations!You win!"
    + "Would you like to play again?",
    "Message", JOptionPane.YES_NO_OPTION);// the yes
    // or no
    // choose
    if (chose1 == JOptionPane.YES_OPTION) {
    a2.setVisible(false);
    c = 0;
    JFrame MyApplet = new JFrame();// set the main frame
    MyApplet.setTitle("Boom!");
    MyApplet.setSize(400, 200);
    MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyApplet.setVisible(true);
    Dimension screenSize = Toolkit.getDefaultToolkit()
    .getScreenSize(); // let the frame be
    // center
    int screenWidth = screenSize.width;
    int screenHight = screenSize.height;
    int x = (screenWidth - MyApplet.getWidth()) / 2;
    int y = (screenHight - MyApplet.getHeight()) / 2;
    MyApplet.setLocation(x, y);
    } else System.exit(0);
    }
    }
    } public class Listener2 implements ActionListener {  // the lose listener public void actionPerformed(ActionEvent e) {
    JButton n = (JButton)e.getSource();
    n.setBackground(Color.black);
    b++;
    if (b == 1 && c != 8) {
    face a1 = new face();// set the sad face
    a1.setTitle("Boom!");
    a1.setSize(400, 200);
    a1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    a1.setVisible(true);
    Dimension screenSize1 = Toolkit.getDefaultToolkit().getScreenSize();
    int screenWidth1 = screenSize1.width;
    int screenHight1 = screenSize1.height;
    int x1 = (screenWidth1 - a1.getWidth()) / 2;
    int y1 = (screenHight1 - a1.getHeight()) / 2;
    a1.setLocation(x1, y1);
    int chose = JOptionPane.showConfirmDialog(null, "BOOooM!!!"
    + "Would you like to play again?", "Message",
    JOptionPane.YES_NO_OPTION);
    setVisible(false);
    if (chose == JOptionPane.YES_OPTION) {
    a1.setVisible(false);
    c = 0;
    JFrame MyApplet = new JFrame();// set the main frame
    MyApplet.setTitle("Boom!");
    MyApplet.setSize(400, 200);
    MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyApplet.setVisible(true);
    Dimension screenSize = Toolkit.getDefaultToolkit()
    .getScreenSize(); // let the frame be
    // center
    int screenWidth = screenSize.width;
    int screenHight = screenSize.height;
    int x = (screenWidth - MyApplet.getWidth()) / 2;
    int y = (screenHight - MyApplet.getHeight()) / 2;
    MyApplet.setLocation(x, y);
    } else System.exit(0);
    }
    }
    } class face extends JFrame {// draw the happy face public face() {
    getContentPane().add(new Ovals());
    } class Ovals extends JPanel { public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
    g.setColor(Color.red);
    g.fillOval(120, 10, 30, 30);
    g.fillOval(230, 10, 30, 30);
    g.drawArc(110, 100, 160, 100, 0, 180);
    }
    }
    } class face2 extends JFrame {  // draw the sad face public face2() {
    getContentPane().add(new Ovals2());
    } class Ovals2 extends JPanel { public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
    g.setColor(Color.red);
    g.fillOval(120, 10, 30, 30);
    g.fillOval(230, 10, 30, 30);
    g.drawArc(110, 50, 160, 100, 180, 180);
    }
    }
    }}
      

  8.   

    本来INIT()也有设置FRAME的 但被我删除了
      

  9.   

    可以使用自己的 java.awt.Canvas 组件显示笑脸,如下:/*
     * FileName: MyFace.java
     * Author:   SageZk
     * Date:     2008-06-08
     */import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;public class MyFace extends Canvas { public static final int FACE_HAPPY = 1;
    public static final int FACE_SAD = 2; private int facestyle = FACE_HAPPY; public MyFace(int face) {
    if (face == FACE_HAPPY || face == FACE_SAD) this.facestyle = face;
    } public void paint(Graphics g) {
    super.paint(g);
    switch (this.facestyle) {
    case FACE_HAPPY:
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
    g.setColor(Color.red);
    g.fillOval(120, 10, 30, 30);
    g.fillOval(230, 10, 30, 30);
    g.drawArc(110, 100, 160, 100, 0, 180);
    break;
    case FACE_SAD:
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 200);
    g.setColor(Color.red);
    g.fillOval(120, 10, 30, 30);
    g.fillOval(230, 10, 30, 30);
    g.drawArc(110, 50, 160, 100, 180, 180);
    break;
    }
    } public void update(Graphics g) {
    paint(g);
    }}然后把此组件添加到你的Applet中。