2015-01-28 3 views
0

괜찮 았던가요, 새 문제가 생겼습니다. 버튼이 매초마다 특정 requeriments를 충족하는지 확인하는 타이머를 만들었습니다.이 방법이 가장 좋지만이 프로그램을 사용하지 않을 것이라고 알고 있습니다. 오랫동안 나는 단지 그것을 원한다.타이머가 회의 조건에 오류가 발생했습니다.

제가하는 데 문제는 타이머가 나에게 다음과 같은 오류 준다 :

Exception in thread "Timer-0" java.lang.NullPointerException 
    at proyecto.Diseños$RemindTask.run(Diseños.java:3561) 
    at java.util.TimerThread.mainLoop(Timer.java:555) 
    at java.util.TimerThread.run(Timer.java:505) 

라인을 3561입니다 :

if(Boton61.getBackground() == Color.red && Boton51.getBackground() == Color.red && Boton41.getBackground() == Color.red && Boton31.getBackground() == Color.red 
       && colores.colorjugador[0] == 1) System.out.println("El jugador ha ganado !"); 

전체 코드는 다음과 같습니다

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package proyecto; 


import java.awt.Color; 
import java.awt.Component; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import static proyecto.Proyecto.turnosjugador1; 
import static proyecto.Proyecto.turnosjugador2; 

import java.util.Timer; 
import java.util.TimerTask; 




/** 
* 
* @author Cristian 
* 
* */ 






public class Diseños extends JComponent implements ActionListener { 

    JButton Boton1, Boton2, Boton3, Boton4, Boton5, Boton6, Boton7; 

    JButton Boton11,Boton21,Boton31,Boton41,Boton51,Boton61; 
    JButton Boton12,Boton22,Boton32,Boton42,Boton52,Boton62; 
    JButton Boton13,Boton23,Boton33,Boton43,Boton53,Boton63; 
    JButton Boton14,Boton24,Boton34,Boton44,Boton54,Boton64; 
    JButton Boton15,Boton25,Boton35,Boton45,Boton55,Boton65; 
    JButton Boton16,Boton26,Boton36,Boton46,Boton56,Boton66; 
    JButton Boton17,Boton27,Boton37,Boton47,Boton57,Boton67; 




