2013-04-13 2 views
1

저는 초보자이며, 결국 저는 수석 프로젝트를 만들고있는 로봇 용 코드를 만들고 싶습니다. 로봇이 지정된 패턴으로 도미노를 세워 무너 뜨릴 계획입니다. 우선 도미노를 그리드에 배치하도록 선택할 수있는 프로그램을 작성해야합니다. 나는 그 프로그램이 Arduino를위한 새로운 프로그램을 인쇄하도록하는 계획을 세우고있다.초심자 : JButton/Gridlayout (지뢰 찾기와 비슷 함)

테스트와 학습을 위해 JButton으로 20x40 그리드를 만들고 싶습니다. 그런 다음 몇 가지 Jbutton을 클릭 한 다음 Jbutton 값을 새 배열에 추가하고 싶습니다. 전의. 1, 5, 30 및 799 번째 버튼을 클릭합니다. 이 건너 뛰는 것 같다 지금 문제입니다 :이 프로그램은 다음 등 array[0]=1, array[2]=5;

이 코드와 함께 와서 시행 착오와 온라인을 찾고 많은 시간을 보냈다 새로운 배열에 사람들을 추가합니다 Buttongrid 방법 (?). 이 메소드를 public static void main (String [] args) {로 만들면 액션 리스너가 작동하지 않습니다.

다시 한 번 나는 많은 일이 잘못되어도 놀라지 않을 것입니다. 그냥 살펴보고 해결해야 할 부분을 찾아 내도록 도와주세요. 감사

import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Arrays; 
public class ButtonGrid extends JFrame implements ActionListener { 
static int clicked[]=new int[800]; 
static JButton button[]=new JButton[800]; 
static int x; 
static int count=0; 
int value; 
ActionListener listen; 
    public ButtonGrid() { 
     JFrame frame= new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
     GridLayout grid=new GridLayout(20,40); 
     frame.setLayout(grid); 
    //fill clicked with 0s//////////////////////////////////// 
      for (int c=0;c<=10;c++){ 
     clicked[c]=0;} 
      for(x=0;x<800;x++){ 
      button[x]= new JButton(); 
      button[x].setActionCommand(Integer.toString(x)); 
      frame.add(button[x]); 
      button[x].addActionListener(this); 
      }} 
      public void actionPerformed (ActionEvent e){ 


      while(count<11){ 
       int newvalue=value; 
       value=Integer.parseInt(e.getActionCommand()); 
       if(value!=newvalue){ 
        clicked[count]=this.value; 
        count=count+1; 
        System.out.println("Found"); 
       } 
       else{ 
        newvalue=value; 
        System.out.println("Looking...");}} 
      } public static void main(String [] args){ 
         ButtonGrid b=new ButtonGrid(); 
         if (count>10){ 
         for (int t=0;t<=11;t++){ 
          System.out.println(clicked[t]); 
           } 
          } 
         } 
      } 
+0

는 실제 소스 코드는 같은 형식인가? 그것은 두통을 줄 것입니다 ... – syb0rg

답변

0

이 같이, 메인 클래스에 ButtonGrid 프레임을 호출하지 않았다 때문입니다 :

public static void main(String[] args) { 
     System.out.println("this prints and no window pops up. it skipped  the grid/frame code"); 
     ButtonGrid b = new ButtonGrid(); 
    } 
+0

대단히 고마워요. 그랬어. 정말 감사. 내 새로운 문제는 while 루프의 구조로 처음 10 개의 버튼 값을 int 배열에 추가 한 다음 []를 클릭하면 인쇄됩니다. 루프가 else/listening ... 영역에 갇혀 다른 버튼을 클릭 할 기회가 생기지 않습니다. 위의 코드를 업데이트했습니다. – user2278269

+0

while 루프에서 무엇을하고 싶은지 모르겠지만 코드가 영원히 반복 될 것입니다. 왜냐하면 다음과 같은 이유 때문입니다. 1 -'int newvalue = value;'2'if (value! = newvalue)' 'false'는 방정식'1'에서 영원히 벗어나서 결코 실행되지 않고'else' 문'newvalue = value;'로 바뀌므로'count'를 변경하지 않으므로 = '0'이됩니다 –

+0

this 어디서 붙어 있니, 정확히 사용하고 싶은 알고리즘을 말해줘. –

관련 문제