2009-04-02 3 views
2
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.util.Random; 
/** 
* 
*/ 
public class GUI 
{ 
    private static final int BTN_MAX = 8; 


    private JFrame frame; 
    private JPanel panel; 
    private JPanel scores; 
    private JButton[] buttons; 
    private ImageIcon[] icons; 

    private Random rand; 

    private ImageIcon beach; 
    private ImageIcon lips; 
    private ImageIcon discoball; 
    private ImageIcon flowers; 
    private ImageIcon blank; 




    /** 
    * 
    */ 
    public GUI() 
    {   
     beach = new ImageIcon("beach.jpg"); 
     lips = new ImageIcon("lips.jpg"); 
     discoball = new ImageIcon ("discoball.jpg"); 
     flowers = new ImageIcon ("flowers.jpg"); 
     blank = new ImageIcon ("blank.jpg"); 

     buttons = makeButtons(); 
     rand = new Random(); 
     startingCondition(); 

     icons = new ImageIcon[] { beach, lips, discoball, lips, beach, flowers, discoball, flowers}; 


     makeFrame(); 
     makeMenuBar(frame);   
     frame.pack(); 
     frame.setVisible(true); 


    } 

    /** 
    * Makes the frame for the gui, inclusive of adding all components. 
    */ 
    private void makeFrame() 
    { 
     int horizGap = 25; // Using this for spaces between the grid layout components 
     int vertGap = 25; // Using this for the spaces between the grid layout componeents 

     frame = new JFrame("Noughts and Crosses"); 
     Container contentPane = frame.getContentPane(); 
     contentPane.setLayout(new GridLayout(4,2));//set layout of frame to BorderLayout 

     for (int i = 0; i < BTN_MAX; i++){ 
      contentPane.add(buttons[i]); 
     } 
    } 

    /** 
    * 
    */ 
    private void makeMenuBar(JFrame frame) 
    {    
     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     JMenu menu; 
     JMenuItem item; 

     menu = new JMenu("File"); 
     menubar.add(menu); 

     item = new JMenuItem("Reset Entire Game"); 
      item.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       } 
      }); 
     menu.add(item); 

     item = new JMenuItem("Reset This Game"); 
      item.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       } 
      }); 
     menu.add(item);  

     item = new JMenuItem("Quit"); 
      item.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       } 
      }); 
     menu.add(item);    

     frame.addWindowListener(new WindowAdapter() 
     { 
      public void windowClosing(WindowEvent e) 
      { 

      } 
     }); 

     menu = new JMenu("About"); 
     menubar.add(menu); 

     item = new JMenuItem("About The Game"); 
      item.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       } 
      }); 
     menu.add(item); 
    }  


    /** 
    * 
    */ 
    private JButton[] makeButtons() 
    { 
     final JButton button[] = new JButton[8]; 
     for (int i = 0; i < BTN_MAX; i++) 
     { 
      button[i] = new JButton(""); 
      button[i].setPreferredSize(new Dimension(200, 100)); 
      final int tmp = i; 
      button[i].addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
        checkForPair(); 
        takeGo(); 
       } 
      }); 
     } 
     return button; 
    } 

    /** 
    * 
    */ 
    private void startingCondition() 
    { 
     for (int i = 0; i < 8; i++){ 
      buttons[i].setIcon(blank); 
     } 
    } 


    /** 
    * 
    */ 
    public int returnButtonNumber() 
    { 
     return 0; 
     // need to tell which button in the array has been clicked 
     //so i can send a value to the takeGo method 
     //appropriatley 
    } 

    public ImageIcon getIcon(int number) 
    { 
     return icons[number]; 
    } 

    /** 
    * 
    */ 
    public boolean checkForPair() 
    { 
     return false; 
    } 

    /** 
    * 
    */ 
    public void takeGo() 
    { 
     int i = returnButtonNumber(); 

     buttons[i].setIcon(getIcon(i)); 
    } 
} 

안녕하세요, 누군가가 어떤 버튼 번호가 눌려 졌는지 확인하는 방법을 도울 수 있는지 궁금 해서요.이 값을 다른 방법으로 보내서 표시 할 수 있습니다. 적절한 이미지, 모든 아이디어는어떤 버튼 번호가 눌 렸는지 알 수있는 도움말

가에 준하여 당신에게

답변

3

감사 그래도 예는 좋은 것입니다,하지만 당신은 자신을 내가 시도하려는 것처럼 가능한 한 전체 코드를 피하려고하고 그것을 할하시기 바랍니다 수 환영합니다 가능한 최소한의 정보를 제공합니다.) - ... JButton.setActionCommand()를 살펴보십시오

또한 모든 버튼에 대해 동일한 ActionListener를 사용하고 어떤 버튼을 눌렀는지 확인하려는 경우 ActionListener를 한 번만 인스턴스화하고 모든 버튼에 하나씩 생성하지 않고 모든 버튼에 추가해야합니다.

+0

+1, 난 모든 버튼에 대해 하나의 ActionListener를 사용합니다 :에

로 –

1

JButton [] 버튼과 함께 ActionEvent.getSource()를 사용하여 해당 actionPerformed를 트리거 한 버튼의 색인을 확인할 수 있습니다. 나는 충분히 그럴 수 있었으면 좋겠다.

1

각기 다른 동작마다 ActionListener의 다른 인스턴스를 사용한다. 동일한 클래스 일 수도 있고, 같은 매개 변수화 된 방법으로 다른 것들과 함께 구성 될 수도 있습니다.

addItem("Quit", quitCommand); 
... 
private void addItem(String text, final Runnable command) { 
    JMenuItem item = new JMenuItem(text); 
    item.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      command.run(); 
     } 
    }); 
    menu.add(item); 
} 
관련 문제