    public Diseños() 
    { 

     Timer timer; 
     timer = new Timer(); 
     timer.schedule(new RemindTask(), 0, //initial delay 
     1 * 1000); 

    Boton1=new JButton("lol"); 
    Boton1.setBounds(90, 410, 30, 30); 
    Boton1.setBackground(Color.black); 
    Component agregar1 = add(Boton1); 
    Boton1.addActionListener(this); 

    Boton2=new JButton("lol"); 
    Boton2.setBounds(150, 410, 30, 30); 
    Boton2.setBackground(Color.black); 
    Component agregar2 = add(Boton2); 
    Boton2.addActionListener(this); 

    Boton3=new JButton("lol"); 
    Boton3.setBounds(210, 410, 30, 30); 
    Boton3.setBackground(Color.black); 
    Component agregar3 = add(Boton3); 
    Boton3.addActionListener(this); 

    Boton4=new JButton("lol"); 
    Boton4.setBounds(270, 410, 30, 30); 
    Boton4.setBackground(Color.black); 
    Component agregar4 = add(Boton4); 
    Boton4.addActionListener(this); 

    Boton5=new JButton("lol"); 
    Boton5.setBounds(330, 410, 30, 30); 
    Boton5.setBackground(Color.black); 
    Component agregar5 = add(Boton5); 
    Boton5.addActionListener(this); 

    Boton6=new JButton("lol"); 
    Boton6.setBounds(390, 410, 30, 30); 
    Boton6.setBackground(Color.black); 
    Component agregar6 = add(Boton6); 
    Boton6.addActionListener(this); 

    Boton7=new JButton("lol"); 
    Boton7.setBounds(450, 410, 30, 30); 
    Boton7.setBackground(Color.black); 
    Component agregar7 = add(Boton7); 
    Boton7.addActionListener(this); 


    Boton11=new JButton(""); 
    Boton11.setBounds(85, 10, 40, 60); 
    Boton11.setBackground(Color.white); 
    Component agregar11 = add(Boton11); 
    Boton11.addActionListener(this); 

    Boton21=new JButton(""); 
    Boton21.setBounds(85, 70, 40, 60); 
    Boton21.setBackground(Color.white); 
    Component agregar21 = add(Boton21); 
    Boton21.addActionListener(this); 

    Boton31=new JButton(""); 
    Boton31.setBounds(85, 130, 40, 60); 
    Boton31.setBackground(Color.white); 
    Component agregar31 = add(Boton31); 
    Boton31.addActionListener(this); 

    Boton41=new JButton(""); 
    Boton41.setBounds(85, 190, 40, 60); 
    Boton41.setBackground(Color.white); 
    Component agregar41 = add(Boton41); 
    Boton41.addActionListener(this); 

    Boton51=new JButton(""); 
    Boton51.setBounds(85, 250, 40, 60); 
    Boton51.setBackground(Color.white); 
    Component agregar51 = add(Boton51); 
    Boton51.addActionListener(this); 

    Boton61=new JButton(""); 
    Boton61.setBounds(85, 310, 40, 60); 
    Boton61.setBackground(Color.white); 
    Component agregar61 = add(Boton61); 
    Boton61.addActionListener(this); 


    Boton12=new JButton(""); 
    Boton12.setBounds(145, 10, 40, 60); 
    Boton12.setBackground(Color.white); 
    Component agregar12 = add(Boton12); 
    Boton12.addActionListener(this); 

    Boton22=new JButton(""); 
    Boton22.setBounds(145, 70, 40, 60); 
    Boton22.setBackground(Color.white); 
    Component agregar22 = add(Boton22); 
    Boton22.addActionListener(this); 

    Boton32=new JButton(""); 
    Boton32.setBounds(145, 130, 40, 60); 
    Boton32.setBackground(Color.white); 
    Component agregar32 = add(Boton32); 
    Boton32.addActionListener(this); 

    Boton42=new JButton(""); 
    Boton42.setBounds(145, 190, 40, 60); 
    Boton42.setBackground(Color.white); 
    Component agregar42 = add(Boton42); 
    Boton42.addActionListener(this); 

    Boton52=new JButton(""); 
    Boton52.setBounds(145, 250, 40, 60); 
    Boton52.setBackground(Color.white); 
    Component agregar52 = add(Boton52); 
    Boton52.addActionListener(this); 

    Boton62=new JButton(""); 
    Boton62.setBounds(145, 310, 40, 60); 
    Boton62.setBackground(Color.white); 
    Component agregar62 = add(Boton62); 
    Boton62.addActionListener(this); 

    Boton13=new JButton(""); 
    Boton13.setBounds(205, 10, 40, 60); 
    Boton13.setBackground(Color.white); 
    Component agregar13 = add(Boton13); 
    Boton13.addActionListener(this); 

    Boton23=new JButton(""); 
    Boton23.setBounds(205, 70, 40, 60); 
    Boton23.setBackground(Color.white); 
    Component agregar23 = add(Boton23); 
    Boton23.addActionListener(this); 

    Boton33=new JButton(""); 
    Boton33.setBounds(205, 130, 40, 60); 
    Boton33.setBackground(Color.white); 
    Component agregar33 = add(Boton33); 
    Boton33.addActionListener(this); 

    Boton43=new JButton(""); 
    Boton43.setBounds(205, 190, 40, 60); 
    Boton43.setBackground(Color.white); 
    Component agregar43 = add(Boton43); 
    Boton43.addActionListener(this); 

    Boton53=new JButton(""); 
    Boton53.setBounds(205, 250, 40, 60); 
    Boton53.setBackground(Color.white); 
    Component agregar53 = add(Boton53); 
    Boton53.addActionListener(this); 

    Boton63=new JButton(""); 
    Boton63.setBounds(205, 310, 40, 60); 
    Boton63.setBackground(Color.white); 
    Component agregar63 = add(Boton63); 
    Boton63.addActionListener(this); 

    Boton14=new JButton(""); 
    Boton14.setBounds(265, 10, 40, 60); 
    Boton14.setBackground(Color.white); 
    Component agregar14 = add(Boton14); 
    Boton14.addActionListener(this); 

    Boton24=new JButton(""); 
    Boton24.setBounds(265, 70, 40, 60); 
    Boton24.setBackground(Color.white); 
    Component agregar24 = add(Boton24); 
    Boton24.addActionListener(this); 

    Boton34=new JButton(""); 
    Boton34.setBounds(265, 130, 40, 60); 
    Boton34.setBackground(Color.white); 
    Component agregar34 = add(Boton34); 
    Boton34.addActionListener(this); 

    Boton44=new JButton(""); 
    Boton44.setBounds(265, 190, 40, 60); 
    Boton44.setBackground(Color.white); 
    Component agregar44 = add(Boton44); 
    Boton44.addActionListener(this); 

    Boton54=new JButton(""); 
    Boton54.setBounds(265, 250, 40, 60); 
    Boton54.setBackground(Color.white); 
    Component agregar54 = add(Boton54); 
    Boton54.addActionListener(this); 

    Boton64=new JButton(""); 
    Boton64.setBounds(265, 310, 40, 60); 
    Boton64.setBackground(Color.white); 
    Component agregar64 = add(Boton64); 
    Boton64.addActionListener(this); 

    Boton15=new JButton(""); 
    Boton15.setBounds(325, 10, 40, 60); 
    Boton15.setBackground(Color.white); 
    Component agregar15 = add(Boton15); 
    Boton15.addActionListener(this); 

    Boton25=new JButton(""); 
    Boton25.setBounds(325, 70, 40, 60); 
    Boton25.setBackground(Color.white); 
    Component agregar25 = add(Boton25); 
    Boton25.addActionListener(this); 

    Boton35=new JButton(""); 
    Boton35.setBounds(325, 130, 40, 60); 
    Boton35.setBackground(Color.white); 
    Component agregar35 = add(Boton35); 
    Boton35.addActionListener(this); 

    Boton45=new JButton(""); 
    Boton45.setBounds(325, 190, 40, 60); 
    Boton45.setBackground(Color.white); 
    Component agregar45 = add(Boton45); 
    Boton45.addActionListener(this); 

    Boton55=new JButton(""); 
    Boton55.setBounds(325, 250, 40, 60); 
    Boton55.setBackground(Color.white); 
    Component agregar55 = add(Boton55); 
    Boton55.addActionListener(this); 

    Boton65=new JButton(""); 
    Boton65.setBounds(325, 310, 40, 60); 
    Boton65.setBackground(Color.white); 
    Component agregar65 = add(Boton65); 
    Boton65.addActionListener(this); 

    Boton16=new JButton(""); 
    Boton16.setBounds(385, 10, 40, 60); 
    Boton16.setBackground(Color.white); 
    Component agregar16 = add(Boton16); 
    Boton16.addActionListener(this); 

    Boton26=new JButton(""); 
    Boton26.setBounds(385, 70, 40, 60); 
    Boton26.setBackground(Color.white); 
    Component agregar26 = add(Boton26); 
    Boton26.addActionListener(this); 

    Boton36=new JButton(""); 
    Boton36.setBounds(385, 130, 40, 60); 
    Boton36.setBackground(Color.white); 
    Component agregar36 = add(Boton36); 
    Boton36.addActionListener(this); 

    Boton46=new JButton(""); 
    Boton46.setBounds(385, 190, 40, 60); 
    Boton46.setBackground(Color.white); 
    Component agregar46 = add(Boton46); 
    Boton46.addActionListener(this); 

    Boton56=new JButton(""); 
    Boton56.setBounds(385, 250, 40, 60); 
    Boton56.setBackground(Color.white); 
    Component agregar56 = add(Boton56); 
    Boton56.addActionListener(this); 

    Boton66=new JButton(""); 
    Boton66.setBounds(385, 310, 40, 60); 
    Boton66.setBackground(Color.white); 
    Component agregar66 = add(Boton66); 
    Boton66.addActionListener(this); 

    Boton17=new JButton(""); 
    Boton17.setBounds(445, 10, 40, 60); 
    Boton17.setBackground(Color.white); 
    Component agregar17 = add(Boton17); 
    Boton17.addActionListener(this); 

    Boton27=new JButton(""); 
    Boton27.setBounds(445, 70, 40, 60); 
    Boton27.setBackground(Color.white); 
    Component agregar27 = add(Boton27); 
    Boton27.addActionListener(this); 

    Boton37=new JButton(""); 
    Boton37.setBounds(445, 130, 40, 60); 
    Boton37.setBackground(Color.white); 
    Component agregar37 = add(Boton37); 
    Boton37.addActionListener(this); 

    Boton47=new JButton(""); 
    Boton47.setBounds(445, 190, 40, 60); 
    Boton47.setBackground(Color.white); 
    Component agregar47 = add(Boton47); 
    Boton47.addActionListener(this); 

    Boton57=new JButton(""); 
    Boton57.setBounds(445, 250, 40, 60); 
    Boton57.setBackground(Color.white); 
    Component agregar57 = add(Boton57); 
    Boton57.addActionListener(this); 

    Boton67=new JButton(""); 
    Boton67.setBounds(445, 310, 40, 60); 
    Boton67.setBackground(Color.white); 
    Component agregar67 = add(Boton67); 
    Boton67.addActionListener(this); 



    } 

