2014-10-11 171 views
-6
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.Random; 

public class sayangeffort extends JFrame{ 

    public JFrame instruc; 
    public JFrame start1; 
    public JFrame start2; 

    public JPanel sayang; 
    public JPanel effort; 
    public JPanel inspanel; 
    public JPanel startp1; 
    public JPanel startp2; 

    public JLabel l1; 
    public JLabel l2; 
    public JLabel point; 

    public JButton start; 
    public JButton ins; 
    public JButton exit; 
    public JButton ins1; 
    public JButton ins2; 
    public JButton ins3; 
    public JButton BackA[]=new JButton[13]; 

    PleaseGumana p=new PleaseGumana(); 
    public Timer time = new Timer(2000,p); 
    public Timer time2=new Timer(2000,p); 

    public static int num1; 
    public static int check[]=new int [12]; 
    public static int j=0,flag; 
    public static int pick1,pick2; 
    public static int verifyer1=0,verifyer2=0,counter=0, counter2=0,sample=0; 
    public static int pointcount=100; 


    Random r=new Random(); 
    Dimension Imagedim; 


public sayangeffort(){ 

    setTitle("Operation X: Memorylord"); 
    setSize(500,120); 
    buildPanel(); 
    setLayout(new BorderLayout()); 
    setResizable(false); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    add(sayang); 
    add(effort, BorderLayout.SOUTH); 

    setVisible(true); 
    } 

public void buildPanel(){ 
    sayang=new JPanel(); 
    l1=new JLabel("Operation X:"); 
    l2=new JLabel("        Memorylord"); 
    l1.setFont(new Font("Algerian", 72,25)); 
    l2.setFont(new Font("Algerian", 72,25)); 
    effort=new JPanel(); 
    start=new JButton("Start"); 
    ins=new JButton("Instruction"); 
    exit=new JButton("Exit"); 
    sayang.setLayout(new GridLayout(2,1)); 
    effort.setLayout(new GridLayout(1,3)); 
    sayang.add(l1); 
    sayang.add(l2); 
    effort.add(start); 
    effort.add(ins); 
    effort.add(exit); 

     exit.addActionListener(new MenuButton()); 
     start.addActionListener(new MenuButton()); 
     ins.addActionListener(new MenuButton()); 

    } 

public void buildStart(){ 

    startp1=new JPanel(); 
    startp1.setLayout(new GridLayout(4,3)); 
    startp2=new JPanel(); 
    point=new JLabel("Points:"+String.valueOf(pointcount)); 


    for(int i=1;i<13;i++){ 
    BackA[i]=new JButton("BACK"); 
    BackA[i].setBackground(Color.GREEN); 
    BackA[i].setPreferredSize(Imagedim); 
    BackA[i].setFont(new Font("Bodoni MT Black", Font.PLAIN,24)); 
    BackA[i].addActionListener(new PleaseGumana()); 
    } 

    for(int c=0;c<12;c++){ 

     do{ 
     flag=0; 
     num1=r.nextInt(13); 
     check[c]=num1; 

     for(int i=0;i<12;i++){ 

     if(check[i]==num1) 
      flag++; 

      } 

     }while(num1==0 || flag>1); 
     startp1.add(BackA[num1]); 
     } 

    startp2.add(point); 

    } 

public class PleaseGumana implements ActionListener{ 
    public void actionPerformed (ActionEvent e){ 

if(e.getSource()==BackA[1]){ 

    if(verifyer1<2){ 
    BackA[1].setText("12 x 5"); 
    BackA[1].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=1; 

      if(pick1==7){ 
       BackA[1].setEnabled(false); 
       BackA[7].setEnabled(false); 
       BackA[1].setBackground(Color.BLACK); 
       BackA[7].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1; 
       counter2++; 
       } 
      else if(pick1!=7){ 
       time.start(); 

       } 
     } 

    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=1; 
    } 
} 

if(e.getSource()==BackA[2]){ 
    if(verifyer1<2){ 
    BackA[2].setText("45/15"); 
    BackA[2].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=2; 

      if(pick1==8){ 
       BackA[2].setEnabled(false); 
       BackA[8].setEnabled(false); 
       BackA[2].setBackground(Color.BLACK); 
       BackA[8].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
      else if(pick1!=8){ 
       time.start(); 

       } 
     } 

    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=2; 
    } 
} 

if(e.getSource()==BackA[3]){ 
    if(verifyer1<2){ 
    BackA[3].setText("17 x 8"); 
    BackA[3].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=3; 

      if(pick1==9){ 
       BackA[3].setEnabled(false); 
       BackA[9].setEnabled(false); 
       BackA[3].setBackground(Color.BLACK); 
       BackA[9].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
      else if(pick1!=9){ 
       time.start(); 

       } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=3; 
    } 
} 


if(e.getSource()==BackA[4]){ 
    if(verifyer1<2){ 
    BackA[4].setText("31 + 14"); 
    BackA[4].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=4; 

      if(pick1==10){ 
       BackA[4].setEnabled(false); 
       BackA[10].setEnabled(false); 
       BackA[4].setBackground(Color.BLACK); 
       BackA[10].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
      else if(pick1!=10){ 
       time.start(); 

       } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=4; 
    } 
} 


if(e.getSource()==BackA[5]){ 
    if(verifyer1<2){ 
    BackA[5].setText("121 - 37"); 
    BackA[5].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=5; 
      if(pick1==11){ 
       BackA[5].setEnabled(false); 
       BackA[11].setEnabled(false); 
       BackA[5].setBackground(Color.BLACK); 
       BackA[11].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=11){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=5; 
    } 
} 


if(e.getSource()==BackA[6]){ 
    if(verifyer1<2){ 
    BackA[6].setText("72 x 2"); 
    BackA[6].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=6; 
      if(pick1==12){ 
       BackA[6].setEnabled(false); 
       BackA[12].setEnabled(false); 
       BackA[6].setBackground(Color.BLACK); 
       BackA[12].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=12){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=6; 
    } 
} 

if(e.getSource()==BackA[7]){ 
    if(verifyer1<2){ 
    BackA[7].setText("60"); 
    BackA[7].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=7; 
      if(pick1==1){ 
       BackA[1].setEnabled(false); 
       BackA[7].setEnabled(false); 
       BackA[1].setBackground(Color.BLACK); 
       BackA[7].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=1){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=7; 
    } 
} 

if(e.getSource()==BackA[8]){ 
    if(verifyer1<2){ 
    BackA[8].setText("3"); 
    BackA[8].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=8; 
      if(pick1==2){ 
       BackA[2].setEnabled(false); 
       BackA[8].setEnabled(false); 
       BackA[2].setBackground(Color.BLACK); 
       BackA[8].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=2){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=8; 
    } 
} 

if(e.getSource()==BackA[9]){ 
    if(verifyer1<2){ 
    BackA[9].setText("136"); 
    BackA[9].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=9; 
      if(pick1==3){ 
       BackA[3].setEnabled(false); 
       BackA[9].setEnabled(false); 
       BackA[3].setBackground(Color.BLACK); 
       BackA[9].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=3){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=9; 
    } 
} 

if(e.getSource()==BackA[10]){ 
    if(verifyer1<2){ 
    BackA[10].setText("45"); 
    BackA[10].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=10; 
      if(pick1==4){ 
       BackA[4].setEnabled(false); 
       BackA[10].setEnabled(false); 
       BackA[4].setBackground(Color.BLACK); 
       BackA[10].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
       } 
     else if(pick1!=4){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=10; 
    } 
} 

if(e.getSource()==BackA[11]){ 
    if(verifyer1<2){ 
    BackA[11].setText("84"); 
    BackA[11].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=11; 
      if(pick1==5){ 
       BackA[5].setEnabled(false); 
       BackA[11].setEnabled(false); 
       BackA[5].setBackground(Color.BLACK); 
       BackA[11].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
      } 
     else if(pick1!=5){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=11; 
    } 
} 

if(e.getSource()==BackA[12]){ 
    if(verifyer1<2){ 
    BackA[12].setText("144"); 
    BackA[12].setBackground(Color.RED); 
    } 
     if(verifyer1==1){ 
     verifyer1++;pick2=12; 
      if(pick1==6){ 
       BackA[6].setEnabled(false); 
       BackA[12].setEnabled(false); 
       BackA[6].setBackground(Color.BLACK); 
       BackA[12].setBackground(Color.BLACK); 
       verifyer1=0; 
       counter=1;counter2++; 
      } 
     else if(pick1!=6){ 
      time.start(); 
      } 
     } 
    if(verifyer1==0 && counter==0){ 
     verifyer1++; 
     pick1=12; 
    } 
} 




    if(counter==1){counter=0;} 
    if(e.getSource()==time && verifyer1==2){ 
     pointcount--; 
     BackA[pick1].setText("BACK"); 
     BackA[pick1].setBackground(Color.GREEN); 
     BackA[pick2].setText("BACK"); 
     BackA[pick2].setBackground(Color.GREEN); 
     verifyer1=0; 
     counter=0; 
     } 

    point.setText("Points:"+String.valueOf(pointcount)); 


    if(counter2==6){ 
     start1.dispose(); 
     buildStart2(); 
     //setVisible(true); 
     } 
    /**if(e.getSource()==time2){ 


     }*/ 
} 
} 

public void buildStart2(){ 

     start2=new JFrame(); 
     start2.setTitle("Operation X: Memorylord"); 
     start2.setSize(600,600); 
     start2.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     start2.setResizable(false); 
     start2.setLocationRelativeTo(null); 
     start2.setLayout(new BorderLayout()); 

     start2.setVisible(true); 

} 

public class MenuButton implements ActionListener{ 
    public void actionPerformed (ActionEvent e){ 

    if(e.getSource()==exit){ 
     System.exit(0); 
      } 
    if(e.getSource()==start){ 

     setVisible(false); 

     start1=new JFrame(); 
     start1.setTitle("Operation X: Memorylord"); 
     start1.setSize(600,600); 
     start1.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     start1.setResizable(false); 
     start1.setLocationRelativeTo(null); 
     start1.setLayout(new BorderLayout()); 
     buildStart(); 
     start1.add(startp1); 
     start1.add(startp2, BorderLayout.SOUTH); 

     start1.setVisible(true); 
      } 
    if(e.getSource()==ins){ 
     dispose(); 
     instruc=new JFrame(); 
     instruc.setTitle("Operation X: Memorylord"); 
     instruc.setSize(500,600); 
     instruc.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     instruc.setResizable(false); 
     instruc.setLocationRelativeTo(null); 
     instruc.setVisible(true); 
      } 

} 
} 


public static void main(String args [])throws Exception{ 

    sayangeffort cc=new sayangeffort(); 

} 
} 

這是我的遊戲代碼,它還沒有完成,它沒有指令,但嘗試播放並完成它,你會看到我的問題,幀不斷出現,但我看不出什麼錯我的代碼爲什麼會發生? java

+0

請格式化你的代碼,使其易於讀取。 – 2014-10-11 08:07:49

+0

這就是您在創建新JFrame並使其可見時會發生的情況:新的JFrame被構建並顯示出來。因爲這就是你在聽衆中所做的事情,所以這不應該令人驚訝。 – 2014-10-11 08:08:08

+0

但它是不停的?這是爲什麼?通常它應該出現一次。 – 2014-10-11 08:09:37

回答

0

基本上,time是重複的Swing Timer從未停止

這意味着,雖然counter2等於6 ....

if (counter2 == 6) { 
    start1.dispose(); 
    buildStart2(); 
    //setVisible(true); 
} 

它將創建的,但,另一架...

我建議......

+0

謝謝!我停下了計時器,現在它正常工作!更大的功率 – 2014-10-11 08:24:00

+0

我應該在哪裏使用cardLayout? – 2014-10-11 08:27:34

+0

刪除除主框架外的所有框架。當你想在視圖之間切換時,使用'CardLayout'來決定顯示哪個視圖 – MadProgrammer 2014-10-11 08:37:25