    @Override 
    public void paintComponent(Graphics g) {  



    super.paintComponent(g); 
    g.drawRect (570, 10, 300, 200); 
    g.setColor(Color.BLACK); 
    g.fillRect(570, 10, 300, 200); 
    g.drawRect (570, 250, 300, 200); 
    g.setColor(Color.BLACK); 
    g.fillRect(570, 250, 300, 200); 
    g.setColor(Color.WHITE); 
    Font oldFont=getFont(); 
    Font fuente=new Font("Primer Jugador", Font.BOLD, 14); 
    g.setFont(fuente); 
    g.drawString("Primer Jugador", 580, 30); 
    Font titulo=new Font("Segundo Jugador", Font.BOLD, 14); 
    g.setFont(titulo); 
    g.drawString("Segundo Jugador", 580, 270); 
    g.drawString("[ " + Proyecto.primernombre + " ]", 580, 50); 
    g.drawString("[ " + Proyecto.segundonombre + " ]", 580, 290); 
    g.setColor(Color.CYAN); 
    g.fillRoundRect(10, 5, 550, 450, 50, 50); 
    g.drawString("Turno Numero : " + turnosjugador1, 580, 70); 
    g.drawString("Turno Numero : " + turnosjugador2, 580, 310); 


    } 











    } 


class RemindTask extends TimerTask { 


    @Override 
    public void run() { 

     if(Boton61.getBackground() == Color.red && Boton51.getBackground() == Color.red && Boton41.getBackground() == Color.red && Boton31.getBackground() == Color.red 
       && colores.colorjugador[0] == 1) System.out.println("El jugador ha ganado !"); 

     //timer.cancel(); //Not necessary because we call System.exit  




    } 
    } 



} 

I 많은 줄들이 불필요한 공간을 낭비한다는 것을 알고 있고, 프로그램은 루프를 사용하여 길어질 수 있지만, 나는 그것을 만들 필요가있다. 가능한 한 간단하게 다른 학생들이 이해할 수 있도록

저는 타이머가 저에게 IF를 사용할 때 타이머가 저에게 왜 저에게주는 지 알고 싶습니다, 어떤 충고도 크게 감사드립니다.

+0

입니다 'javax.swing.Timer'를 사용하십시오 – MadProgrammer

답변

1

그것은 클래스가 초기화되는 동안 JavaDocs을 살펴 경우 타이머가 ... 실행 한 가능성이 높습니다의 delay 매개 변수

지연이다 - 밀리 초 지연 작업을하기 전에 처형되다.

이것은 거의 즉시 실행되어 가능성이있는 인스턴스 필드 중 일부가 아직 초기화되지 않았 음을 의미합니다.

스윙 UI를 다루는 경우 일반적으로 javax.swing.Timer을 사용하는 것이 좋으며, 이벤트 발송 스레드의 컨텍스트 내에서 ActionListener이라고 부르기 때문에 경쟁 조건이 발생할 위험이 줄어 듭니다.

또한 단순히 버튼에 ActionListener를 부착하고 클릭 될 때 상태를 확인하는 것이 좋습니다 것입니다,하지만 당신은 타이머의 어떤 종류의, 당신이해야 통해 UI와 상호 작용해야하는 경우 그건 그냥 내 생각

+0

신경 쓰지 마세요. 지연에 3 초를 더했습니다. 지식을 공유해 주셔서 감사합니다. –

+0

현재 이동이 완료되었을 때 승리 조건이 성립되었는지 확인할 수없는 이유는 무엇입니까? – MadProgrammer